PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.6
MxChat – AI Chatbot & Content Generation for WordPress v2.4.6
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | admin/class-knowledge-manager.php +4525 -9494 3.2.212.4.6 View file →
@@ -1,9495 +1,4526 @@
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 - // Post IDs whose vectors were already deleted by mxchat_handle_status_transition this
17 - // request, so the transient-based branch in mxchat_handle_post_update can skip the
18 - // redundant (idempotent but network-visible) second deletion.
19 - private $transition_deleted_posts = array();
20 -
21 - // Post IDs already INDEXED by mxchat_handle_status_transition's arrival edge this
22 - // request. Normal editor publishes fire transition_post_status first, then
23 - // post_updated — without this guard every editor publish would embed twice.
24 - private $transition_indexed_posts = array();
25 -
26 - // Post IDs core has announced an in-flight UPDATE for. pre_post_update fires only
27 - // inside wp_insert_post's update branch and always before wp_transition_post_status,
28 - // so this is an exact "a post_updated is coming later this request" signal — which is
29 - // what makes it safe to arm transition_indexed_posts (plan a664f3).
30 - private $pending_post_update = array();
31 -
32 - /**
33 - * Constructor - Register hooks for content processing
34 - */
35 -public function __construct() {
36 - $this->options = get_option('mxchat_options', array());
37 - $this->mxchat_init_hooks();
38 -
39 - $this->mxchat_init_role_hooks();
40 -}
41 -
42 -/**
43 - * Initialize WordPress hooks for content processing
44 - *
45 - */
46 -private function mxchat_init_hooks() {
47 - // Admin post handlers for form submissions
48 - add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission'));
49 - add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission'));
50 - add_action('admin_post_mxchat_submit_pdf_file', array($this, 'mxchat_handle_pdf_file_submission'));
51 - add_action('admin_post_mxchat_submit_document_file', array($this, 'mxchat_handle_document_file_submission'));
52 - add_action('admin_post_mxchat_submit_youtube', array($this, 'mxchat_handle_youtube_submission'));
53 - add_action('admin_post_mxchat_stop_processing', array($this, 'mxchat_stop_processing'));
54 -
55 - // AJAX handlers for real-time processing and status updates
56 - add_action('wp_ajax_mxchat_get_status_updates', array($this, 'mxchat_ajax_get_status_updates'));
57 - add_action('wp_ajax_mxchat_dismiss_completed_status', array($this, 'mxchat_ajax_dismiss_completed_status'));
58 - add_action('wp_ajax_mxchat_get_content_list', array($this, 'ajax_mxchat_get_content_list'));
59 - add_action('wp_ajax_mxchat_process_selected_content', array($this, 'ajax_mxchat_process_selected_content'));
60 - add_action('wp_ajax_mxchat_save_inline_prompt', array($this, 'mxchat_save_inline_prompt'));
61 - add_action('admin_post_mxchat_delete_pinecone_prompt', array($this, 'mxchat_handle_pinecone_prompt_delete'));
62 - add_action('wp_ajax_mxchat_delete_pinecone_prompt', array($this, 'ajax_mxchat_delete_pinecone_prompt'));
63 - add_action('wp_ajax_mxchat_delete_chunks_by_url', array($this, 'ajax_mxchat_delete_chunks_by_url'));
64 - add_action('wp_ajax_mxchat_delete_wordpress_prompt', array($this, 'ajax_mxchat_delete_wordpress_prompt'));
65 - add_action('wp_ajax_mxchat_bulk_delete_knowledge', array($this, 'ajax_mxchat_bulk_delete_knowledge'));
66 - add_action('wp_ajax_mxchat_update_role_restriction', array($this, 'ajax_mxchat_update_role_restriction'));
67 -
68 - // Queue-based processing AJAX handlers
69 - add_action('wp_ajax_mxchat_get_next_queue_item', array($this, 'ajax_mxchat_get_next_queue_item'));
70 - add_action('wp_ajax_mxchat_process_queue_item', array($this, 'ajax_mxchat_process_queue_item'));
71 - add_action('wp_ajax_mxchat_get_queue_status', array($this, 'ajax_mxchat_get_queue_status'));
72 - add_action('wp_ajax_mxchat_clear_queue', array($this, 'ajax_mxchat_clear_queue'));
73 - add_action('wp_ajax_mxchat_retry_failed', array($this, 'ajax_mxchat_retry_failed'));
74 - add_action('wp_ajax_mxchat_get_recent_entries', array($this, 'ajax_mxchat_get_recent_entries'));
75 - add_action('wp_ajax_mxchat_detect_sitemaps', array($this, 'ajax_mxchat_detect_sitemaps'));
76 - add_action('wp_ajax_mxchat_refresh_pinecone_entries', array($this, 'ajax_mxchat_refresh_pinecone_entries'));
77 - add_action('wp_ajax_mxchat_paginate_entries', array($this, 'ajax_mxchat_paginate_entries'));
78 - add_action('wp_ajax_mxchat_get_entry_content', array($this, 'ajax_mxchat_get_entry_content'));
79 - add_action('wp_ajax_mxchat_save_entry_content', array($this, 'ajax_mxchat_save_entry_content'));
80 - add_action('wp_ajax_mxchat_inspect_entry', array($this, 'ajax_mxchat_inspect_entry'));
81 -
82 - // WordPress post management hooks
83 - add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
84 - add_action('post_updated', array($this, 'mxchat_handle_post_update'), 10, 3);
85 - add_action('before_delete_post', array($this, 'mxchat_handle_post_delete'));
86 - add_action('wp_trash_post', array($this, 'mxchat_handle_post_delete'));
87 - // Authoritative unpublish detection: core hands this hook the REAL previous status, so
88 - // removal no longer depends on the mxchat_prev_status_* transients (evictable by persistent
89 - // object caches, never written by paths that bypass wp_update_post, e.g. plugins flipping
90 - // post_status directly and calling wp_transition_post_status themselves).
91 - add_action('transition_post_status', array($this, 'mxchat_handle_status_transition'), 10, 3);
92 -
93 - // ACF hook - fires AFTER ACF fields are saved, ensuring ACF data is available
94 - // Priority 20 to run after ACF's own save (which runs at priority 10)
95 - add_action('acf/save_post', array($this, 'mxchat_handle_acf_save'), 20);
96 -
97 - // One-time cleanup for vectors orphaned by unpublishes that predate the
98 - // transition_post_status handler (plan 816fb1): wp mxchat prune-unpublished
99 - if (defined('WP_CLI') && WP_CLI) {
100 - WP_CLI::add_command('mxchat prune-unpublished', array($this, 'cli_prune_unpublished'));
101 - // In-place repair for RTL KB rows imported in visual order before the
102 - // 32bf9e normalizer existed: wp mxchat rtl-repair (plan d1e6f7)
103 - WP_CLI::add_command('mxchat rtl-repair', array($this, 'cli_rtl_repair'));
104 - }
105 -
106 - add_action('wp_ajax_mxchat_mark_queue_complete', array($this, 'ajax_mxchat_mark_queue_complete'));
107 -
108 - // WooCommerce product hooks (if WooCommerce is active)
109 - if (class_exists('WooCommerce')) {
110 - add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
111 - add_action('save_post_product', array($this, 'mxchat_handle_product_change'), 10, 3);
112 - add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
113 - add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
114 - }
115 -}
116 -
117 - /**
118 - * Get current options (refreshed)
119 - */
120 - private function mxchat_get_options() {
121 - if (empty($this->options)) {
122 - $this->options = get_option('mxchat_options', array());
123 - }
124 - return $this->options;
125 - }
126 -
127 -
128 - // ========================================
129 - // MAIN CONTENT SUBMISSION HANDLERS
130 - // ========================================
131 -
132 -public function mxchat_handle_content_submission() {
133 - // Check if the form was submitted and the user has permission.
134 - if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) {
135 - return;
136 - }
137 -
138 - // Verify the nonce.
139 - $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : '';
140 - if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) {
141 - wp_die(esc_html__('Nonce verification failed.', 'mxchat'));
142 - }
143 -
144 - // Sanitize the inputs.
145 - // Use wp_kses_post to allow safe HTML and wp_unslash to remove WordPress added slashes
146 - $article_content = wp_kses_post(wp_unslash($_POST['article_content']));
147 - $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
148 -
149 - // Get bot_id from form submission
150 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
151 -
152 - // Get bot-specific options and API key
153 - $bot_options = $this->get_bot_options($bot_id);
154 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
155 -
156 - // Custom-provider-aware decision; keyless custom sites must pass (plan cbd5fd).
157 - $preflight = MxChat_Utils::embedding_preflight($options);
158 - if (!$preflight['ok']) {
159 - set_transient('mxchat_admin_notice_error', esc_html($preflight['reason']), 30);
160 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
161 - exit;
162 - }
163 - $api_key = $preflight['api_key'];
164 -
165 - // Use centralized utility function with bot_id
166 - $result = MxChat_Utils::submit_content_to_db($article_content, $article_url, $api_key, null, $bot_id);
167 -
168 - if (is_wp_error($result)) {
169 - set_transient('mxchat_admin_notice_error',
170 - esc_html__('Error storing content: ', 'mxchat') . $result->get_error_message(),
171 - 30
172 - );
173 - } else {
174 - set_transient('mxchat_admin_notice_success',
175 - esc_html__('Content successfully submitted!', 'mxchat'),
176 - 30
177 - );
178 - }
179 -
180 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
181 - exit;
182 -}
183 -
184 -/**
185 - * Handle the "YouTube" KB import source (admin-post form submission).
186 - *
187 - * Per-video description mode:
188 - * - auto: fetch oEmbed metadata (reliable) + best-effort captions transcript.
189 - * If no usable transcript, index the metadata anyway, tell the admin,
190 - * and bounce back with the manual box pre-filled (never fail silently).
191 - * - manual: the admin's own description is what gets indexed; metadata rides along.
192 - *
193 - * The row is stored with content_type 'youtube' and source_url = the canonical
194 - * watch URL, so re-importing the same video UPDATES the entry (source_url
195 - * duplicate handling in MxChat_Utils::store_in_wordpress_db) — that is also the
196 - * "augment a metadata-only entry" path.
197 - */
198 -public function mxchat_handle_youtube_submission() {
199 - if (!isset($_POST['submit_youtube']) || !current_user_can('manage_options')) {
200 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
201 - }
202 -
203 - check_admin_referer('mxchat_submit_youtube_action', 'mxchat_submit_youtube_nonce');
204 -
205 - $redirect_url = admin_url('admin.php?page=mxchat-prompts');
206 -
207 - $youtube_url = isset($_POST['youtube_url']) ? esc_url_raw(wp_unslash($_POST['youtube_url'])) : '';
208 - $video_id = MxChat_Utils::parse_youtube_id($youtube_url);
209 -
210 - if (empty($video_id)) {
211 - set_transient('mxchat_admin_notice_error',
212 - esc_html__('That does not look like a link to a single YouTube video. Please paste a watch, youtu.be, or Shorts URL.', 'mxchat'),
213 - 30
214 - );
215 - wp_safe_redirect(esc_url($redirect_url));
216 - exit;
217 - }
218 -
219 - $canonical_url = 'https://www.youtube.com/watch?v=' . $video_id;
220 -
221 - $description_mode = (isset($_POST['youtube_description_mode']) && $_POST['youtube_description_mode'] === 'manual') ? 'manual' : 'auto';
222 - $manual_description = isset($_POST['youtube_description']) ? trim(wp_kses_post(wp_unslash($_POST['youtube_description']))) : '';
223 -
224 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
225 -
226 - // Resolve the embedding decision exactly like the sibling handlers —
227 - // custom-provider-aware (plan cbd5fd).
228 - $bot_options = $this->get_bot_options($bot_id);
229 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
230 -
231 - $preflight = MxChat_Utils::embedding_preflight($options);
232 - if (!$preflight['ok']) {
233 - set_transient('mxchat_admin_notice_error', esc_html($preflight['reason']), 30);
234 - wp_safe_redirect(esc_url($redirect_url));
235 - exit;
236 - }
237 - $api_key = $preflight['api_key'];
238 -
239 - // Metadata is fetched in BOTH modes — it is the reliable half of auto, and in
240 - // manual mode it enriches the indexed text with the real title/channel.
241 - $meta = $this->mxchat_fetch_youtube_oembed($video_id);
242 - $video_title = isset($meta['title']) ? sanitize_text_field($meta['title']) : '';
243 - $video_channel = isset($meta['author_name']) ? sanitize_text_field($meta['author_name']) : '';
244 -
245 - $header_lines = 'YouTube Video: ' . ($video_title !== '' ? $video_title : $canonical_url) . "\n";
246 - if ($video_channel !== '') {
247 - $header_lines .= 'Channel: ' . $video_channel . "\n";
248 - }
249 - $header_lines .= 'URL: ' . $canonical_url . "\n\n";
250 -
251 - $transcript_missing = false;
252 -
253 - if ($description_mode === 'manual') {
254 - if ($manual_description === '') {
255 - set_transient('mxchat_admin_notice_error',
256 - esc_html__('Please write a description for the video, or switch to auto-fetch.', 'mxchat'),
257 - 30
258 - );
259 - wp_safe_redirect(esc_url($redirect_url));
260 - exit;
261 - }
262 - $indexed_text = $header_lines . $manual_description;
263 - } else {
264 - $transcript = $this->mxchat_fetch_youtube_transcript($video_id);
265 -
266 - if (strlen($transcript) >= 200) {
267 - $indexed_text = $header_lines . $transcript;
268 - } else {
269 - // Graceful fallback: captions disabled / blocked / no speech. Auto
270 - // reliably gets metadata; it does NOT guarantee a transcript.
271 - $transcript_missing = true;
272 -
273 - if ($video_title === '' && $video_channel === '') {
274 - // Both halves failed — nothing meaningful to index.
275 - set_transient('mxchat_admin_notice_error',
276 - esc_html__('Could not retrieve any information for that video (no metadata and no captions). Please check the URL, or use the manual description option.', 'mxchat'),
277 - 30
278 - );
279 - wp_safe_redirect(esc_url($redirect_url));
280 - exit;
281 - }
282 -
283 - $indexed_text = $header_lines . sprintf(
284 - /* translators: 1: video title, 2: channel name */
285 - __('A YouTube video titled "%1$s" from the channel %2$s.', 'mxchat'),
286 - $video_title !== '' ? $video_title : $canonical_url,
287 - $video_channel !== '' ? $video_channel : 'YouTube'
288 - );
289 - }
290 - }
291 -
292 - $result = MxChat_Utils::submit_content_to_db($indexed_text, $canonical_url, $api_key, null, $bot_id, 'youtube');
293 -
294 - if (is_wp_error($result)) {
295 - set_transient('mxchat_admin_notice_error',
296 - esc_html__('Error storing video in the knowledge base: ', 'mxchat') . $result->get_error_message(),
297 - 30
298 - );
299 - wp_safe_redirect(esc_url($redirect_url));
300 - exit;
301 - }
302 -
303 - if ($transcript_missing) {
304 - set_transient('mxchat_admin_notice_success',
305 - esc_html__('Video indexed from its title and channel — no captions were available for a transcript. The form below is pre-filled: write your own description and import again to improve matching (it updates the same entry).', 'mxchat'),
306 - 30
307 - );
308 - // Bounce back with prefill args so the page reopens the YouTube form in
309 - // manual mode with the URL + fetched title ready to augment.
310 - $redirect_url = add_query_arg(array(
311 - 'mxchat_yt_prefill' => '1',
312 - 'yt_url' => rawurlencode($canonical_url),
313 - 'yt_title' => rawurlencode($video_title),
314 - ), $redirect_url);
315 - } else {
316 - set_transient('mxchat_admin_notice_success',
317 - esc_html__('YouTube video successfully added to the knowledge base!', 'mxchat'),
318 - 30
319 - );
320 - }
321 -
322 - wp_safe_redirect(esc_url_raw($redirect_url));
323 - exit;
324 -}
325 -
326 -/**
327 - * Fetch YouTube oEmbed metadata for a video (no API key required).
328 - * Returns the decoded array (title, author_name, thumbnail_url, ...) or array().
329 - */
330 -private function mxchat_fetch_youtube_oembed($video_id) {
331 - $oembed_url = 'https://www.youtube.com/oembed?url=' . rawurlencode('https://www.youtube.com/watch?v=' . $video_id) . '&format=json';
332 - $response = wp_remote_get($oembed_url, array('timeout' => 15));
333 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
334 - return array();
335 - }
336 - $data = json_decode(wp_remote_retrieve_body($response), true);
337 - return is_array($data) ? $data : array();
338 -}
339 -
340 -/**
341 - * Best-effort captions transcript for a video. Deliberately ISOLATED: this uses
342 - * YouTube's unofficial timedtext route (the caption track list embedded in the
343 - * watch page), which YouTube has broken before and will break again. Every
344 - * failure mode returns '' so a break degrades to the metadata-only import path
345 - * instead of erroring the whole submission. Do not let anything in here throw.
346 - */
347 -private function mxchat_fetch_youtube_transcript($video_id) {
348 - $watch_url = 'https://www.youtube.com/watch?v=' . $video_id . '&hl=en';
349 -
350 - // First try the honest ingest UA; some responses omit the player config for
351 - // bot UAs, so retry once with a browser UA before giving up.
352 - $user_agents = array(
353 - mxchat_ingest_user_agent(),
354 - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
355 - );
356 -
357 - $tracks = array();
358 - foreach ($user_agents as $ua) {
359 - $response = wp_remote_get($watch_url, array(
360 - 'timeout' => 20,
361 - 'user-agent' => $ua,
362 - ));
363 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
364 - continue;
365 - }
366 - $body = wp_remote_retrieve_body($response);
367 - if (!is_string($body) || $body === '' || !preg_match('/"captionTracks":(\[.*?\])(?=,")/s', $body, $m)) {
368 - continue;
369 - }
370 - $decoded = json_decode($m[1], true);
371 - if (is_array($decoded) && !empty($decoded)) {
372 - $tracks = $decoded;
373 - break;
374 - }
375 - }
376 -
377 - if (empty($tracks)) {
378 - return '';
379 - }
380 -
381 - // Prefer an English track, else take the first offered.
382 - $chosen = null;
383 - foreach ($tracks as $track) {
384 - if (isset($track['languageCode']) && strpos($track['languageCode'], 'en') === 0) {
385 - $chosen = $track;
386 - break;
387 - }
388 - }
389 - if ($chosen === null) {
390 - $chosen = $tracks[0];
391 - }
392 - if (empty($chosen['baseUrl']) || !is_string($chosen['baseUrl'])) {
393 - return '';
394 - }
395 -
396 - $timedtext = wp_remote_get($chosen['baseUrl'], array('timeout' => 20));
397 - if (is_wp_error($timedtext) || wp_remote_retrieve_response_code($timedtext) !== 200) {
398 - return '';
399 - }
400 - $xml = wp_remote_retrieve_body($timedtext);
401 - if (!is_string($xml) || strpos($xml, '<text') === false) {
402 - return '';
403 - }
404 -
405 - // <text start=".." dur="..">caption</text> — strip tags, decode the
406 - // double-encoded entities timedtext ships, collapse whitespace.
407 - $text = preg_replace('/<[^>]+>/', ' ', $xml);
408 - $text = html_entity_decode(html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'), ENT_QUOTES | ENT_HTML5, 'UTF-8');
409 - $text = trim(preg_replace('/\s+/u', ' ', $text));
410 -
411 - return $text;
412 -}
413 -
414 -public function mxchat_is_pdf_url($url, $response) {
415 - $content_type = wp_remote_retrieve_header($response, 'content-type');
416 - $file_extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
417 -
418 - // Check Content-Disposition header for .pdf filename (Google Drive sends this)
419 - $disposition = wp_remote_retrieve_header($response, 'content-disposition');
420 - $has_pdf_disposition = ! empty($disposition) && stripos($disposition, '.pdf') !== false;
421 -
422 - return strpos($content_type, 'pdf') !== false || $file_extension === 'pdf' || $has_pdf_disposition;
423 -}
424 -
425 -
426 -public function mxchat_handle_pdf_for_knowledge_base($pdf_url, $response, $bot_id = 'default') {
427 - if (!current_user_can('manage_options')) {
428 - return false;
429 - }
430 -
431 - $pdf_url = esc_url_raw($pdf_url);
432 - $upload_dir = wp_upload_dir();
433 -
434 - if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
435 - return false;
436 - }
437 -
438 - $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
439 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
440 -
441 - $response_body = wp_remote_retrieve_body($response);
442 - if (empty($response_body)) {
443 - return false;
444 - }
445 -
446 - if (!wp_mkdir_p(dirname($pdf_path))) {
447 - return false;
448 - }
449 -
450 - try {
451 - file_put_contents($pdf_path, $response_body);
452 -
453 - if (!file_exists($pdf_path)) {
454 - throw new Exception(__('Failed to save PDF file', 'mxchat'));
455 - }
456 -
457 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
458 -
459 - if ($total_pages === false || $total_pages < 1) {
460 - throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
461 - }
462 -
463 - // Create unique queue ID
464 - $queue_id = 'pdf_' . md5($pdf_url . time());
465 -
466 - // Create array of pages to process
467 - $pages = array();
468 - for ($i = 1; $i <= $total_pages; $i++) {
469 - $pages[] = array(
470 - 'pdf_path' => $pdf_path,
471 - 'pdf_url' => $pdf_url,
472 - 'page_number' => $i,
473 - 'total_pages' => $total_pages
474 - );
475 - }
476 -
477 - // Add pages to queue
478 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
479 -
480 - if ($queued_count === 0) {
481 - wp_delete_file($pdf_path);
482 - throw new Exception(__('Failed to add PDF pages to processing queue', 'mxchat'));
483 - }
484 -
485 - // Store queue metadata
486 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $pdf_url);
487 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'pdf');
488 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_pages);
489 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
490 - $this->mxchat_set_queue_meta($queue_id, 'pdf_path', $pdf_path);
491 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
492 -
493 - // Store queue ID in transient for status tracking
494 - set_transient('mxchat_active_queue_pdf', $queue_id, DAY_IN_SECONDS);
495 - set_transient('mxchat_last_pdf_url', $pdf_url, DAY_IN_SECONDS);
496 -
497 - return 'queued';
498 -
499 - } catch (Exception $e) {
500 - if (file_exists($pdf_path)) {
501 - wp_delete_file($pdf_path);
502 - }
503 - return $e->getMessage();
504 - }
505 -}
506 -
507 -/**
508 - * Handle direct PDF file upload from the knowledge base page
509 - */
510 -public function mxchat_handle_pdf_file_submission() {
511 - if (!isset($_POST['submit_pdf_file']) || !current_user_can('manage_options')) {
512 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
513 - }
514 -
515 - check_admin_referer('mxchat_submit_pdf_file_action', 'mxchat_submit_pdf_file_nonce');
516 -
517 - $redirect_url = admin_url('admin.php?page=mxchat-prompts');
518 -
519 - // Validate file upload
520 - if (empty($_FILES['pdf_file']) || $_FILES['pdf_file']['error'] !== UPLOAD_ERR_OK) {
521 - $error_code = isset($_FILES['pdf_file']['error']) ? $_FILES['pdf_file']['error'] : UPLOAD_ERR_NO_FILE;
522 - $error_messages = array(
523 - UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the server upload_max_filesize limit.', 'mxchat'),
524 - UPLOAD_ERR_FORM_SIZE => __('The uploaded file exceeds the form MAX_FILE_SIZE limit.', 'mxchat'),
525 - UPLOAD_ERR_PARTIAL => __('The file was only partially uploaded.', 'mxchat'),
526 - UPLOAD_ERR_NO_FILE => __('No file was uploaded. Please select a PDF file.', 'mxchat'),
527 - UPLOAD_ERR_NO_TMP_DIR => __('Server missing temporary folder.', 'mxchat'),
528 - UPLOAD_ERR_CANT_WRITE => __('Server failed to write file to disk.', 'mxchat'),
529 - );
530 - $error_msg = isset($error_messages[$error_code]) ? $error_messages[$error_code] : __('Unknown upload error.', 'mxchat');
531 - set_transient('mxchat_admin_notice_error', $error_msg, 30);
532 - wp_safe_redirect(esc_url($redirect_url));
533 - exit;
534 - }
535 -
536 - $file = $_FILES['pdf_file'];
537 -
538 - // Validate MIME type
539 - $finfo = finfo_open(FILEINFO_MIME_TYPE);
540 - $mime_type = finfo_file($finfo, $file['tmp_name']);
541 - finfo_close($finfo);
542 -
543 - if ($mime_type !== 'application/pdf') {
544 - set_transient('mxchat_admin_notice_error',
545 - esc_html__('Invalid file type. Only PDF files are accepted.', 'mxchat'),
546 - 30
547 - );
548 - wp_safe_redirect(esc_url($redirect_url));
549 - exit;
550 - }
551 -
552 - // Validate extension
553 - $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
554 - if ($ext !== 'pdf') {
555 - set_transient('mxchat_admin_notice_error',
556 - esc_html__('Invalid file extension. Only .pdf files are accepted.', 'mxchat'),
557 - 30
558 - );
559 - wp_safe_redirect(esc_url($redirect_url));
560 - exit;
561 - }
562 -
563 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
564 - $original_filename = sanitize_file_name($file['name']);
565 -
566 - $upload_dir = wp_upload_dir();
567 - if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
568 - set_transient('mxchat_admin_notice_error',
569 - esc_html__('WordPress upload directory is not writable.', 'mxchat'),
570 - 30
571 - );
572 - wp_safe_redirect(esc_url($redirect_url));
573 - exit;
574 - }
575 -
576 - $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
577 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
578 -
579 - if (!wp_mkdir_p(dirname($pdf_path))) {
580 - set_transient('mxchat_admin_notice_error',
581 - esc_html__('Failed to create upload directory.', 'mxchat'),
582 - 30
583 - );
584 - wp_safe_redirect(esc_url($redirect_url));
585 - exit;
586 - }
587 -
588 - // Move uploaded file
589 - if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
590 - set_transient('mxchat_admin_notice_error',
591 - esc_html__('Failed to save uploaded PDF file.', 'mxchat'),
592 - 30
593 - );
594 - wp_safe_redirect(esc_url($redirect_url));
595 - exit;
596 - }
597 -
598 - try {
599 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
600 -
601 - if ($total_pages === false || $total_pages < 1) {
602 - throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
603 - }
604 -
605 - // Use original filename as the source identifier
606 - $source_label = 'upload://' . $original_filename;
607 -
608 - $queue_id = 'pdf_' . md5($source_label . time());
609 -
610 - $pages = array();
611 - for ($i = 1; $i <= $total_pages; $i++) {
612 - $pages[] = array(
613 - 'pdf_path' => $pdf_path,
614 - 'pdf_url' => $source_label,
615 - 'page_number' => $i,
616 - 'total_pages' => $total_pages,
617 - );
618 - }
619 -
620 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
621 -
622 - if ($queued_count === 0) {
623 - wp_delete_file($pdf_path);
624 - throw new Exception(__('Failed to add PDF pages to processing queue', 'mxchat'));
625 - }
626 -
627 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $source_label);
628 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'pdf');
629 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_pages);
630 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
631 - $this->mxchat_set_queue_meta($queue_id, 'pdf_path', $pdf_path);
632 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
633 -
634 - set_transient('mxchat_active_queue_pdf', $queue_id, DAY_IN_SECONDS);
635 - set_transient('mxchat_last_pdf_url', $source_label, DAY_IN_SECONDS);
636 -
637 - set_transient('mxchat_admin_notice_success',
638 - sprintf(
639 - esc_html__('PDF "%s" (%d pages) queued for processing. Processing will start automatically.', 'mxchat'),
640 - esc_html($original_filename),
641 - $total_pages
642 - ),
643 - 30
644 - );
645 -
646 - } catch (Exception $e) {
647 - if (file_exists($pdf_path)) {
648 - wp_delete_file($pdf_path);
649 - }
650 - set_transient('mxchat_admin_notice_error',
651 - esc_html__('Failed to process uploaded PDF: ', 'mxchat') . esc_html($e->getMessage()),
652 - 30
653 - );
654 - }
655 -
656 - wp_safe_redirect(esc_url($redirect_url));
657 - exit;
658 -}
659 -
660 -/**
661 - * Handle direct document upload (.docx / .txt / .md) from the knowledge base
662 - * page (plan 0485e5). Unlike PDF Upload there is no per-page queue: the text
663 - * extracts in one pass and routes through submit_content_to_db, whose chunker
664 - * takes over for long content. The uploaded file is read from the PHP temp
665 - * file and never persisted — only its extracted text enters the KB.
666 - *
667 - * Source identity matches PDF Upload's scheme: upload://<filename>, stable
668 - * across re-uploads so a re-import REPLACES (delete_chunks_for_url + upsert
669 - * per identity) instead of duplicating.
670 - */
671 -public function mxchat_handle_document_file_submission() {
672 - if (!isset($_POST['submit_document_file']) || !current_user_can('manage_options')) {
673 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
674 - }
675 -
676 - check_admin_referer('mxchat_submit_document_file_action', 'mxchat_submit_document_file_nonce');
677 -
678 - $redirect_url = admin_url('admin.php?page=mxchat-prompts');
679 -
680 - if (empty($_FILES['document_file']) || $_FILES['document_file']['error'] !== UPLOAD_ERR_OK) {
681 - $error_code = isset($_FILES['document_file']['error']) ? $_FILES['document_file']['error'] : UPLOAD_ERR_NO_FILE;
682 - $error_messages = array(
683 - UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the server upload_max_filesize limit.', 'mxchat'),
684 - UPLOAD_ERR_FORM_SIZE => __('The uploaded file exceeds the form MAX_FILE_SIZE limit.', 'mxchat'),
685 - UPLOAD_ERR_PARTIAL => __('The file was only partially uploaded.', 'mxchat'),
686 - UPLOAD_ERR_NO_FILE => __('No file was uploaded. Please select a document.', 'mxchat'),
687 - UPLOAD_ERR_NO_TMP_DIR => __('Server missing temporary folder.', 'mxchat'),
688 - UPLOAD_ERR_CANT_WRITE => __('Server failed to write file to disk.', 'mxchat'),
689 - );
690 - $error_msg = isset($error_messages[$error_code]) ? $error_messages[$error_code] : __('Unknown upload error.', 'mxchat');
691 - set_transient('mxchat_admin_notice_error', $error_msg, 30);
692 - wp_safe_redirect(esc_url($redirect_url));
693 - exit;
694 - }
695 -
696 - $file = $_FILES['document_file'];
697 - $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
698 -
699 - $finfo = finfo_open(FILEINFO_MIME_TYPE);
700 - $mime_type = finfo_file($finfo, $file['tmp_name']);
701 - finfo_close($finfo);
702 -
703 - // Per-extension MIME expectations. finfo commonly reports .docx as
704 - // application/zip (it IS a Zip container) and .md as plain text.
705 - $mime_ok = false;
706 - if ($ext === 'docx') {
707 - $mime_ok = in_array($mime_type, array(
708 - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
709 - 'application/zip',
710 - ), true);
711 - } elseif ($ext === 'txt' || $ext === 'md') {
712 - $mime_ok = (strpos((string) $mime_type, 'text/') === 0);
713 - }
714 -
715 - if (!$mime_ok) {
716 - set_transient('mxchat_admin_notice_error',
717 - esc_html__('Invalid or unreadable document. Accepted types: .docx, .txt, .md.', 'mxchat'),
718 - 30
719 - );
720 - wp_safe_redirect(esc_url($redirect_url));
721 - exit;
722 - }
723 -
724 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
725 - $original_filename = sanitize_file_name($file['name']);
726 -
727 - // ---- Extract text (ONE extractor for .docx — the word handler's) ----
728 - if ($ext === 'docx') {
729 - $text = MXChat_Word_Handler::extract_docx_text($file['tmp_name']);
730 - if ($text === false) {
731 - set_transient('mxchat_admin_notice_error',
732 - esc_html__('The .docx file could not be read. It may be corrupt, empty, or not a real Word document.', 'mxchat'),
733 - 30
734 - );
735 - wp_safe_redirect(esc_url($redirect_url));
736 - exit;
737 - }
738 - } else {
739 - // .txt / .md read as-is. Markdown keeps its syntax on purpose —
740 - // headings are useful retrieval signal.
741 - $text = (string) file_get_contents($file['tmp_name']);
742 - $text = wp_check_invalid_utf8($text);
743 - $text = trim($text);
744 - }
745 -
746 - if ($text === '') {
747 - set_transient('mxchat_admin_notice_error',
748 - esc_html__('The uploaded document contains no readable text.', 'mxchat'),
749 - 30
750 - );
751 - wp_safe_redirect(esc_url($redirect_url));
752 - exit;
753 - }
754 -
755 - // Size cap — same pdf_max_pages setting the PDF/toolbar paths use, but
756 - // estimated by CHARACTERS (~2500/page): the .docx cleaner collapses all
757 - // newlines to spaces, so a paragraph count reads 1 for any Word file.
758 - // Processing is synchronous — an unbounded document risks a timeout.
759 - $options = get_option('mxchat_options', array());
760 - $max_pages = isset($options['pdf_max_pages']) ? intval($options['pdf_max_pages']) : 69;
761 - $estimated_pages = (int) ceil(strlen($text) / 2500);
762 - if ($estimated_pages > $max_pages) {
763 - set_transient('mxchat_admin_notice_error',
764 - sprintf(
765 - esc_html__('The document is too large (about %1$d pages; the limit is %2$d). Split it into smaller files, or raise the PDF max pages setting.', 'mxchat'),
766 - $estimated_pages,
767 - $max_pages
768 - ),
769 - 30
770 - );
771 - wp_safe_redirect(esc_url($redirect_url));
772 - exit;
773 - }
774 -
775 - // Embedding API key — bot-aware, same shape as the direct-content handler.
776 - $api_key = '';
777 - if ($bot_id !== 'default' && class_exists('MxChat_Multi_Bot_Manager')) {
778 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
779 - $api_key = $bot_options['api_key'] ?? '';
780 - }
781 - if (empty($api_key)) {
782 - $api_key = $options['api_key'] ?? '';
783 - }
784 -
785 - // Stable identity — PDF Upload's scheme. A re-upload of the same filename
786 - // replaces: clear old chunks first (covers a doc shrinking below the chunk
787 - // threshold, where the single-vector path would not clean them), then
788 - // submit — the chunked path re-deletes harmlessly.
789 - $source_label = 'upload://' . $original_filename;
790 - MxChat_Utils::delete_chunks_for_url($source_label, $bot_id);
791 - $result = MxChat_Utils::submit_content_to_db($text, $source_label, $api_key, null, $bot_id, 'document');
792 -
793 - if (is_wp_error($result)) {
794 - set_transient('mxchat_admin_notice_error',
795 - esc_html__('Failed to import the document: ', 'mxchat') . esc_html($result->get_error_message()),
796 - 30
797 - );
798 - } else {
799 - set_transient('mxchat_admin_notice_success',
800 - sprintf(
801 - esc_html__('Document "%s" imported into the knowledge base.', 'mxchat'),
802 - esc_html($original_filename)
803 - ),
804 - 30
805 - );
806 - }
807 -
808 - wp_safe_redirect(esc_url($redirect_url));
809 - exit;
810 -}
811 -
812 -/**
813 - * Validate PDF and count pages with multiple parser attempts
814 - */
815 -private function mxchat_validate_and_count_pdf_pages($pdf_path) {
816 - // Method 1: Try with Smalot PDF Parser (your current method)
817 - try {
818 - mxchat_load_pdf_parser();
819 - $parser = new \Smalot\PdfParser\Parser();
820 - $pdf = $parser->parseFile($pdf_path);
821 - $pages = $pdf->getPages();
822 - $page_count = count($pages);
823 -
824 - if ($page_count > 0) {
825 - //error_log('PDF parsed successfully with Smalot parser: ' . $page_count . ' pages');
826 - return $page_count;
827 - }
828 - } catch (Exception $e) {
829 - //error_log('Smalot PDF parser failed: ' . $e->getMessage());
830 - }
831 -
832 - // Method 2: Try with pdfinfo command (if available)
833 - if (function_exists('shell_exec') && !$this->mxchat_is_shell_disabled()) {
834 - try {
835 - $command = 'pdfinfo ' . escapeshellarg($pdf_path) . ' 2>&1';
836 - $output = shell_exec($command);
837 -
838 - if ($output && preg_match('/Pages:\s*(\d+)/', $output, $matches)) {
839 - $page_count = intval($matches[1]);
840 - if ($page_count > 0) {
841 - //error_log('PDF parsed successfully with pdfinfo: ' . $page_count . ' pages');
842 - return $page_count;
843 - }
844 - }
845 - } catch (Exception $e) {
846 - //error_log('pdfinfo command failed: ' . $e->getMessage());
847 - }
848 - }
849 -
850 - // Method 3: Try to repair PDF and parse again
851 - try {
852 - $repaired_path = $this->mxchat_attempt_pdf_repair($pdf_path);
853 - if ($repaired_path && $repaired_path !== $pdf_path) {
854 - mxchat_load_pdf_parser();
855 - $parser = new \Smalot\PdfParser\Parser();
856 - $pdf = $parser->parseFile($repaired_path);
857 - $pages = $pdf->getPages();
858 - $page_count = count($pages);
859 -
860 - if ($page_count > 0) {
861 - // Replace original with repaired version
862 - copy($repaired_path, $pdf_path);
863 - unlink($repaired_path);
864 - //error_log('PDF repaired and parsed successfully: ' . $page_count . ' pages');
865 - return $page_count;
866 - }
867 -
868 - // Clean up repaired file if it didn't work
869 - unlink($repaired_path);
870 - }
871 - } catch (Exception $e) {
872 - //error_log('PDF repair attempt failed: ' . $e->getMessage());
873 - }
874 -
875 - // Method 4: Manual PDF structure analysis (basic page count)
876 - try {
877 - $page_count = $this->mxchat_manual_pdf_page_count($pdf_path);
878 - if ($page_count > 0) {
879 - //error_log('PDF page count determined manually: ' . $page_count . ' pages');
880 - return $page_count;
881 - }
882 - } catch (Exception $e) {
883 - //error_log('Manual PDF analysis failed: ' . $e->getMessage());
884 - }
885 -
886 - //error_log('All PDF parsing methods failed for: ' . $pdf_path);
887 - return false;
888 -}
889 -
890 -/**
891 - * Check if shell_exec is disabled
892 - */
893 -private function mxchat_is_shell_disabled() {
894 - $disabled = explode(',', ini_get('disable_functions'));
895 - return in_array('shell_exec', $disabled);
896 -}
897 -
898 -/**
899 - * Attempt to repair PDF using basic methods
900 - */
901 -private function mxchat_attempt_pdf_repair($pdf_path) {
902 - try {
903 - $content = file_get_contents($pdf_path);
904 - if (!$content) {
905 - return false;
906 - }
907 -
908 - // Check if PDF starts with proper header
909 - if (substr($content, 0, 4) !== '%PDF') {
910 - // Try to find PDF header in the content
911 - $header_pos = strpos($content, '%PDF');
912 - if ($header_pos !== false && $header_pos < 1024) {
913 - // Remove junk before PDF header
914 - $content = substr($content, $header_pos);
915 - $repaired_path = $pdf_path . '.repaired';
916 - file_put_contents($repaired_path, $content);
917 - return $repaired_path;
918 - }
919 - }
920 -
921 - // Check for EOF marker
922 - $content = rtrim($content);
923 - if (!preg_match('/%%EOF\s*$/', $content)) {
924 - // Add EOF marker if missing
925 - $content .= "\n%%EOF";
926 - $repaired_path = $pdf_path . '.repaired';
927 - file_put_contents($repaired_path, $content);
928 - return $repaired_path;
929 - }
930 -
931 - } catch (Exception $e) {
932 - //error_log('PDF repair error: ' . $e->getMessage());
933 - }
934 -
935 - return false;
936 -}
937 -
938 -/**
939 - * Manual PDF page counting by analyzing PDF structure
940 - */
941 -private function mxchat_manual_pdf_page_count($pdf_path) {
942 - try {
943 - $content = file_get_contents($pdf_path);
944 - if (!$content) {
945 - return 0;
946 - }
947 -
948 - // Method 1: Count /Type /Page objects
949 - $page_count = preg_match_all('/\/Type\s*\/Page[^s]/', $content);
950 - if ($page_count > 0) {
951 - return $page_count;
952 - }
953 -
954 - // Method 2: Look for /Count in pages object
955 - if (preg_match('/\/Type\s*\/Pages.*?\/Count\s+(\d+)/', $content, $matches)) {
956 - return intval($matches[1]);
957 - }
958 -
959 - // Method 3: Count page references
960 - $page_count = preg_match_all('/\d+\s+0\s+obj\s*<<[^>]*\/Type\s*\/Page/', $content);
961 - if ($page_count > 0) {
962 - return $page_count;
963 - }
964 -
965 - } catch (Exception $e) {
966 - //error_log('Manual PDF analysis error: ' . $e->getMessage());
967 - }
968 -
969 - return 0;
970 -}
971 -
972 -
973 -public function mxchat_save_inline_prompt() {
974 - // DEBUG: Log what we're receiving
975 - //error_log('=== MXCHAT DEBUG ===');
976 - //error_log('POST data: ' . print_r($_POST, true));
977 - //error_log('Nonce from POST: ' . ($_POST['_ajax_nonce'] ?? 'NOT FOUND'));
978 -
979 - // Check for nonce security
980 - check_ajax_referer('mxchat_save_inline_nonce', '_ajax_nonce');
981 -
982 - // If we get here, nonce passed
983 - //error_log('Nonce verification PASSED');
984 -
985 - // Verify permissions
986 - if (!current_user_can('manage_options')) {
987 - wp_send_json_error(esc_html__('Permission denied.', 'mxchat'));
988 - return;
989 - }
990 -
991 - global $wpdb;
992 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
993 -
994 - // Validate and sanitize input data
995 - $prompt_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
996 - $article_content = isset($_POST['article_content']) ? wp_kses_post(wp_unslash($_POST['article_content'])) : '';
997 - $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
998 -
999 - if ($prompt_id > 0 && !empty($article_content)) {
1000 - // Re-generate the embedding vector for the updated content
1001 - $embedding_vector = $this->mxchat_generate_embedding($article_content);
1002 - if (is_array($embedding_vector)) {
1003 - // Serialize the embedding vector before storing it
1004 - $embedding_vector_serialized = serialize($embedding_vector);
1005 - // Update the prompt in the database
1006 - $updated = $wpdb->update(
1007 - $table_name,
1008 - array(
1009 - 'article_content' => $article_content,
1010 - 'embedding_vector' => $embedding_vector_serialized,
1011 - 'source_url' => $article_url,
1012 - ),
1013 - array('id' => $prompt_id),
1014 - array('%s', '%s', '%s'),
1015 - array('%d')
1016 - );
1017 - if ($updated !== false) {
1018 - wp_send_json_success();
1019 - } else {
1020 - MxChat_Admin::mxchat_log_debug('knowledge_error', 'Database update failed when saving knowledge entry');
1021 - wp_send_json_error(esc_html__('Database update failed.', 'mxchat'));
1022 - }
1023 - } else {
1024 - MxChat_Admin::mxchat_log_debug('embedding_error', 'Embedding generation failed for knowledge entry');
1025 - wp_send_json_error(esc_html__('Embedding generation failed.', 'mxchat'));
1026 - }
1027 - } else {
1028 - wp_send_json_error(esc_html__('Invalid data.', 'mxchat'));
1029 - }
1030 -}
1031 -
1032 -
1033 -/**
1034 - * AJAX: Get full content for editing — reassembles chunks if needed.
1035 - * Works for both WordPress DB and Pinecone entries.
1036 - */
1037 -/**
1038 - * Sanitize a knowledge entry's source_url from an AJAX request WITHOUT destroying
1039 - * its identity. sanitize_text_field() strips percent-encoded octets (%20, %D7%A9…),
1040 - * so a percent-encoded URL — every non-ASCII permalink — would md5 to a DIFFERENT
1041 - * id than the one it was stored under: reads miss the entry and saves write an
1042 - * orphan copy while the original keeps its stale text. URLs get esc_url_raw
1043 - * (identity-preserving, matches what import stored); non-URL keys (mxchat://,
1044 - * _ungrouped_) keep the old sanitizer.
1045 - */
1046 -private function sanitize_entry_source_url( $raw ) {
1047 - $raw = trim( (string) $raw );
1048 - if ( preg_match( '#^https?://#i', $raw ) ) {
1049 - return esc_url_raw( $raw );
1050 - }
1051 - return sanitize_text_field( $raw );
1052 -}
1053 -
1054 -public function ajax_mxchat_get_entry_content() {
1055 - check_ajax_referer('mxchat_edit_entry_nonce', 'nonce');
1056 -
1057 - if ( ! current_user_can('manage_options') ) {
1058 - wp_send_json_error( array( 'message' => 'Permission denied.' ) );
1059 - }
1060 -
1061 - $source_url = $this->sanitize_entry_source_url( isset($_POST['source_url']) ? wp_unslash($_POST['source_url']) : '' );
1062 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
1063 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
1064 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1065 -
1066 - if ( $data_source === 'pinecone' ) {
1067 - // Pinecone ids are strings (md5 hashes, manual_* ids) — absint() would
1068 - // destroy them, so re-read the raw value for this branch only.
1069 - $vector_id = isset($_POST['entry_id']) ? sanitize_text_field( wp_unslash($_POST['entry_id']) ) : '';
1070 - // Pinecone: fetch vectors by source_url, reassemble chunks
1071 - $content = $this->get_pinecone_entry_content( $source_url, $vector_id, $bot_id );
1072 - } else {
1073 - // WordPress DB
1074 - $content = $this->get_wordpress_entry_content( $source_url, $entry_id );
1075 - }
1076 -
1077 - if ( is_wp_error( $content ) ) {
1078 - wp_send_json_error( array( 'message' => $content->get_error_message() ) );
1079 - }
1080 -
1081 - wp_send_json_success( $content );
1082 -}
1083 -
1084 -/**
1085 - * Get content from WordPress DB — reassembles chunks by source_url.
1086 - */
1087 -private function get_wordpress_entry_content( $source_url, $entry_id ) {
1088 - global $wpdb;
1089 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
1090 -
1091 - // If we have a source_url, check for chunks
1092 - if ( ! empty( $source_url ) && strpos( $source_url, 'mxchat://' ) !== 0 ) {
1093 - $rows = $wpdb->get_results( $wpdb->prepare(
1094 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE source_url = %s ORDER BY id ASC",
1095 - $source_url
1096 - ) );
1097 -
1098 - if ( $rows && count( $rows ) > 1 ) {
1099 - // Multiple rows = chunked. Reassemble.
1100 - $chunks = array();
1101 - foreach ( $rows as $row ) {
1102 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
1103 - $index = isset( $parsed['metadata']['chunk_index'] ) ? intval( $parsed['metadata']['chunk_index'] ) : count( $chunks );
1104 - $chunks[ $index ] = $parsed['text'];
1105 - }
1106 - ksort( $chunks );
1107 - return array(
1108 - 'content' => implode( "\n\n", $chunks ),
1109 - 'source_url' => $source_url,
1110 - 'is_chunked' => true,
1111 - 'chunk_count' => count( $chunks ),
1112 - 'content_type' => $rows[0]->content_type,
1113 - );
1114 - } elseif ( $rows && count( $rows ) === 1 ) {
1115 - $parsed = MxChat_Chunker::parse_stored_chunk( $rows[0]->article_content );
1116 - return array(
1117 - 'content' => $parsed['text'],
1118 - 'source_url' => $source_url,
1119 - 'entry_id' => $rows[0]->id,
1120 - 'is_chunked' => false,
1121 - 'content_type' => $rows[0]->content_type,
1122 - );
1123 - }
1124 - }
1125 -
1126 - // Fallback: fetch by ID
1127 - if ( $entry_id > 0 ) {
1128 - $row = $wpdb->get_row( $wpdb->prepare(
1129 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE id = %d",
1130 - $entry_id
1131 - ) );
1132 - if ( $row ) {
1133 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
1134 - return array(
1135 - 'content' => $parsed['text'],
1136 - 'source_url' => $row->source_url,
1137 - 'entry_id' => $row->id,
1138 - 'is_chunked' => false,
1139 - 'content_type' => $row->content_type,
1140 - );
1141 - }
1142 - }
1143 -
1144 - return new WP_Error( 'not_found', 'Entry not found.' );
1145 -}
1146 -
1147 -/**
1148 - * Get content from Pinecone — fetches vectors by source_url, reassembles chunks.
1149 - */
1150 -private function get_pinecone_entry_content( $source_url, $entry_id, $bot_id ) {
1151 - if ( ! class_exists('MxChat_Pinecone_Manager') ) {
1152 - return new WP_Error( 'pinecone_unavailable', 'Pinecone manager not available.' );
1153 - }
1154 -
1155 - // Get Pinecone config
1156 - if ( $bot_id === 'default' || ! class_exists('MxChat_Multi_Bot_Manager') ) {
1157 - $pinecone_options = get_option('mxchat_pinecone_addon_options');
1158 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
1159 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1160 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
1161 - } else {
1162 - $bot_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
1163 - $api_key = $bot_config['api_key'] ?? '';
1164 - $host = $bot_config['host'] ?? '';
1165 - $namespace = $bot_config['namespace'] ?? '';
1166 - }
1167 -
1168 - if ( empty($host) || empty($api_key) ) {
1169 - return new WP_Error( 'pinecone_config', 'Pinecone not configured.' );
1170 - }
1171 -
1172 - // Manual entries carry no source_url (their vector id is a minted manual_* string,
1173 - // not md5 of anything the row can hand us) — fetch the exact vector instead.
1174 - // '_ungrouped_' is the table view's synthetic display key for such rows.
1175 - if ( ( empty($source_url) || strpos($source_url, '_ungrouped_') === 0 ) && ! empty($entry_id) && is_string($entry_id) ) {
1176 - $vector_ids = array( $entry_id );
1177 - } else {
1178 - // List vectors with the source_url prefix
1179 - $base_id = md5( $source_url );
1180 - $vector_ids = array( $base_id );
1181 -
1182 - // Find chunk vectors
1183 - // NOTE: Pinecone's /vectors/list is a GET endpoint with query parameters.
1184 - // A POST is answered 200-with-an-empty-body, which reads as "no vectors".
1185 - $list_url = "https://{$host}/vectors/list";
1186 - $list_params = array( 'prefix' => $base_id . '_chunk_', 'limit' => 100 );
1187 - if ( ! empty($namespace) ) {
1188 - $list_params['namespace'] = $namespace;
1189 - }
1190 -
1191 - $list_resp = wp_remote_get( $list_url . '?' . http_build_query( $list_params ), array(
1192 - 'headers' => array( 'Api-Key' => $api_key, 'accept' => 'application/json' ),
1193 - 'timeout' => 15,
1194 - ) );
1195 -
1196 - if ( ! is_wp_error($list_resp) ) {
1197 - $list_data = json_decode( wp_remote_retrieve_body($list_resp), true );
1198 - if ( ! empty($list_data['vectors']) ) {
1199 - foreach ( $list_data['vectors'] as $v ) {
1200 - $vector_ids[] = $v['id'];
1201 - }
1202 - }
1203 - }
1204 - }
1205 -
1206 - // Fetch vectors with metadata
1207 - // NOTE: /vectors/fetch is a GET endpoint too, and Pinecone expects the ids
1208 - // repeated (ids=a&ids=b) — http_build_query would emit ids[0]=a, so build
1209 - // the query string explicitly.
1210 - $fetch_query = array();
1211 - foreach ( $vector_ids as $fetch_vid ) {
1212 - $fetch_query[] = 'ids=' . rawurlencode( $fetch_vid );
1213 - }
1214 - if ( ! empty($namespace) ) {
1215 - $fetch_query[] = 'namespace=' . rawurlencode( $namespace );
1216 - }
1217 -
1218 - $fetch_resp = wp_remote_get( "https://{$host}/vectors/fetch?" . implode( '&', $fetch_query ), array(
1219 - 'headers' => array( 'Api-Key' => $api_key, 'accept' => 'application/json' ),
1220 - 'timeout' => 15,
1221 - ) );
1222 -
1223 - if ( is_wp_error($fetch_resp) ) {
1224 - return new WP_Error( 'pinecone_fetch', 'Failed to fetch from Pinecone.' );
1225 - }
1226 -
1227 - $fetch_data = json_decode( wp_remote_retrieve_body($fetch_resp), true );
1228 - $vectors = $fetch_data['vectors'] ?? array();
1229 -
1230 - if ( empty($vectors) ) {
1231 - return new WP_Error( 'not_found', 'Entry not found in Pinecone.' );
1232 - }
1233 -
1234 - // Reassemble chunks
1235 - $chunks = array();
1236 - $content_type = 'content';
1237 - foreach ( $vectors as $vid => $vector ) {
1238 - $meta = $vector['metadata'] ?? array();
1239 - $text = $meta['text'] ?? '';
1240 - $index = $meta['chunk_index'] ?? 0;
1241 - $content_type = $meta['type'] ?? 'content';
1242 - $chunks[ intval($index) ] = $text;
1243 - }
1244 - ksort( $chunks );
1245 -
1246 - return array(
1247 - 'content' => implode( "\n\n", $chunks ),
1248 - 'source_url' => $source_url,
1249 - 'is_chunked' => count($chunks) > 1,
1250 - 'chunk_count' => count($chunks),
1251 - 'content_type' => $content_type,
1252 - );
1253 -}
1254 -
1255 -/**
1256 - * AJAX: Inspect a knowledge entry — returns the per-chunk stored text + metadata
1257 - * WITHOUT collapsing it, so a site owner can see exactly what was indexed for an
1258 - * entry (plan-mxchat-20260628-d8cb4b). READ-ONLY: never re-embeds or mutates.
1259 - */
1260 -public function ajax_mxchat_inspect_entry() {
1261 - check_ajax_referer('mxchat_inspect_entry_nonce', 'nonce');
1262 -
1263 - if ( ! current_user_can('manage_options') ) {
1264 - wp_send_json_error( array( 'message' => esc_html__('Permission denied.', 'mxchat') ) );
1265 - }
1266 -
1267 - $source_url = isset($_POST['source_url']) ? sanitize_text_field( wp_unslash($_POST['source_url']) ) : '';
1268 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
1269 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
1270 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1271 -
1272 - if ( $data_source === 'pinecone' ) {
1273 - $result = $this->inspect_pinecone_entry( $source_url, $entry_id, $bot_id );
1274 - } else {
1275 - $result = $this->inspect_wordpress_entry( $source_url, $entry_id );
1276 - }
1277 -
1278 - if ( is_wp_error( $result ) ) {
1279 - wp_send_json_error( array( 'message' => $result->get_error_message() ) );
1280 - }
1281 -
1282 - wp_send_json_success( $result );
1283 -}
1284 -
1285 -/**
1286 - * Read-only inspector for WordPress-DB entries. Mirrors get_wordpress_entry_content()
1287 - * but returns each STORED chunk's exact text + length (no implode), plus the assembled
1288 - * embedded text. This shows what is actually in the index, not a re-derivation from the post.
1289 - */
1290 -private function inspect_wordpress_entry( $source_url, $entry_id ) {
1291 - global $wpdb;
1292 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
1293 -
1294 - $rows = array();
1295 -
1296 - // Group by the real stored source_url — this INCLUDES "mxchat://" manual
1297 - // Direct Content entries (the spec's manual-entry case), which share one
1298 - // source_url across their chunk rows. Only the synthetic "_ungrouped_<id>"
1299 - // display key (invented by the table view for rows with no source_url) is
1300 - // excluded; those fall through to the entry_id lookup below.
1301 - if ( ! empty( $source_url ) && strpos( $source_url, '_ungrouped_' ) !== 0 ) {
1302 - $rows = $wpdb->get_results( $wpdb->prepare(
1303 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE source_url = %s ORDER BY id ASC",
1304 - $source_url
1305 - ) );
1306 - }
1307 -
1308 - // Fallback / manual "Direct Content" entries: fetch the single row by id.
1309 - if ( empty( $rows ) && $entry_id > 0 ) {
1310 - $row = $wpdb->get_row( $wpdb->prepare(
1311 - "SELECT id, article_content, source_url, content_type FROM {$table} WHERE id = %d",
1312 - $entry_id
1313 - ) );
1314 - if ( $row ) {
1315 - $rows = array( $row );
1316 - }
1317 - }
1318 -
1319 - if ( empty( $rows ) ) {
1320 - return new WP_Error( 'not_found', esc_html__('Entry not found in the local knowledge database.', 'mxchat') );
1321 - }
1322 -
1323 - $chunks = array();
1324 - $content_type = '';
1325 - foreach ( $rows as $row ) {
1326 - $parsed = MxChat_Chunker::parse_stored_chunk( $row->article_content );
1327 - $text = isset( $parsed['text'] ) ? $parsed['text'] : '';
1328 - $index = isset( $parsed['metadata']['chunk_index'] ) ? intval( $parsed['metadata']['chunk_index'] ) : count( $chunks );
1329 - $content_type = $row->content_type;
1330 - $chunks[] = array(
1331 - 'index' => $index,
1332 - 'text' => $text,
1333 - 'length' => function_exists('mb_strlen') ? mb_strlen( $text ) : strlen( $text ),
1334 - 'row_id' => intval( $row->id ),
1335 - );
1336 - }
1337 -
1338 - usort( $chunks, function( $a, $b ) { return $a['index'] - $b['index']; } );
1339 -
1340 - $assembled = implode( "\n\n", wp_list_pluck( $chunks, 'text' ) );
1341 -
1342 - return array(
1343 - 'store' => 'wordpress',
1344 - 'source_url' => $source_url,
1345 - 'content_type' => $content_type,
1346 - 'is_chunked' => count( $chunks ) > 1,
1347 - 'chunk_count' => count( $chunks ),
1348 - 'assembled' => $assembled,
1349 - 'assembled_length' => function_exists('mb_strlen') ? mb_strlen( $assembled ) : strlen( $assembled ),
1350 - 'chunks' => array_values( $chunks ),
1351 - // WP-DB storage carries no separate vector metadata; surface that fact
1352 - // rather than letting the owner guess (the spec's taxonomy question).
1353 - 'metadata' => array(),
1354 - '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'),
1355 - );
1356 -}
1357 -
1358 -/**
1359 - * Read-only inspector for Pinecone entries. Mirrors get_pinecone_entry_content()
1360 - * but keeps each vector's text + metadata instead of imploding, so the owner can
1361 - * confirm exactly which metadata fields (text/source_url/type/last_updated/created_at/bot_id)
1362 - * are present per chunk. READ-ONLY.
1363 - */
1364 -private function inspect_pinecone_entry( $source_url, $entry_id, $bot_id ) {
1365 - if ( ! class_exists('MxChat_Pinecone_Manager') ) {
1366 - return new WP_Error( 'pinecone_unavailable', esc_html__('Pinecone manager not available.', 'mxchat') );
1367 - }
1368 -
1369 - if ( $bot_id === 'default' || ! class_exists('MxChat_Multi_Bot_Manager') ) {
1370 - $pinecone_options = get_option('mxchat_pinecone_addon_options');
1371 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
1372 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1373 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
1374 - } else {
1375 - $bot_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
1376 - $api_key = $bot_config['api_key'] ?? '';
1377 - $host = $bot_config['host'] ?? '';
1378 - $namespace = $bot_config['namespace'] ?? '';
1379 - }
1380 -
1381 - if ( empty($host) || empty($api_key) ) {
1382 - return new WP_Error( 'pinecone_config', esc_html__('Pinecone not configured.', 'mxchat') );
1383 - }
1384 -
1385 - $base_id = md5( $source_url );
1386 - $vector_ids = array( $base_id );
1387 -
1388 - // NOTE: Pinecone's /vectors/list is a GET endpoint with query parameters.
1389 - // A POST is answered 200-with-an-empty-body, which reads as "no vectors".
1390 - $list_url = "https://{$host}/vectors/list";
1391 - $list_params = array( 'prefix' => $base_id . '_chunk_', 'limit' => 100 );
1392 - if ( ! empty($namespace) ) {
1393 - $list_params['namespace'] = $namespace;
1394 - }
1395 -
1396 - $list_resp = wp_remote_get( $list_url . '?' . http_build_query( $list_params ), array(
1397 - 'headers' => array( 'Api-Key' => $api_key, 'accept' => 'application/json' ),
1398 - 'timeout' => 15,
1399 - ) );
1400 -
1401 - if ( ! is_wp_error($list_resp) ) {
1402 - $list_data = json_decode( wp_remote_retrieve_body($list_resp), true );
1403 - if ( ! empty($list_data['vectors']) ) {
1404 - foreach ( $list_data['vectors'] as $v ) {
1405 - $vector_ids[] = $v['id'];
1406 - }
1407 - }
1408 - }
1409 -
1410 - // NOTE: /vectors/fetch is a GET endpoint too, and Pinecone expects the ids
1411 - // repeated (ids=a&ids=b) — http_build_query would emit ids[0]=a, so build
1412 - // the query string explicitly.
1413 - $fetch_query = array();
1414 - foreach ( $vector_ids as $fetch_vid ) {
1415 - $fetch_query[] = 'ids=' . rawurlencode( $fetch_vid );
1416 - }
1417 - if ( ! empty($namespace) ) {
1418 - $fetch_query[] = 'namespace=' . rawurlencode( $namespace );
1419 - }
1420 -
1421 - $fetch_resp = wp_remote_get( "https://{$host}/vectors/fetch?" . implode( '&', $fetch_query ), array(
1422 - 'headers' => array( 'Api-Key' => $api_key, 'accept' => 'application/json' ),
1423 - 'timeout' => 15,
1424 - ) );
1425 -
1426 - if ( is_wp_error($fetch_resp) ) {
1427 - return new WP_Error( 'pinecone_fetch', esc_html__('Failed to fetch from Pinecone.', 'mxchat') );
1428 - }
1429 -
1430 - $fetch_data = json_decode( wp_remote_retrieve_body($fetch_resp), true );
1431 - $vectors = $fetch_data['vectors'] ?? array();
1432 -
1433 - if ( empty($vectors) ) {
1434 - return new WP_Error( 'not_found', esc_html__('Entry not found in Pinecone.', 'mxchat') );
1435 - }
1436 -
1437 - // Whitelisted metadata fields the spec calls out — shown so devs can confirm
1438 - // what is (and is NOT) stored per vector.
1439 - $meta_fields = array( 'text', 'source_url', 'type', 'last_updated', 'created_at', 'bot_id', 'chunk_index', 'total_chunks' );
1440 - $chunks = array();
1441 - $content_type = '';
1442 - foreach ( $vectors as $vid => $vector ) {
1443 - $meta = isset($vector['metadata']) && is_array($vector['metadata']) ? $vector['metadata'] : array();
1444 - $text = $meta['text'] ?? '';
1445 - $index = isset($meta['chunk_index']) ? intval($meta['chunk_index']) : count($chunks);
1446 - $content_type = $meta['type'] ?? $content_type;
1447 -
1448 - $clean_meta = array();
1449 - foreach ( $meta_fields as $field ) {
1450 - if ( array_key_exists( $field, $meta ) && $field !== 'text' ) {
1451 - $clean_meta[ $field ] = is_scalar( $meta[ $field ] ) ? (string) $meta[ $field ] : wp_json_encode( $meta[ $field ] );
1452 - }
1453 - }
1454 -
1455 - $chunks[] = array(
1456 - 'index' => $index,
1457 - 'text' => $text,
1458 - 'length' => function_exists('mb_strlen') ? mb_strlen( $text ) : strlen( $text ),
1459 - 'vector_id' => (string) $vid,
1460 - 'metadata' => $clean_meta,
1461 - );
1462 - }
1463 -
1464 - usort( $chunks, function( $a, $b ) { return $a['index'] - $b['index']; } );
1465 -
1466 - $assembled = implode( "\n\n", wp_list_pluck( $chunks, 'text' ) );
1467 -
1468 - return array(
1469 - 'store' => 'pinecone',
1470 - 'source_url' => $source_url,
1471 - 'content_type' => $content_type,
1472 - 'is_chunked' => count( $chunks ) > 1,
1473 - 'chunk_count' => count( $chunks ),
1474 - 'assembled' => $assembled,
1475 - 'assembled_length' => function_exists('mb_strlen') ? mb_strlen( $assembled ) : strlen( $assembled ),
1476 - 'chunks' => array_values( $chunks ),
1477 - 'metadata' => array(),
1478 - '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'),
1479 - );
1480 -}
1481 -
1482 -/**
1483 - * AJAX: Save edited content — re-chunks and re-embeds as needed.
1484 - * Works for both WordPress DB and Pinecone entries.
1485 - */
1486 -public function ajax_mxchat_save_entry_content() {
1487 - check_ajax_referer('mxchat_edit_entry_nonce', 'nonce');
1488 -
1489 - if ( ! current_user_can('manage_options') ) {
1490 - wp_send_json_error( array( 'message' => 'Permission denied.' ) );
1491 - }
1492 -
1493 - $source_url = $this->sanitize_entry_source_url( isset($_POST['source_url']) ? wp_unslash($_POST['source_url']) : '' );
1494 - $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
1495 - $content = isset($_POST['content']) ? wp_kses_post( wp_unslash($_POST['content']) ) : '';
1496 - $data_source = isset($_POST['data_source']) ? sanitize_key($_POST['data_source']) : 'wordpress';
1497 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1498 - $content_type = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : 'content';
1499 -
1500 - if ( empty($content) ) {
1501 - wp_send_json_error( array( 'message' => 'Content cannot be empty.' ) );
1502 - }
1503 -
1504 - // Get the embedding API key
1505 - $options = get_option('mxchat_options', array());
1506 - $api_key = '';
1507 -
1508 - if ( $bot_id !== 'default' && class_exists('MxChat_Multi_Bot_Manager') ) {
1509 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
1510 - $api_key = $bot_options['api_key'] ?? '';
1511 - }
1512 - if ( empty($api_key) ) {
1513 - $api_key = $options['api_key'] ?? '';
1514 - }
1515 -
1516 - if ( $data_source === 'pinecone' ) {
1517 - // Pinecone branch. The WP-DB manual-entry delete below must never run here:
1518 - // Pinecone ids are strings, and absint() on a digit-leading md5 hash would
1519 - // yield a real (unrelated) WP row id.
1520 - $raw_vector_id = isset($_POST['entry_id']) ? sanitize_text_field( wp_unslash($_POST['entry_id']) ) : '';
1521 - $is_manual_single = empty($source_url) || strpos($source_url, '_ungrouped_') === 0;
1522 - $is_manual_chunked = strpos($source_url, 'mxchat://') === 0;
1523 -
1524 - if ( $is_manual_single || $is_manual_chunked ) {
1525 - // Manual content: remove the old vectors first, then store as fresh manual
1526 - // content — submit_content_to_db mints a new unique identity (manual_* id
1527 - // for a single vector, an mxchat:// chunk prefix if it now chunks).
1528 - if ( $is_manual_chunked ) {
1529 - // Minted identity: base + chunk vectors share the md5(mxchat://...) prefix.
1530 - MxChat_Utils::delete_chunks_for_url( $source_url, $bot_id );
1531 - } elseif ( ! empty($raw_vector_id) && class_exists('MxChat_Pinecone_Manager') ) {
1532 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
1533 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options( $bot_id );
1534 - if ( ! empty($pinecone_options['mxchat_pinecone_api_key']) && ! empty($pinecone_options['mxchat_pinecone_host']) ) {
1535 - $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
1536 - $raw_vector_id,
1537 - $pinecone_options['mxchat_pinecone_api_key'],
1538 - $pinecone_options['mxchat_pinecone_host'],
1539 - $pinecone_options['mxchat_pinecone_namespace'] ?? ''
1540 - );
1541 - }
1542 - }
1543 - $result = MxChat_Utils::submit_content_to_db( $content, '', $api_key, null, $bot_id, $content_type );
1544 - } else {
1545 - // URL-sourced entry: identity is md5(source_url). submit_content_to_db
1546 - // handles delete-old-chunks → re-chunk → re-embed → store, and sweeps
1547 - // stale chunk vectors when the content now fits in a single vector.
1548 - $result = MxChat_Utils::submit_content_to_db( $content, $source_url, $api_key, md5($source_url), $bot_id, $content_type );
1549 - }
1550 - } else {
1551 - global $wpdb;
1552 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
1553 -
1554 - // If source_url is empty but we have an entry_id, look it up
1555 - if ( empty($source_url) && $entry_id > 0 ) {
1556 - $row = $wpdb->get_row( $wpdb->prepare( "SELECT source_url FROM {$table} WHERE id = %d", $entry_id ) );
1557 - if ( $row && ! empty($row->source_url) ) {
1558 - $source_url = $row->source_url;
1559 - }
1560 - }
1561 -
1562 - // For manual entries (no source_url or mxchat:// prefix), delete the old entry by ID first
1563 - // so submit_content_to_db creates a replacement instead of a duplicate
1564 - // Also treat legacy mxchat.ai source URLs as manual — old bug assigned the site URL to manual entries
1565 - $is_legacy_manual = !empty($source_url) && strpos($source_url, 'mxchat.ai') !== false && strpos($source_url, 'mxchat://') !== 0;
1566 - if ( $entry_id > 0 && (empty($source_url) || strpos($source_url, 'mxchat://') === 0 || $is_legacy_manual) ) {
1567 - $wpdb->delete( $table, array( 'id' => $entry_id ), array( '%d' ) );
1568 - // Clear legacy URL so submit_content_to_db generates a unique mxchat:// identifier
1569 - // instead of reusing the shared URL (which would mass-delete other entries with the same URL)
1570 - if ( $is_legacy_manual ) {
1571 - $source_url = '';
1572 - }
1573 - }
1574 -
1575 - // Use the existing submit_content_to_db which handles chunking, Pinecone, and WP DB
1576 - $vector_id = ! empty($source_url) ? md5($source_url) : md5('mxchat_manual_' . $entry_id);
1577 -
1578 - // submit_content_to_db already handles: delete old chunks → re-chunk → re-embed → store
1579 - $result = MxChat_Utils::submit_content_to_db( $content, $source_url, $api_key, $vector_id, $bot_id, $content_type );
1580 - }
1581 -
1582 - if ( is_wp_error($result) ) {
1583 - wp_send_json_error( array( 'message' => $result->get_error_message() ) );
1584 - }
1585 -
1586 - wp_send_json_success( array( 'message' => 'Content saved and re-embedded successfully.' ) );
1587 -}
1588 -
1589 -public function mxchat_get_pdf_processing_status($pdf_url) {
1590 - $pdf_url = esc_url_raw($pdf_url);
1591 - $status = get_transient(sanitize_key('mxchat_pdf_status_' . md5($pdf_url)));
1592 -
1593 - if (!$status || !is_array($status)) {
1594 - return false;
1595 - }
1596 -
1597 - // Check for stalled processing (no updates for 5 minutes)
1598 - if ($status['status'] === 'processing' && (time() - absint($status['last_update'])) > 300) {
1599 - $status['status'] = 'error';
1600 - $status['error'] = __('PDF processing appears to be stalled. No updates for over 5 minutes.', 'mxchat');
1601 -
1602 - // Save the updated status
1603 - set_transient(
1604 - sanitize_key('mxchat_pdf_status_' . md5($pdf_url)),
1605 - array_map('sanitize_text_field', $status),
1606 - DAY_IN_SECONDS
1607 - );
1608 - }
1609 -
1610 - $result = array(
1611 - 'total_pages' => absint($status['total_pages']),
1612 - 'processed_pages' => absint($status['processed_pages']),
1613 - 'failed_pages' => absint($status['failed_pages'] ?? 0),
1614 - 'percentage' => ($status['total_pages'] > 0)
1615 - ? round((absint($status['processed_pages']) / absint($status['total_pages'])) * 100)
1616 - : 0,
1617 - 'status' => sanitize_text_field($status['status']),
1618 - 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
1619 - 'failed_pages_list' => isset($status['failed_pages_list']) ? $status['failed_pages_list'] : array(),
1620 - 'completion_summary' => isset($status['completion_summary']) ? $status['completion_summary'] : null
1621 - );
1622 -
1623 - // Add error message if present
1624 - if (isset($status['error']) && !empty($status['error'])) {
1625 - $result['error'] = sanitize_text_field($status['error']);
1626 - }
1627 -
1628 - return $result;
1629 -}
1630 -
1631 -
1632 -public function mxchat_handle_sitemap_submission() {
1633 - // Check if the form was submitted and verify permissions
1634 - if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) {
1635 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
1636 - }
1637 -
1638 - // Verify nonce
1639 - check_admin_referer('mxchat_submit_sitemap_action', 'mxchat_submit_sitemap_nonce');
1640 -
1641 - // Validate URL
1642 - if (!isset($_POST['sitemap_url']) || empty($_POST['sitemap_url'])) {
1643 - set_transient('mxchat_admin_notice_error',
1644 - esc_html__('Please provide a valid URL.', 'mxchat'),
1645 - 30
1646 - );
1647 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1648 - exit;
1649 - }
1650 -
1651 - $submitted_url = esc_url_raw($_POST['sitemap_url']);
1652 -
1653 - // Convert Google Drive sharing URLs to direct download URLs
1654 - if ( strpos($submitted_url, 'drive.google.com') !== false ) {
1655 - $file_id = '';
1656 - if ( preg_match('/[?&]id=([a-zA-Z0-9_-]+)/', $submitted_url, $m) ) {
1657 - $file_id = $m[1];
1658 - } elseif ( preg_match('#/file/d/([a-zA-Z0-9_-]+)#', $submitted_url, $m) ) {
1659 - $file_id = $m[1];
1660 - }
1661 - if ( ! empty($file_id) ) {
1662 - $submitted_url = 'https://drive.google.com/uc?export=download&id=' . $file_id;
1663 - }
1664 - }
1665 -
1666 - // Get bot_id from form submission
1667 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1668 -
1669 - // Get bot-specific options and validate the embedding decision —
1670 - // custom-provider-aware (plan cbd5fd).
1671 - $bot_options = $this->get_bot_options($bot_id);
1672 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1673 -
1674 - $preflight = MxChat_Utils::embedding_preflight($options);
1675 - if (!$preflight['ok']) {
1676 - set_transient('mxchat_admin_notice_error', esc_html($preflight['reason']), 30);
1677 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1678 - exit;
1679 - }
1680 - $api_key = $preflight['api_key'];
1681 -
1682 - // Fetch URL — send an honest, versioned MXChat crawler UA (not a spoofed
1683 - // browser). Stale browser UAs are exactly what WAFs like SiteGround's
1684 - // ModSecurity flag as scrapers, 403-ing the fetch (including PDFs served
1685 - // from the site's own media library, which route through this same call).
1686 - // See mxchat_ingest_user_agent(). Accept is kept for content negotiation;
1687 - // the browser-only Accept-Language fingerprint is dropped so it stays
1688 - // coherent with a bot identity.
1689 - $response = wp_remote_get($submitted_url, array(
1690 - 'timeout' => 30,
1691 - 'sslverify' => false,
1692 - 'user-agent' => mxchat_ingest_user_agent(),
1693 - 'headers' => array(
1694 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
1695 - ),
1696 - ));
1697 -
1698 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1699 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP Status: ' . wp_remote_retrieve_response_code($response);
1700 - set_transient('mxchat_admin_notice_error',
1701 - sprintf(
1702 - esc_html__('Failed to fetch the URL: %s', 'mxchat'),
1703 - esc_html($error_message)
1704 - ),
1705 - 30
1706 - );
1707 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1708 - exit;
1709 - }
1710 -
1711 - $content_type = wp_remote_retrieve_header($response, 'content-type');
1712 - $body_content = wp_remote_retrieve_body($response);
1713 -
1714 - if (empty($body_content)) {
1715 - set_transient('mxchat_admin_notice_error',
1716 - esc_html__('Empty response received from URL.', 'mxchat'),
1717 - 30
1718 - );
1719 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1720 - exit;
1721 - }
1722 -
1723 - // Handle PDF URL
1724 - if ($this->mxchat_is_pdf_url($submitted_url, $response)) {
1725 - $result = $this->mxchat_handle_pdf_for_knowledge_base($submitted_url, $response, $bot_id);
1726 -
1727 - if ($result === 'queued') {
1728 - set_transient('mxchat_admin_notice_success',
1729 - esc_html__('PDF queued for processing. Processing will start automatically.', 'mxchat'),
1730 - 30
1731 - );
1732 - } else {
1733 - set_transient('mxchat_admin_notice_error',
1734 - esc_html__('Failed to queue PDF processing: ', 'mxchat') . esc_html($result),
1735 - 30
1736 - );
1737 - }
1738 -
1739 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1740 - exit;
1741 - }
1742 -
1743 - // Handle Sitemap XML
1744 - if (strpos($content_type, 'xml') !== false || strpos($body_content, '<urlset') !== false) {
1745 - libxml_use_internal_errors(true);
1746 - $xml = simplexml_load_string($body_content);
1747 - $xml_errors = libxml_get_errors();
1748 - libxml_clear_errors();
1749 -
1750 - if ($xml === false || !empty($xml_errors)) {
1751 - set_transient('mxchat_admin_notice_error',
1752 - esc_html__('Invalid sitemap XML. Please provide a valid sitemap.', 'mxchat'),
1753 - 30
1754 - );
1755 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1756 - exit;
1757 - }
1758 -
1759 - $result = $this->mxchat_handle_sitemap_for_knowledge_base($xml, $submitted_url, $bot_id);
1760 -
1761 - if ($result === 'queued') {
1762 - set_transient('mxchat_admin_notice_success',
1763 - esc_html__('Sitemap queued for processing. Processing will start automatically.', 'mxchat'),
1764 - 30
1765 - );
1766 - } else {
1767 - // Surface the reason the handler already computed (embedding pre-flight,
1768 - // empty sitemap, queue failure). The old message pointed at the status
1769 - // area, which is empty on this path — nothing was ever queued.
1770 - if (is_string($result) && $result !== '') {
1771 - set_transient('mxchat_admin_notice_error',
1772 - esc_html__('Failed to queue sitemap processing: ', 'mxchat') . esc_html($result),
1773 - 30
1774 - );
1775 - } else {
1776 - set_transient('mxchat_admin_notice_error',
1777 - esc_html__('Failed to queue sitemap processing.', 'mxchat'),
1778 - 30
1779 - );
1780 - }
1781 - }
1782 -
1783 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1784 - exit;
1785 - }
1786 -
1787 - // Handle Regular URL (single page)
1788 - $page_content = $this->mxchat_extract_main_content($body_content);
1789 - $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
1790 -
1791 - //error_log('[MXCHAT-URL-DEBUG] URL: ' . $submitted_url);
1792 - //error_log('[MXCHAT-URL-DEBUG] Extracted page_content length: ' . strlen($page_content));
1793 - //error_log('[MXCHAT-URL-DEBUG] Sanitized content length: ' . strlen($sanitized_content));
1794 - //error_log('[MXCHAT-URL-DEBUG] Content preview: ' . substr($sanitized_content, 0, 500));
1795 -
1796 - if (empty($sanitized_content)) {
1797 - set_transient('mxchat_admin_notice_error',
1798 - esc_html__('No valid content found on the provided URL.', 'mxchat'),
1799 - 30
1800 - );
1801 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1802 - exit;
1803 - }
1804 -
1805 - // For single URLs, process immediately using submit_content_to_db
1806 - // This handles chunking automatically for large content
1807 - $db_result = MxChat_Utils::submit_content_to_db(
1808 - $sanitized_content,
1809 - $submitted_url,
1810 - $api_key,
1811 - null,
1812 - $bot_id,
1813 - 'url' // content_type
1814 - );
1815 -
1816 - if (is_wp_error($db_result)) {
1817 - $error_message = esc_html__('Failed to store content in database: ', 'mxchat') . esc_html($db_result->get_error_message());
1818 - set_transient('mxchat_admin_notice_error', $error_message, 30);
1819 - } else {
1820 - $success_message = esc_html__('URL content successfully submitted!', 'mxchat');
1821 - set_transient('mxchat_admin_notice_success', $success_message, 30);
1822 - }
1823 -
1824 - wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1825 - exit;
1826 -}
1827 -
1828 -
1829 -public function mxchat_get_single_url_status() {
1830 - $status = get_transient('mxchat_single_url_status');
1831 - if (!$status) {
1832 - return null;
1833 - }
1834 -
1835 - // Add human-readable time
1836 - if (isset($status['timestamp'])) {
1837 - $status['human_time'] = human_time_diff(strtotime($status['timestamp']), current_time('timestamp')) . ' ' . __('ago', 'mxchat');
1838 - }
1839 -
1840 - return $status;
1841 -}
1842 -
1843 -public function mxchat_handle_sitemap_for_knowledge_base($xml, $sitemap_url, $bot_id = 'default') {
1844 - if (!current_user_can('manage_options')) {
1845 - return false;
1846 - }
1847 -
1848 - try {
1849 - $sitemap_url = esc_url_raw($sitemap_url);
1850 -
1851 - if (!$xml || !is_object($xml)) {
1852 - throw new Exception(__('Invalid XML object provided', 'mxchat'));
1853 - }
1854 -
1855 - // Get bot-specific embedding API for validation
1856 - $bot_options = $this->get_bot_options($bot_id);
1857 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1858 -
1859 - // Test the embedding API before processing
1860 - $test_phrase = "Test embedding generation for MxChat";
1861 - $test_result = $this->mxchat_generate_embedding($test_phrase, $bot_id);
1862 -
1863 - if (is_string($test_result)) {
1864 - throw new Exception(__('Embedding API validation failed: ', 'mxchat') . $test_result);
1865 - }
1866 -
1867 - if (!is_array($test_result)) {
1868 - throw new Exception(__('Embedding API returned unexpected result type. Please check your configuration.', 'mxchat'));
1869 - }
1870 -
1871 - // Extract URLs from sitemap
1872 - $urls = array();
1873 - foreach ($xml->url as $url_element) {
1874 - $url = esc_url_raw((string)$url_element->loc);
1875 - if ($url) {
1876 - $urls[] = array('url' => $url);
1877 - }
1878 - }
1879 -
1880 - $total_urls = count($urls);
1881 -
1882 - if ($total_urls < 1) {
1883 - throw new Exception(__('No valid URLs found in sitemap', 'mxchat'));
1884 - }
1885 -
1886 - // Create unique queue ID
1887 - $queue_id = 'sitemap_' . md5($sitemap_url . time());
1888 -
1889 - // Add URLs to queue
1890 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'url', $urls, $bot_id);
1891 -
1892 - if ($queued_count === 0) {
1893 - throw new Exception(__('Failed to add URLs to processing queue', 'mxchat'));
1894 - }
1895 -
1896 - // Store queue metadata
1897 - $this->mxchat_set_queue_meta($queue_id, 'source_url', $sitemap_url);
1898 - $this->mxchat_set_queue_meta($queue_id, 'queue_type', 'sitemap');
1899 - $this->mxchat_set_queue_meta($queue_id, 'total_items', $total_urls);
1900 - $this->mxchat_set_queue_meta($queue_id, 'bot_id', $bot_id);
1901 - $this->mxchat_set_queue_meta($queue_id, 'created_at', current_time('mysql'));
1902 -
1903 - // Store queue ID in transient for status tracking
1904 - set_transient('mxchat_active_queue_sitemap', $queue_id, DAY_IN_SECONDS);
1905 - set_transient('mxchat_last_sitemap_url', $sitemap_url, DAY_IN_SECONDS);
1906 -
1907 - return 'queued';
1908 -
1909 - } catch (Exception $e) {
1910 - $error_message = $e->getMessage();
1911 - //error_log(sprintf(esc_html__('Error preparing sitemap for processing: %s', 'mxchat'), esc_html($error_message)));
1912 -
1913 - return $error_message;
1914 - }
1915 -
1916 -}
1917 -
1918 -/**
1919 - * Remove shortcode tags but preserve the content inside them
1920 - * Example: [vc_column]Hello World[/vc_column] becomes "Hello World"
1921 - *
1922 - * @param string $content The content containing shortcodes
1923 - * @return string Content with shortcode tags removed but inner content preserved
1924 - */
1925 -/**
1926 - * Single-pass HTML entity decode for text entering the knowledge base.
1927 - * The corpus should hold what a human reads: a stored `&amp;` consumes
1928 - * extra tokens, distorts the vector away from the form a visitor's
1929 - * question uses, and can be quoted back verbatim in an answer.
1930 - * Deliberately NOT looped to a fixed point — a stored `&amp;amp;` is a
1931 - * legitimate literal `&amp;` and must not collapse further (data loss).
1932 - * UTF-8 charset keeps multibyte (CJK/RTL) text untouched. Both assembly
1933 - * paths call this at their output points so the treatment cannot drift.
1934 - * (Plan d2c92e.)
1935 - */
1936 -private function mxchat_decode_entities_for_indexing($text) {
1937 - return html_entity_decode((string) $text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
1938 -}
1939 -
1940 -/**
1941 - * Price lines for a product's indexed text, pinned to the store's BASE currency.
1942 - *
1943 - * The four product assembly paths each used to call get_woocommerce_currency_symbol()
1944 - * with no argument, which resolves the currency active on the CURRENT request.
1945 - * Multi-currency plugins (CURCY, WOOCS, Aelia, WPML Multicurrency) filter that per
1946 - * request, so whichever currency the store happened to be serving when an import ran
1947 - * was frozen into every product it indexed. The amounts have the mirror problem: the
1948 - * woocommerce_product_get_* filters convert prices in the 'view' context but not in
1949 - * 'edit', so a converted amount could be paired with an unconverted symbol and produce
1950 - * a price that is not merely wrong but incoherent.
1951 - *
1952 - * Base currency option + 'edit' context makes both halves agree and makes the output
1953 - * independent of when the import ran. The currency CODE is emitted alongside the symbol
1954 - * so the model cannot read a bare "$" as USD on a store whose plugin swapped symbols.
1955 - * (Plan 7403ec.)
1956 - */
1957 -private function mxchat_product_price_lines($product) {
1958 - if (!is_object($product) || !method_exists($product, 'get_regular_price')) {
1959 - return '';
1960 - }
1961 -
1962 - $currency = get_option('woocommerce_currency');
1963 - $currency = is_string($currency) ? trim($currency) : '';
1964 - $symbol = ($currency !== '')
1965 - ? get_woocommerce_currency_symbol($currency)
1966 - : get_woocommerce_currency_symbol();
1967 - $symbol = $this->mxchat_decode_entities_for_indexing($symbol);
1968 -
1969 - $regular_price = $product->get_regular_price('edit');
1970 - $sale_price = $product->get_sale_price('edit');
1971 - $price = $product->get_price('edit');
1972 -
1973 - $lines = '';
1974 -
1975 - if (!empty($regular_price)) {
1976 - $lines .= "Price: " . $this->mxchat_format_indexed_price($regular_price, $currency, $symbol) . "\n";
1977 - } elseif (!empty($price)) {
1978 - $lines .= "Price: " . $this->mxchat_format_indexed_price($price, $currency, $symbol) . "\n";
1979 - }
1980 -
1981 - if (!empty($sale_price) && $sale_price !== $regular_price) {
1982 - $lines .= "Sale Price: " . $this->mxchat_format_indexed_price($sale_price, $currency, $symbol) . "\n";
1983 - }
1984 -
1985 - if ($product->is_type('variable')) {
1986 - list($min_price, $max_price) = $this->mxchat_variation_price_range($product);
1987 - if ($min_price !== null && $max_price !== null && (float) $min_price !== (float) $max_price) {
1988 - $lines .= "Price Range: " . $this->mxchat_format_indexed_price($min_price, $currency, $symbol)
1989 - . " - " . $this->mxchat_format_indexed_price($max_price, $currency, $symbol) . "\n";
1990 - }
1991 - }
1992 -
1993 - return $lines;
1994 -}
1995 -
1996 -/**
1997 - * One indexed price amount, labelled with its currency code.
1998 - *
1999 - * "INR 1299.00 (Rs.1299.00)" — the code is what the model should reason from; the symbol
2000 - * is kept so a quoted price still reads naturally. Falls back to the old symbol-only
2001 - * shape when WooCommerce has no base currency configured, and drops the parenthetical
2002 - * when the symbol is absent or IS the code (several currencies have no distinct glyph).
2003 - */
2004 -private function mxchat_format_indexed_price($amount, $currency, $symbol) {
2005 - $amount = (string) $amount;
2006 -
2007 - if ($currency === '') {
2008 - return $symbol . $amount;
2009 - }
2010 -
2011 - if ($symbol === '' || $symbol === $currency) {
2012 - return $currency . ' ' . $amount;
2013 - }
2014 -
2015 - return $currency . ' ' . $amount . ' (' . $symbol . $amount . ')';
2016 -}
2017 -
2018 -/**
2019 - * Min/max variation price read from the variations themselves in 'edit' context.
2020 - *
2021 - * get_variation_price() reads WooCommerce's display price cache, which multi-currency
2022 - * plugins populate with converted values — the same defect the rest of this helper
2023 - * exists to remove. Returns raw stored strings (not floats) so the indexed text keeps
2024 - * the store's own price formatting, and (null, null) when no variation carries a price.
2025 - */
2026 -private function mxchat_variation_price_range($product) {
2027 - $min_raw = null;
2028 - $max_raw = null;
2029 - $min_val = null;
2030 - $max_val = null;
2031 -
2032 - $children = method_exists($product, 'get_children') ? $product->get_children() : array();
2033 -
2034 - foreach ($children as $child_id) {
2035 - $variation = wc_get_product($child_id);
2036 - if (!$variation) {
2037 - continue;
2038 - }
2039 - $raw = $variation->get_price('edit');
2040 - if ($raw === '' || $raw === null) {
2041 - continue;
2042 - }
2043 - $val = (float) $raw;
2044 - if ($min_val === null || $val < $min_val) {
2045 - $min_val = $val;
2046 - $min_raw = $raw;
2047 - }
2048 - if ($max_val === null || $val > $max_val) {
2049 - $max_val = $val;
2050 - $max_raw = $raw;
2051 - }
2052 - }
2053 -
2054 - return array($min_raw, $max_raw);
2055 -}
2056 -
2057 -private function strip_shortcode_tags_preserve_content($content) {
2058 - // Single-pass regex removes all shortcode brackets: [tag], [tag attr="val"], [/tag], [tag /]
2059 - // Content between tags is inherently preserved since only brackets are targeted
2060 - $result = preg_replace('/\[\/?\w[\w-]*[^\]]*\]/', '', $content);
2061 - return ($result !== null) ? $result : $content;
2062 -}
2063 -
2064 -public function mxchat_sanitize_content_for_api($content) {
2065 - //error_log('[MXCHAT-SANITIZE] Original content preview: ' . substr($content, 0, 500) . '...');
2066 -
2067 - // Remove shortcode tags but PRESERVE content inside them
2068 - $content = $this->strip_shortcode_tags_preserve_content($content);
2069 -
2070 - // Remove script, style tags, and HTML comments
2071 - $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
2072 - $content = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $content);
2073 - $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
2074 -
2075 - // Remove all HTML tags and decode HTML entities
2076 - $content = wp_strip_all_tags($content);
2077 - $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
2078 -
2079 - // Normalize whitespace but preserve paragraph breaks
2080 - // First, normalize line endings to \n
2081 - $content = str_replace(["\r\n", "\r"], "\n", $content);
2082 - // Replace multiple spaces/tabs with single space, but preserve newlines
2083 - $content = preg_replace('/[ \t]+/', ' ', $content);
2084 - // Replace 3+ newlines with 2 newlines (max 2 blank lines)
2085 - $content = preg_replace('/\n{3,}/', "\n\n", $content);
2086 - // Trim each line
2087 - $lines = explode("\n", $content);
2088 - $lines = array_map('trim', $lines);
2089 - $content = implode("\n", $lines);
2090 - // Final trim
2091 - $content = trim($content);
2092 -
2093 - // Remove control characters (which can cause database issues) but preserve \n (0x0A) and \r (0x0D)
2094 - $content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $content);
2095 -
2096 - // Remove NULL bytes which can cause database errors
2097 - $content = str_replace("\0", "", $content);
2098 -
2099 - // Ensure valid UTF-8 encoding
2100 - $content = wp_check_invalid_utf8($content);
2101 -
2102 - // Remove extremely long runs with no whitespace (base64 blobs, minified JS).
2103 - // Counts CHARACTERS (/u), and never strips a run containing characters from a
2104 - // script written without spaces — Japanese, Chinese, Thai, Khmer, Lao, Myanmar —
2105 - // where a normal paragraph is legitimately one unbroken run.
2106 - $content = preg_replace_callback('/\S{300,}/u', function ($m) {
2107 - return preg_match('/[\p{Han}\p{Hiragana}\p{Katakana}\p{Thai}\p{Khmer}\p{Lao}\p{Myanmar}]/u', $m[0]) ? $m[0] : ' ';
2108 - }, $content);
2109 -
2110 - // Remove emoji/symbol blocks only — not the whole supplementary plane, which
2111 - // also holds CJK Extension B ideographs used in real Chinese/Japanese names.
2112 - // A ZWJ (U+200D) BETWEEN stripped pictographs is consumed with them, so a
2113 - // family sequence like 👨‍👩‍👧 leaves no invisible zero-width residue behind
2114 - // (the joiner between NON-emoji characters — Hindi conjuncts — is untouched).
2115 - $content = preg_replace('/[\x{1F000}-\x{1F0FF}\x{1F300}-\x{1FAFF}](?:\x{200D}[\x{1F000}-\x{1F0FF}\x{1F300}-\x{1FAFF}])*/u', '', $content);
2116 -
2117 - // There is deliberately NO catch-all character allowlist here (plan 209e57;
2118 - // one existed until 3.2.20). Every genuinely dangerous byte is already gone:
2119 - // control characters, null bytes, invalid UTF-8 and the emoji blocks are all
2120 - // stripped above. The allowlist's only remaining effect was to damage scripts
2121 - // nobody thought to enumerate — Unicode Cf (Format) was missing, so it
2122 - // replaced the zero-width joiner/non-joiner with spaces and silently split
2123 - // Persian words (می‌روم → می روم) and broke Hindi conjuncts (क्‍ष → क् ष).
2124 - // Do not add one back; the failure mode of an allowlist is exactly this.
2125 -
2126 - // Limit to reasonable length if needed (byte limit — MySQL TEXT is byte-sized,
2127 - // but cut on a character boundary so a multibyte char is never split mid-sequence)
2128 - $max_length = 65000; // Just under MySQL TEXT field limit
2129 - if (strlen($content) > $max_length) {
2130 - $content = mb_strcut($content, 0, $max_length, 'UTF-8');
2131 - }
2132 -
2133 - //error_log('[MXCHAT-SANITIZE] Sanitized content preview: ' . substr($content, 0, 500) . '...');
2134 - return $content;
2135 -}
2136 -public function mxchat_extract_main_content($html) {
2137 - if (empty($html)) {
2138 - return '';
2139 - }
2140 - try {
2141 - $dom = new DOMDocument;
2142 - libxml_use_internal_errors(true); // Suppress HTML parsing errors
2143 - @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
2144 - $xpath = new DOMXPath($dom);
2145 -
2146 - // For debugging purposes
2147 - $debugEnabled = true; // Set to true to enable debugging output
2148 - $debug = function($message) use ($debugEnabled) {
2149 - if ($debugEnabled) {
2150 - //error_log('[MXCHAT-EXTRACT-DEBUG] ' . $message);
2151 - }
2152 - };
2153 -
2154 - // Direct targeting for Gerow theme posts
2155 - $post_text = $xpath->query('//div[contains(@class, "post-text")]');
2156 - if ($post_text && $post_text->length > 0) {
2157 - $debug("Found post-text directly");
2158 - $content = '';
2159 - foreach ($post_text as $node) {
2160 - $content .= $dom->saveHTML($node);
2161 - }
2162 - if (!empty($content)) {
2163 - $debug("Returning post-text content");
2164 - return $content;
2165 - }
2166 - }
2167 -
2168 - // Try to get the blog details content which contains the post-text
2169 - $blog_details = $xpath->query('//div[contains(@class, "blog-details-content")]');
2170 - if ($blog_details && $blog_details->length > 0) {
2171 - $debug("Found blog-details-content");
2172 - $content = '';
2173 - foreach ($blog_details as $node) {
2174 - $content .= $dom->saveHTML($node);
2175 - }
2176 - if (!empty($content)) {
2177 - $debug("Returning blog-details-content");
2178 - return $content;
2179 - }
2180 - }
2181 -
2182 - // Try to get the article which contains the blog details
2183 - $article = $xpath->query('//article[contains(@class, "blog-details-wrap")]');
2184 - if ($article && $article->length > 0) {
2185 - $debug("Found article with blog-details-wrap");
2186 - $content = '';
2187 - foreach ($article as $node) {
2188 - $content .= $dom->saveHTML($node);
2189 - }
2190 - if (!empty($content)) {
2191 - $debug("Returning article content");
2192 - return $content;
2193 - }
2194 - }
2195 -
2196 - // Try even broader with the blog-item-wrap
2197 - $blog_item = $xpath->query('//div[contains(@class, "blog-item-wrap")]');
2198 - if ($blog_item && $blog_item->length > 0) {
2199 - $debug("Found blog-item-wrap");
2200 - $content = '';
2201 - foreach ($blog_item as $node) {
2202 - $content .= $dom->saveHTML($node);
2203 - }
2204 - if (!empty($content)) {
2205 - $debug("Returning blog-item-wrap content");
2206 - return $content;
2207 - }
2208 - }
2209 -
2210 - // Specific Gerow theme path
2211 - $gerow_path = $xpath->query('//section[contains(@class, "blog-area")]//div[contains(@class, "post-text")]');
2212 - if ($gerow_path && $gerow_path->length > 0) {
2213 - $debug("Found Gerow theme path to post-text");
2214 - $content = '';
2215 - foreach ($gerow_path as $node) {
2216 - $content .= $dom->saveHTML($node);
2217 - }
2218 - if (!empty($content)) {
2219 - $debug("Returning Gerow post-text content");
2220 - return $content;
2221 - }
2222 - }
2223 -
2224 - // Generic blog post selectors
2225 - $selectors = [
2226 - // Blog post specific selectors
2227 - '//div[contains(@class, "post-text")]',
2228 - '//article[contains(@class, "blog-post-item")]//div[contains(@class, "post-text")]',
2229 - '//div[contains(@class, "blog-details-content")]',
2230 - '//article[contains(@class, "blog-details-wrap")]',
2231 - '//div[contains(@class, "entry-content")]',
2232 - '//div[contains(@class, "blog-content")]',
2233 - '//div[contains(@class, "blog-item-wrap")]',
2234 -
2235 - // More general content selectors
2236 - '//div[contains(@class, "page__content")]',
2237 - '//div[contains(@class, "elementor-widget-container")]',
2238 - '//div[contains(@class, "elementor-text-editor")]',
2239 - '//div[contains(@class, "elementor-widget-text-editor")]',
2240 - '//*[contains(@class, "entry-content")]',
2241 - '//*[contains(@class, "post-content")]',
2242 - '//*[contains(@class, "article-content")]',
2243 - '//*[@id="content"]',
2244 - '//*[@id="main-content"]',
2245 - '//section[contains(@class, "blog-area")]',
2246 - '//article',
2247 - '//main',
2248 - '//div[contains(@class, "content")]'
2249 - ];
2250 -
2251 - // First handle Elementor content - get only leaf widget containers to avoid duplicates
2252 - $debug("Checking for Elementor content");
2253 - // Get widget containers that are direct children of widgets (not nested inside other widget containers)
2254 - $elementor_widgets = $xpath->query('//div[contains(@class, "elementor-widget")]//div[contains(@class, "elementor-widget-container")]');
2255 - if ($elementor_widgets && $elementor_widgets->length > 0) {
2256 - $debug("Found Elementor widgets");
2257 - $seen_content = array(); // Track seen content to avoid duplicates
2258 - $combined_content = '';
2259 - foreach ($elementor_widgets as $widget) {
2260 - $widget_content = $dom->saveHTML($widget);
2261 - if (!empty($widget_content)) {
2262 - // Create a hash of the content to detect duplicates
2263 - $content_hash = md5($widget_content);
2264 - if (!isset($seen_content[$content_hash])) {
2265 - $seen_content[$content_hash] = true;
2266 - $combined_content .= $widget_content;
2267 - }
2268 - }
2269 - }
2270 - if (!empty($combined_content)) {
2271 - $debug("Returning Elementor content");
2272 - return $combined_content;
2273 - }
2274 - }
2275 -
2276 - // Try standard selectors one by one
2277 - foreach ($selectors as $selector) {
2278 - $debug("Trying selector: " . $selector);
2279 - $nodes = $xpath->query($selector);
2280 - if ($nodes && $nodes->length > 0) {
2281 - $debug("Found " . $nodes->length . " matches for selector: " . $selector);
2282 - // Only take the FIRST matching node to avoid duplicate content
2283 - // (pages often have nested or multiple containers with same class)
2284 - $content = $dom->saveHTML($nodes->item(0));
2285 - if (!empty($content)) {
2286 - $debug("Returning content from selector: " . $selector . " (first match only)");
2287 - return $content;
2288 - }
2289 - }
2290 - }
2291 -
2292 - // Manual regex fallback for post-text if DOM methods fail
2293 - $debug("Trying regex fallback");
2294 - if (preg_match('/<div class="post-text">(.*?)<\/div>\s*<\/div>\s*<\/div>/s', $html, $matches)) {
2295 - $debug("Found post-text via regex");
2296 - return '<div class="post-text">' . $matches[1] . '</div>';
2297 - }
2298 -
2299 - // Try to extract the blog section as a whole
2300 - $blog_section = $xpath->query('//section[contains(@class, "blog-area")]');
2301 - if ($blog_section && $blog_section->length > 0) {
2302 - $debug("Found blog-area section");
2303 - $content = '';
2304 - foreach ($blog_section as $node) {
2305 - $content .= $dom->saveHTML($node);
2306 - }
2307 - if (!empty($content)) {
2308 - $debug("Returning blog-area section content");
2309 - return $content;
2310 - }
2311 - }
2312 -
2313 - // Generic container selectors for non-CMS sites (like .asp pages)
2314 - $debug("Trying generic container selectors");
2315 - $generic_selectors = [
2316 - '//div[@id="main"]',
2317 - '//div[@id="wrapper"]',
2318 - '//div[@id="page"]',
2319 - '//div[@id="site-content"]',
2320 - '//div[contains(@class, "main-content")]',
2321 - '//div[contains(@class, "page-content")]',
2322 - '//div[contains(@class, "site-content")]',
2323 - ];
2324 -
2325 - foreach ($generic_selectors as $selector) {
2326 - $debug("Trying generic selector: " . $selector);
2327 - $nodes = $xpath->query($selector);
2328 - if ($nodes && $nodes->length > 0) {
2329 - $content = $dom->saveHTML($nodes->item(0));
2330 - if (!empty($content)) {
2331 - $debug("Returning content from generic selector: " . $selector);
2332 - return $content;
2333 - }
2334 - }
2335 - }
2336 -
2337 - // Paragraph-based content detection - find regions with substantial text
2338 - $debug("Trying paragraph-based content detection");
2339 - $paragraphs = $xpath->query('//p[string-length(normalize-space()) > 50]');
2340 - if ($paragraphs && $paragraphs->length >= 3) {
2341 - $debug("Found " . $paragraphs->length . " substantial paragraphs");
2342 - // Collect all substantial paragraphs and their content
2343 - $paragraph_content = '';
2344 - foreach ($paragraphs as $p) {
2345 - $paragraph_content .= $dom->saveHTML($p) . "\n";
2346 - }
2347 - if (!empty($paragraph_content)) {
2348 - $debug("Returning paragraph-based content");
2349 - return $paragraph_content;
2350 - }
2351 - }
2352 -
2353 - // Improved body fallback - strip nav/header/footer elements first
2354 - $debug("Using improved body fallback");
2355 - $body = $dom->getElementsByTagName('body');
2356 - if ($body->length > 0) {
2357 - // Clone the body to avoid modifying the original DOM
2358 - $body_clone = $body->item(0)->cloneNode(true);
2359 -
2360 - // Remove common non-content elements by tag name
2361 - $remove_tags = ['nav', 'header', 'footer', 'aside', 'script', 'style', 'noscript'];
2362 - foreach ($remove_tags as $tag) {
2363 - $elements = $body_clone->getElementsByTagName($tag);
2364 - // Iterate backwards to safely remove elements
2365 - for ($i = $elements->length - 1; $i >= 0; $i--) {
2366 - $el = $elements->item($i);
2367 - if ($el && $el->parentNode) {
2368 - $el->parentNode->removeChild($el);
2369 - }
2370 - }
2371 - }
2372 -
2373 - // Remove elements with common non-content class names using XPath on the cloned body
2374 - $temp_dom = new DOMDocument();
2375 - @$temp_dom->appendChild($temp_dom->importNode($body_clone, true));
2376 - $temp_xpath = new DOMXPath($temp_dom);
2377 -
2378 - $remove_class_patterns = [
2379 - '//*[contains(@class, "nav")]',
2380 - '//*[contains(@class, "menu")]',
2381 - '//*[contains(@class, "sidebar")]',
2382 - '//*[contains(@class, "footer")]',
2383 - '//*[contains(@class, "header")]',
2384 - '//*[contains(@id, "nav")]',
2385 - '//*[contains(@id, "menu")]',
2386 - '//*[contains(@id, "sidebar")]',
2387 - '//*[contains(@id, "footer")]',
2388 - '//*[contains(@id, "header")]',
2389 - ];
2390 -
2391 - foreach ($remove_class_patterns as $pattern) {
2392 - $elements = $temp_xpath->query($pattern);
2393 - if ($elements) {
2394 - for ($i = $elements->length - 1; $i >= 0; $i--) {
2395 - $el = $elements->item($i);
2396 - if ($el && $el->parentNode) {
2397 - $el->parentNode->removeChild($el);
2398 - }
2399 - }
2400 - }
2401 - }
2402 -
2403 - $cleaned_content = $temp_dom->saveHTML();
2404 - if (!empty($cleaned_content)) {
2405 - $debug("Returning cleaned body content");
2406 - return $cleaned_content;
2407 - }
2408 - }
2409 -
2410 - // Last resort: return the original HTML
2411 - $debug("Returning original HTML");
2412 - return $html;
2413 - } catch (Exception $e) {
2414 - //error_log('[MXCHAT-ERROR] Content extraction failed: ' . $e->getMessage());
2415 - return $html; // Return original HTML if parsing fails
2416 - } finally {
2417 - libxml_clear_errors();
2418 - }
2419 -}
2420 -public function mxchat_get_sitemap_processing_status($sitemap_url) {
2421 - $sitemap_url = esc_url_raw($sitemap_url);
2422 - $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
2423 - $status = get_transient($status_key);
2424 -
2425 - if (!$status || !is_array($status)) {
2426 - return false;
2427 - }
2428 -
2429 - // Auto-complete check: if all URLs are processed but status isn't complete
2430 - if (isset($status['processed_urls']) && isset($status['total_urls']) &&
2431 - $status['processed_urls'] >= $status['total_urls'] &&
2432 - isset($status['status']) && $status['status'] !== 'complete' &&
2433 - $status['status'] !== 'error') {
2434 -
2435 - // Mark as complete
2436 - $status['status'] = 'complete';
2437 - $status['processed_urls'] = $status['total_urls']; // Ensure exact match
2438 -
2439 - // Update the transient with the corrected status
2440 - set_transient($status_key, $status, DAY_IN_SECONDS);
2441 - }
2442 -
2443 - return array(
2444 - 'total_urls' => absint($status['total_urls']),
2445 - 'processed_urls' => absint($status['processed_urls']),
2446 - 'failed_urls' => absint($status['failed_urls'] ?? 0),
2447 - 'percentage' => ($status['total_urls'] > 0)
2448 - ? round((absint($status['processed_urls']) / absint($status['total_urls'])) * 100)
2449 - : 0,
2450 - 'status' => sanitize_text_field($status['status']),
2451 - 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
2452 - 'error' => isset($status['error']) ? sanitize_text_field($status['error']) : '',
2453 - 'last_error' => isset($status['last_error']) ? sanitize_text_field($status['last_error']) : '',
2454 - 'failed_urls_list' => isset($status['failed_urls_list']) ? $status['failed_urls_list'] : array()
2455 - );
2456 -}
2457 -
2458 -public function mxchat_ajax_get_status_updates() {
2459 - try {
2460 - // Verify the request
2461 - check_ajax_referer('mxchat_status_nonce', 'nonce');
2462 -
2463 - // Get active queue IDs
2464 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
2465 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
2466 -
2467 - $sitemap_status = false;
2468 - $pdf_status = false;
2469 -
2470 - // Get sitemap queue status
2471 - if ($sitemap_queue_id) {
2472 - $sitemap_status = $this->mxchat_get_queue_status_data($sitemap_queue_id, 'sitemap');
2473 - }
2474 -
2475 - // Get PDF queue status
2476 - if ($pdf_queue_id) {
2477 - $pdf_status = $this->mxchat_get_queue_status_data($pdf_queue_id, 'pdf');
2478 - }
2479 -
2480 - $is_active_processing =
2481 - ($sitemap_status && $sitemap_status['status'] === 'processing') ||
2482 - ($pdf_status && $pdf_status['status'] === 'processing');
2483 -
2484 - // Return JSON response with the status data
2485 - wp_send_json(array(
2486 - 'pdf_status' => $pdf_status,
2487 - 'sitemap_status' => $sitemap_status,
2488 - 'is_processing' => $is_active_processing,
2489 - 'sitemap_queue_id' => $sitemap_queue_id,
2490 - 'pdf_queue_id' => $pdf_queue_id
2491 - ));
2492 -
2493 - } catch (Exception $e) {
2494 - //error_log('MxChat Status Update Error: ' . $e->getMessage());
2495 -
2496 - wp_send_json_error(array(
2497 - 'message' => 'Error getting status updates: ' . $e->getMessage(),
2498 - 'status' => 'error'
2499 - ));
2500 - }
2501 -}
2502 -
2503 -/**
2504 - * Helper function to get queue status data
2505 - */
2506 -private function mxchat_get_queue_status_data($queue_id, $type = 'sitemap') {
2507 - global $wpdb;
2508 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
2509 -
2510 - // Get counts by status
2511 - $counts = $wpdb->get_results($wpdb->prepare(
2512 - "SELECT status, COUNT(*) as count
2513 - FROM $table_name
2514 - WHERE queue_id = %s
2515 - GROUP BY status",
2516 - $queue_id
2517 - ), OBJECT_K);
2518 -
2519 - $total = 0;
2520 - $completed = 0;
2521 - $failed = 0;
2522 - $processing = 0;
2523 - $pending = 0;
2524 -
2525 - foreach ($counts as $status => $data) {
2526 - $count = absint($data->count);
2527 - $total += $count;
2528 -
2529 - switch ($status) {
2530 - case 'completed':
2531 - $completed = $count;
2532 - break;
2533 - case 'failed':
2534 - $failed = $count;
2535 - break;
2536 - case 'processing':
2537 - $processing = $count;
2538 - break;
2539 - case 'pending':
2540 - $pending = $count;
2541 - break;
2542 - }
2543 - }
2544 -
2545 - if ($total === 0) {
2546 - return false;
2547 - }
2548 -
2549 - // Calculate percentage
2550 - $percentage = round((($completed + $failed) / $total) * 100);
2551 -
2552 - // Get failed items details (limit to 50)
2553 - $failed_items = array();
2554 - if ($failed > 0) {
2555 - $failed_results = $wpdb->get_results($wpdb->prepare(
2556 - "SELECT item_type, item_data, error_message, attempts, completed_at
2557 - FROM $table_name
2558 - WHERE queue_id = %s
2559 - AND status = 'failed'
2560 - AND attempts >= max_attempts
2561 - ORDER BY id DESC
2562 - LIMIT 50",
2563 - $queue_id
2564 - ));
2565 -
2566 - foreach ($failed_results as $item) {
2567 - $data = json_decode($item->item_data, true);
2568 - $url = $type === 'pdf' ? ($data['pdf_url'] ?? '') : ($data['url'] ?? '');
2569 -
2570 - $failed_items[] = array(
2571 - 'url' => $url,
2572 - 'page' => $type === 'pdf' ? ($data['page_number'] ?? 0) : 0,
2573 - 'error' => $item->error_message,
2574 - 'retries' => $item->attempts,
2575 - 'time' => strtotime($item->completed_at)
2576 - );
2577 - }
2578 - }
2579 -
2580 - // Get queue metadata
2581 - $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
2582 -
2583 - // Determine if queue is complete
2584 - $is_complete = ($pending === 0 && $processing === 0);
2585 -
2586 - // Get last update time
2587 - $last_update = $wpdb->get_var($wpdb->prepare(
2588 - "SELECT MAX(UNIX_TIMESTAMP(COALESCE(completed_at, started_at, created_at)))
2589 - FROM $table_name
2590 - WHERE queue_id = %s",
2591 - $queue_id
2592 - ));
2593 -
2594 - $last_update_text = $last_update ? human_time_diff($last_update, time()) . ' ' . __('ago', 'mxchat') : __('Just now', 'mxchat');
2595 -
2596 - // Format based on type
2597 - if ($type === 'pdf') {
2598 - return array(
2599 - 'total_pages' => $total,
2600 - 'processed_pages' => $completed + $failed,
2601 - 'failed_pages' => $failed,
2602 - 'percentage' => $percentage,
2603 - 'status' => $is_complete ? 'complete' : 'processing',
2604 - 'last_update' => $last_update_text,
2605 - 'failed_pages_list' => $failed_items,
2606 - 'pdf_url' => $source_url,
2607 - 'queue_id' => $queue_id
2608 - );
2609 - } else {
2610 - return array(
2611 - 'total_urls' => $total,
2612 - 'processed_urls' => $completed + $failed,
2613 - 'failed_urls' => $failed,
2614 - 'percentage' => $percentage,
2615 - 'status' => $is_complete ? 'complete' : 'processing',
2616 - 'last_update' => $last_update_text,
2617 - 'failed_urls_list' => $failed_items,
2618 - 'sitemap_url' => $source_url,
2619 - 'queue_id' => $queue_id
2620 - );
2621 - }
2622 -}
2623 -
2624 -/**
2625 - * Public method to get processing status for both sitemap and PDF queues
2626 - * Used by admin pages to display processing status
2627 - *
2628 - * @return array Array with 'sitemap_status', 'pdf_status', and 'is_processing' keys
2629 - */
2630 -public function mxchat_get_processing_statuses() {
2631 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
2632 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
2633 -
2634 - $sitemap_status = false;
2635 - $pdf_status = false;
2636 -
2637 - if ($sitemap_queue_id) {
2638 - $sitemap_status = $this->mxchat_get_queue_status_data($sitemap_queue_id, 'sitemap');
2639 - }
2640 -
2641 - if ($pdf_queue_id) {
2642 - $pdf_status = $this->mxchat_get_queue_status_data($pdf_queue_id, 'pdf');
2643 - }
2644 -
2645 - $is_processing =
2646 - ($sitemap_status && $sitemap_status['status'] === 'processing') ||
2647 - ($pdf_status && $pdf_status['status'] === 'processing');
2648 -
2649 - return array(
2650 - 'sitemap_status' => $sitemap_status,
2651 - 'pdf_status' => $pdf_status,
2652 - 'is_processing' => $is_processing
2653 - );
2654 -}
2655 -
2656 -/**
2657 - * AJAX handler to get recent knowledge entries for real-time table updates
2658 - * UPDATED: Now supports both WordPress DB and Pinecone data sources
2659 - */
2660 -public function ajax_mxchat_get_recent_entries() {
2661 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
2662 -
2663 - if (!current_user_can('manage_options')) {
2664 - wp_send_json_error(array('message' => 'Unauthorized'));
2665 - return;
2666 - }
2667 -
2668 - global $wpdb;
2669 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2670 -
2671 - // Get parameters
2672 - $last_id = isset($_POST['last_id']) ? absint($_POST['last_id']) : 0;
2673 - $limit = isset($_POST['limit']) ? min(absint($_POST['limit']), 50) : 10;
2674 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
2675 -
2676 - // Check if Pinecone is enabled for this bot
2677 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
2678 - $pinecone_options = $pinecone_manager ? $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id) : array();
2679 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2680 - $has_pinecone_api = !empty($pinecone_options['mxchat_pinecone_api_key']);
2681 -
2682 - if ($use_pinecone && $has_pinecone_api) {
2683 - // PINECONE DATA SOURCE - Get grouped entry count (not raw vector count)
2684 - // Use mxchat_fetch_pinecone_records which returns total_unique_entries
2685 - $records = $pinecone_manager->mxchat_fetch_pinecone_records($pinecone_options, '', 1, 10, $bot_id, '');
2686 - $total_count = $records['total'] ?? 0;
2687 -
2688 - // For Pinecone, we don't return individual entries during polling
2689 - // (entries are already displayed on page load via mxchat_fetch_pinecone_records)
2690 - // We just return the updated count
2691 - wp_send_json_success(array(
2692 - 'entries' => array(),
2693 - 'total_count' => absint($total_count),
2694 - 'max_id' => $last_id,
2695 - 'data_source' => 'pinecone'
2696 - ));
2697 - return;
2698 - }
2699 -
2700 - // WORDPRESS DB DATA SOURCE
2701 - // Build query to get entries newer than last_id
2702 - $where_clauses = array('1=1');
2703 - $where_values = array();
2704 -
2705 - if ($last_id > 0) {
2706 - $where_clauses[] = 'id > %d';
2707 - $where_values[] = $last_id;
2708 - }
2709 -
2710 - // Note: WordPress DB table doesn't have bot_id column
2711 - // Multi-bot filtering is handled via Pinecone namespaces
2712 -
2713 - $where_sql = implode(' AND ', $where_clauses);
2714 -
2715 - // Get recent entries
2716 - $query = "SELECT id, article_content, source_url, timestamp
2717 - FROM $table_name
2718 - WHERE $where_sql
2719 - ORDER BY id DESC
2720 - LIMIT %d";
2721 -
2722 - $where_values[] = $limit;
2723 -
2724 - $entries = $wpdb->get_results($wpdb->prepare($query, $where_values));
2725 -
2726 - // Get total count of GROUPED entries (by source_url) - matches pagination display
2727 - // Count unique source_urls (excluding mxchat:// internal refs) + count of ungrouped rows
2728 - $total_count = $wpdb->get_var(
2729 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} WHERE source_url != '' AND source_url NOT LIKE 'mxchat://%') +
2730 - (SELECT COUNT(*) FROM {$table_name} WHERE source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')"
2731 - );
2732 -
2733 - // Format entries for response
2734 - $formatted_entries = array();
2735 - $preview_length = 150;
2736 - foreach ($entries as $entry) {
2737 - // Parse chunk metadata using the proper chunker method (same as initial page load)
2738 - if (class_exists('MxChat_Chunker')) {
2739 - $chunk_meta = MxChat_Chunker::parse_stored_chunk($entry->article_content);
2740 - $display_content = $chunk_meta['text'];
2741 - $chunk_metadata = $chunk_meta['metadata'];
2742 - } else {
2743 - $display_content = $entry->article_content;
2744 - $chunk_metadata = array();
2745 - }
2746 -
2747 - $content_preview = mb_strlen($display_content) > $preview_length
2748 - ? mb_substr($display_content, 0, $preview_length) . '...'
2749 - : $display_content;
2750 -
2751 - $formatted_entries[] = array(
2752 - 'id' => $entry->id,
2753 - 'preview' => esc_html($content_preview),
2754 - 'full_content' => wp_kses_post(wpautop($display_content)),
2755 - 'content_length' => mb_strlen($display_content),
2756 - 'preview_length' => $preview_length,
2757 - 'source_url' => $entry->source_url,
2758 - 'has_link' => !empty($entry->source_url) && strpos($entry->source_url, 'mxchat://') !== 0,
2759 - 'chunk_metadata' => $chunk_metadata,
2760 - 'bot_id' => $entry->bot_id ?? 'default',
2761 - 'edit_nonce' => wp_create_nonce('mxchat_edit_entry_nonce'),
2762 - 'delete_nonce' => wp_create_nonce('mxchat_delete_prompt_nonce')
2763 - );
2764 - }
2765 -
2766 - wp_send_json_success(array(
2767 - 'entries' => $formatted_entries,
2768 - 'total_count' => absint($total_count),
2769 - 'max_id' => !empty($entries) ? $entries[0]->id : $last_id,
2770 - 'data_source' => 'wordpress'
2771 - ));
2772 -}
2773 -
2774 -/**
2775 - * Get Pinecone total count from stats API
2776 - * Helper function for ajax_mxchat_get_recent_entries
2777 - */
2778 -private function mxchat_get_pinecone_count_from_stats($pinecone_options) {
2779 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2780 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2781 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
2782 -
2783 - if (empty($api_key) || empty($host)) {
2784 - return 0;
2785 - }
2786 -
2787 - try {
2788 - $stats_url = "https://{$host}/describe_index_stats";
2789 -
2790 - $response = wp_remote_post($stats_url, array(
2791 - 'headers' => array(
2792 - 'Api-Key' => $api_key,
2793 - 'Content-Type' => 'application/json'
2794 - ),
2795 - 'body' => '{}',
2796 - 'timeout' => 10
2797 - ));
2798 -
2799 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
2800 - $body = wp_remote_retrieve_body($response);
2801 - $stats_data = json_decode($body, true);
2802 -
2803 - // If namespace is specified, get count from that specific namespace
2804 - if (!empty($namespace) && isset($stats_data['namespaces'][$namespace]['vectorCount'])) {
2805 - return intval($stats_data['namespaces'][$namespace]['vectorCount']);
2806 - }
2807 -
2808 - // If no namespace specified or namespace not found in response, use total
2809 - return intval($stats_data['totalVectorCount'] ?? 0);
2810 - }
2811 -
2812 - return 0;
2813 -
2814 - } catch (Exception $e) {
2815 - return 0;
2816 - }
2817 -}
2818 -
2819 -/**
2820 - * AJAX handler to refresh Pinecone entries table via AJAX
2821 - * Returns the table HTML for updating the UI without a full page reload
2822 - */
2823 -public function ajax_mxchat_refresh_pinecone_entries() {
2824 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
2825 -
2826 - if (!current_user_can('manage_options')) {
2827 - wp_send_json_error(array('message' => 'Unauthorized'));
2828 - return;
2829 - }
2830 -
2831 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
2832 - $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
2833 - $per_page = 25;
2834 - $search_query = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
2835 - $content_type_filter = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : '';
2836 -
2837 - // Get Pinecone manager and options
2838 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
2839 - if (!$pinecone_manager) {
2840 - wp_send_json_error(array('message' => 'Pinecone manager not available'));
2841 - return;
2842 - }
2843 -
2844 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
2845 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2846 - $pinecone_api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2847 -
2848 - if (!$use_pinecone || empty($pinecone_api_key)) {
2849 - wp_send_json_error(array('message' => 'Pinecone not configured'));
2850 - return;
2851 - }
2852 -
2853 - // Fetch records from Pinecone
2854 - $records = $pinecone_manager->mxchat_fetch_pinecone_records($pinecone_options, $search_query, $page, $per_page, $bot_id, $content_type_filter);
2855 - $prompts = $records['data'] ?? array();
2856 - $total_records = $records['total'] ?? 0;
2857 -
2858 - // Preprocess Pinecone records — set chunk_metadata and display_content
2859 - // (matches admin-knowledge-page.php preprocessing)
2860 - foreach ($prompts as $prompt) {
2861 - if (isset($prompt->chunk_index) && $prompt->chunk_index !== null) {
2862 - $prompt->chunk_metadata = array(
2863 - 'chunk_index' => intval($prompt->chunk_index),
2864 - 'total_chunks' => isset($prompt->total_chunks) ? intval($prompt->total_chunks) : null,
2865 - 'is_chunked' => isset($prompt->is_chunked) ? (bool) $prompt->is_chunked : true,
2866 - 'source_url' => $prompt->source_url ?? ''
2867 - );
2868 - $prompt->display_content = $prompt->article_content;
2869 - } else {
2870 - $prompt->chunk_metadata = array();
2871 - $prompt->display_content = $prompt->article_content ?? '';
2872 - }
2873 - }
2874 -
2875 - // Group prompts by source_url
2876 - $grouped_prompts = array();
2877 - foreach ($prompts as $prompt) {
2878 - $source_url = '';
2879 - if (!empty($prompt->chunk_metadata['source_url'])) {
2880 - $source_url = $prompt->chunk_metadata['source_url'];
2881 - } elseif (!empty($prompt->source_url)) {
2882 - $source_url = $prompt->source_url;
2883 - }
2884 -
2885 - if (!empty($source_url)) {
2886 - if (!isset($grouped_prompts[$source_url])) {
2887 - $grouped_prompts[$source_url] = array();
2888 - }
2889 - $grouped_prompts[$source_url][] = $prompt;
2890 - } else {
2891 - $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
2892 - }
2893 - }
2894 -
2895 - // Sort each group by chunk_index
2896 - foreach ($grouped_prompts as $source_url => &$group) {
2897 - usort($group, function($a, $b) {
2898 - $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
2899 - $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
2900 - return $index_a - $index_b;
2901 - });
2902 - }
2903 - unset($group);
2904 -
2905 - // Build HTML for the table rows - must match admin-knowledge-page.php structure exactly
2906 - ob_start();
2907 - $display_index = 0;
2908 - $current_page = $page;
2909 - $data_source = 'pinecone';
2910 - $current_bot_id = $bot_id;
2911 - $preview_length = 150;
2912 -
2913 - if (empty($grouped_prompts)) {
2914 - echo '<tr><td colspan="4" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">';
2915 - esc_html_e('No knowledge entries found in Pinecone.', 'mxchat');
2916 - echo '</td></tr>';
2917 - } else {
2918 - foreach ($grouped_prompts as $source_url => $group) {
2919 - $chunk_count = count($group);
2920 - $first_prompt = $group[0];
2921 - $display_index++;
2922 -
2923 - if ($chunk_count > 1) {
2924 - // Multiple chunks - show grouped row with expand button
2925 - $group_id = 'group-' . md5($source_url);
2926 - ?>
2927 - <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
2928 - class="mxchat-chunk-group-header"
2929 - data-source="<?php echo esc_attr($data_source); ?>"
2930 - data-group-id="<?php echo esc_attr($group_id); ?>"
2931 - style="border-bottom: 1px solid var(--mxch-card-border); background: rgba(33, 150, 243, 0.02);">
2932 - <td style="padding: 12px 16px; text-align: center;">
2933 - <input type="checkbox"
2934 - class="mxchat-entry-checkbox"
2935 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2936 - data-source="<?php echo esc_attr($data_source); ?>"
2937 - data-source-url="<?php echo esc_attr($source_url); ?>"
2938 - data-is-group="true"
2939 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
2940 - </td>
2941 - <td style="padding: 12px 16px; font-size: 13px;">
2942 - <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
2943 - </td>
2944 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
2945 - <div class="mxchat-chunk-group-info">
2946 - <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
2947 - <span class="dashicons dashicons-arrow-right-alt2"></span>
2948 - </button>
2949 - <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
2950 - <span class="mxchat-chunk-preview">
2951 - <?php
2952 - $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : (isset($first_prompt->article_content) ? $first_prompt->article_content : '');
2953 - $content_preview = mb_substr($parent_content, 0, 100);
2954 - echo esc_html($content_preview . '...');
2955 - ?>
2956 - </span>
2957 - </div>
2958 - </td>
2959 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
2960 - <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
2961 - <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
2962 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
2963 - <?php esc_html_e('View Source', 'mxchat'); ?>
2964 - </a>
2965 - <?php else : ?>
2966 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
2967 - <?php endif; ?>
2968 - </td>
2969 - <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
2970 - <button type="button"
2971 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
2972 - data-source-url="<?php echo esc_attr($source_url); ?>"
2973 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2974 - data-data-source="<?php echo esc_attr($data_source); ?>"
2975 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2976 - data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
2977 - title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
2978 - <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
2979 - </button>
2980 - <button type="button"
2981 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
2982 - data-source-url="<?php echo esc_attr($source_url); ?>"
2983 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
2984 - data-data-source="<?php echo esc_attr($data_source); ?>"
2985 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2986 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
2987 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
2988 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
2989 - </button>
2990 - <button type="button"
2991 - class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
2992 - data-source-url="<?php echo esc_attr($source_url); ?>"
2993 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
2994 - data-data-source="<?php echo esc_attr($data_source); ?>"
2995 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
2996 - data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
2997 - style="color: var(--mxch-error);"
2998 - title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
2999 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
3000 - </button>
3001 - </td>
3002 - </tr>
3003 - <?php
3004 - // Render hidden chunk rows
3005 - foreach ($group as $chunk_index => $chunk) {
3006 - $meta_chunk_index = isset($chunk->chunk_metadata['chunk_index']) ? intval($chunk->chunk_metadata['chunk_index']) : $chunk_index;
3007 - $meta_total_chunks = isset($chunk->chunk_metadata['total_chunks']) ? intval($chunk->chunk_metadata['total_chunks']) : $chunk_count;
3008 - $content = isset($chunk->display_content) ? $chunk->display_content : (isset($chunk->article_content) ? $chunk->article_content : '');
3009 - $content_preview = mb_strlen($content) > $preview_length
3010 - ? mb_substr($content, 0, $preview_length) . '...'
3011 - : $content;
3012 - ?>
3013 - <tr id="prompt-<?php echo esc_attr($chunk->id); ?>"
3014 - class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
3015 - data-source="<?php echo esc_attr($data_source); ?>"
3016 - style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
3017 - <td style="padding: 12px 16px; text-align: center;">
3018 - <!-- Checkbox column placeholder for chunks (managed by group) -->
3019 - </td>
3020 - <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
3021 - <!-- Hidden ID column for chunks -->
3022 - </td>
3023 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
3024 - <div class="mxchat-accordion-wrapper">
3025 - <div class="mxchat-content-preview">
3026 - <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
3027 - <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
3028 - </span>
3029 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
3030 - <?php if (mb_strlen($content) > $preview_length) : ?>
3031 - <button class="mxchat-expand-toggle" type="button">
3032 - <span class="dashicons dashicons-arrow-down-alt2"></span>
3033 - </button>
3034 - <?php endif; ?>
3035 - </div>
3036 - <div class="mxchat-content-full" style="display: none;">
3037 - <div class="content-view">
3038 - <?php
3039 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
3040 - echo '<div dir="rtl" lang="he" class="rtl-content">';
3041 - echo wp_kses_post(wpautop($content));
3042 - echo '</div>';
3043 - } else {
3044 - echo wp_kses_post(wpautop($content));
3045 - }
3046 - ?>
3047 - </div>
3048 - </div>
3049 - </div>
3050 - </td>
3051 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
3052 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
3053 - </td>
3054 - <td class="mxchat-actions-cell" style="padding: 12px 16px;">
3055 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
3056 - </td>
3057 - </tr>
3058 - <?php
3059 - }
3060 - } else {
3061 - // Single entry - display normally with accordion
3062 - $prompt = $first_prompt;
3063 - $content = isset($prompt->display_content) ? $prompt->display_content : (isset($prompt->article_content) ? $prompt->article_content : '');
3064 - $content_preview = mb_strlen($content) > $preview_length
3065 - ? mb_substr($content, 0, $preview_length) . '...'
3066 - : $content;
3067 - ?>
3068 - <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
3069 - data-source="<?php echo esc_attr($data_source); ?>"
3070 - style="border-bottom: 1px solid var(--mxch-card-border); background: rgba(33, 150, 243, 0.02);">
3071 - <td style="padding: 12px 16px; text-align: center;">
3072 - <input type="checkbox"
3073 - class="mxchat-entry-checkbox"
3074 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
3075 - data-source="<?php echo esc_attr($data_source); ?>"
3076 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
3077 - data-is-group="false"
3078 - data-chunk-count="1">
3079 - </td>
3080 - <td style="padding: 12px 16px; font-size: 13px;">
3081 - <?php echo esc_html($display_index + (($current_page - 1) * $per_page)); ?>
3082 - </td>
3083 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
3084 - <div class="mxchat-accordion-wrapper">
3085 - <div class="mxchat-content-preview">
3086 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
3087 - <?php if (mb_strlen($content) > $preview_length) : ?>
3088 - <button class="mxchat-expand-toggle" type="button">
3089 - <span class="dashicons dashicons-arrow-down-alt2"></span>
3090 - </button>
3091 - <?php endif; ?>
3092 - </div>
3093 - <div class="mxchat-content-full" style="display: none;">
3094 - <div class="content-view">
3095 - <?php
3096 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
3097 - echo '<div dir="rtl" lang="he" class="rtl-content">';
3098 - echo wp_kses_post(wpautop($content));
3099 - echo '</div>';
3100 - } else {
3101 - echo wp_kses_post(wpautop($content));
3102 - }
3103 - ?>
3104 - </div>
3105 - </div>
3106 - </div>
3107 - </td>
3108 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
3109 - <?php
3110 - $actual_source = $source_url;
3111 - if (strpos($source_url, '_ungrouped_') === 0) {
3112 - $actual_source = $prompt->source_url ?? '';
3113 - }
3114 - if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
3115 - <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
3116 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
3117 - <?php esc_html_e('View', 'mxchat'); ?>
3118 - </a>
3119 - <?php else : ?>
3120 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
3121 - <?php endif; ?>
3122 - </td>
3123 - <td style="padding: 12px 16px; white-space: nowrap;">
3124 - <button type="button"
3125 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
3126 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
3127 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
3128 - data-data-source="<?php echo esc_attr($data_source); ?>"
3129 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
3130 - data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
3131 - title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
3132 - <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
3133 - </button>
3134 - <button type="button"
3135 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
3136 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
3137 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
3138 - data-data-source="<?php echo esc_attr($data_source); ?>"
3139 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
3140 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
3141 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
3142 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
3143 - </button>
3144 - <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);">
3145 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
3146 - </button>
3147 - </td>
3148 - </tr>
3149 - <?php
3150 - }
3151 - }
3152 - }
3153 - $html = ob_get_clean();
3154 -
3155 - // Generate pagination HTML for Pinecone
3156 - $total_pages = ceil($total_records / $per_page);
3157 - $pagination_html = '';
3158 - if ($total_pages > 1) {
3159 - $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) . '">';
3160 -
3161 - // Previous button
3162 - if ($page > 1) {
3163 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page - 1) . '">' . esc_html__('&laquo; Previous', 'mxchat') . '</a> ';
3164 - }
3165 -
3166 - // Page numbers
3167 - $start_page = max(1, $page - 2);
3168 - $end_page = min($total_pages, $page + 2);
3169 -
3170 - if ($start_page > 1) {
3171 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
3172 - if ($start_page > 2) {
3173 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
3174 - }
3175 - }
3176 -
3177 - for ($i = $start_page; $i <= $end_page; $i++) {
3178 - if ($i == $page) {
3179 - $pagination_html .= '<span class="mxchat-page-current">' . $i . '</span> ';
3180 - } else {
3181 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
3182 - }
3183 - }
3184 -
3185 - if ($end_page < $total_pages) {
3186 - if ($end_page < $total_pages - 1) {
3187 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
3188 - }
3189 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
3190 - }
3191 -
3192 - // Next button
3193 - if ($page < $total_pages) {
3194 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page + 1) . '">' . esc_html__('Next &raquo;', 'mxchat') . '</a>';
3195 - }
3196 -
3197 - $pagination_html .= '</div>';
3198 - }
3199 -
3200 - wp_send_json_success(array(
3201 - 'html' => $html,
3202 - 'pagination_html' => $pagination_html,
3203 - 'total_count' => $total_records,
3204 - 'total_pages' => $total_pages,
3205 - 'page' => $page,
3206 - 'per_page' => $per_page,
3207 - 'data_source' => 'pinecone'
3208 - ));
3209 -}
3210 -
3211 -/**
3212 - * AJAX handler for pagination - handles both WordPress DB and Pinecone data sources
3213 - * Returns paginated entries without requiring a full page reload
3214 - */
3215 -public function ajax_mxchat_paginate_entries() {
3216 - check_ajax_referer('mxchat_entries_nonce', 'nonce');
3217 -
3218 - if (!current_user_can('manage_options')) {
3219 - wp_send_json_error(array('message' => 'Unauthorized'));
3220 - return;
3221 - }
3222 -
3223 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
3224 - $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
3225 - $search_query = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
3226 - $content_type_filter = isset($_POST['content_type']) ? sanitize_key($_POST['content_type']) : '';
3227 - $per_page = 25;
3228 -
3229 - // Check if Pinecone is enabled for this bot
3230 - $pinecone_manager = $this->mxchat_get_pinecone_manager();
3231 - $pinecone_options = $pinecone_manager ? $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id) : array();
3232 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3233 - $has_pinecone_api = !empty($pinecone_options['mxchat_pinecone_api_key']);
3234 -
3235 - if ($use_pinecone && $has_pinecone_api) {
3236 - // Delegate to Pinecone pagination handler (pass search params)
3237 - $_POST['page'] = $page;
3238 - $_POST['search'] = $search_query;
3239 - $_POST['content_type'] = $content_type_filter;
3240 - $this->ajax_mxchat_refresh_pinecone_entries();
3241 - return;
3242 - }
3243 -
3244 - // WordPress DB pagination - MUST match initial page load logic exactly
3245 - global $wpdb;
3246 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3247 - $offset = ($page - 1) * $per_page;
3248 -
3249 - // Build WHERE clause for search and content type filtering
3250 - $where_clauses = array();
3251 - $where_values = array();
3252 -
3253 - if ($search_query) {
3254 - $where_clauses[] = "article_content LIKE %s";
3255 - $where_values[] = '%' . $wpdb->esc_like($search_query) . '%';
3256 - }
3257 -
3258 - if ($content_type_filter) {
3259 - switch ($content_type_filter) {
3260 - case 'manual':
3261 - $where_clauses[] = "(source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')";
3262 - break;
3263 - case 'pdf':
3264 - $where_clauses[] = "source_url LIKE '%.pdf'";
3265 - break;
3266 - case 'url':
3267 - $where_clauses[] = "source_url != '' AND source_url IS NOT NULL AND source_url NOT LIKE 'mxchat://%' AND source_url NOT LIKE '%.pdf'";
3268 - break;
3269 - }
3270 - }
3271 -
3272 - $where_sql = !empty($where_clauses) ? 'WHERE ' . implode(' AND ', $where_clauses) : '';
3273 -
3274 - // Count grouped entries with filters applied
3275 - if (!empty($where_values)) {
3276 - $count_args = array_merge($where_values, $where_values);
3277 - $count_query = $wpdb->prepare(
3278 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} {$where_sql} AND source_url != '' AND source_url NOT LIKE 'mxchat://%') +
3279 - (SELECT COUNT(*) FROM {$table_name} {$where_sql} AND (source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%'))",
3280 - ...$count_args
3281 - );
3282 - $total_records = $wpdb->get_var($count_query);
3283 - } else if (!empty($where_sql)) {
3284 - // Content type filter only (no search), no prepared values needed
3285 - $total_records = $wpdb->get_var(
3286 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} {$where_sql} AND source_url != '' AND source_url NOT LIKE 'mxchat://%') +
3287 - (SELECT COUNT(*) FROM {$table_name} {$where_sql} AND (source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%'))"
3288 - );
3289 - } else {
3290 - // No filters
3291 - $total_records = $wpdb->get_var(
3292 - "SELECT (SELECT COUNT(DISTINCT source_url) FROM {$table_name} WHERE source_url != '' AND source_url NOT LIKE 'mxchat://%') +
3293 - (SELECT COUNT(*) FROM {$table_name} WHERE source_url = '' OR source_url IS NULL OR source_url LIKE 'mxchat://%')"
3294 - );
3295 - }
3296 - $total_pages = ceil($total_records / $per_page);
3297 -
3298 - // Step 1: Get unique source_urls for this page (ordered by latest timestamp) with filters
3299 - if (!empty($where_values)) {
3300 - $query_args = array_merge($where_values, array($per_page, $offset));
3301 - $urls_query = $wpdb->prepare(
3302 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
3303 - {$where_sql}
3304 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
3305 - ...$query_args
3306 - );
3307 - } else if (!empty($where_sql)) {
3308 - $urls_query = $wpdb->prepare(
3309 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
3310 - {$where_sql}
3311 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
3312 - $per_page, $offset
3313 - );
3314 - } else {
3315 - $urls_query = $wpdb->prepare(
3316 - "SELECT source_url, MAX(timestamp) as latest_ts FROM {$table_name}
3317 - GROUP BY source_url ORDER BY latest_ts DESC LIMIT %d OFFSET %d",
3318 - $per_page, $offset
3319 - );
3320 - }
3321 - $page_urls = $wpdb->get_results($urls_query);
3322 -
3323 - // Step 2: Build list of source_urls to fetch
3324 - $url_list = array();
3325 - $url_order_map = array();
3326 - $order_index = 0;
3327 - foreach ($page_urls as $url_row) {
3328 - $url = $url_row->source_url;
3329 - $url_list[] = $url;
3330 - $url_order_map[$url] = $order_index++;
3331 - }
3332 -
3333 - // Step 3: Fetch all rows for these source_urls (with search filter if applicable)
3334 - $prompts = array();
3335 - if (!empty($url_list)) {
3336 - $placeholders = implode(',', array_fill(0, count($url_list), '%s'));
3337 - if ($search_query) {
3338 - // Include search filter in the final fetch
3339 - $prompts_query = $wpdb->prepare(
3340 - "SELECT id, article_content, source_url, timestamp, role_restriction
3341 - FROM {$table_name}
3342 - WHERE source_url IN ($placeholders) AND article_content LIKE %s
3343 - ORDER BY timestamp DESC",
3344 - ...array_merge($url_list, ['%' . $wpdb->esc_like($search_query) . '%'])
3345 - );
3346 - } else {
3347 - $prompts_query = $wpdb->prepare(
3348 - "SELECT id, article_content, source_url, timestamp, role_restriction
3349 - FROM {$table_name}
3350 - WHERE source_url IN ($placeholders)
3351 - ORDER BY timestamp DESC",
3352 - $url_list
3353 - );
3354 - }
3355 - $prompts = $wpdb->get_results($prompts_query);
3356 - }
3357 -
3358 - // Group prompts by source_url for chunk display
3359 - $grouped_prompts = array();
3360 - foreach ($prompts as $prompt) {
3361 - $source_url = $prompt->source_url ?? '';
3362 -
3363 - // Parse chunk metadata using the proper chunker method (same as initial page load)
3364 - if (class_exists('MxChat_Chunker')) {
3365 - $chunk_meta = MxChat_Chunker::parse_stored_chunk($prompt->article_content);
3366 - $prompt->chunk_metadata = $chunk_meta['metadata'];
3367 - $prompt->display_content = $chunk_meta['text'];
3368 - } else {
3369 - $prompt->chunk_metadata = array();
3370 - $prompt->display_content = $prompt->article_content;
3371 - }
3372 -
3373 - if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0) {
3374 - if (!isset($grouped_prompts[$source_url])) {
3375 - $grouped_prompts[$source_url] = array();
3376 - }
3377 - $grouped_prompts[$source_url][] = $prompt;
3378 - } else {
3379 - // Ungrouped entries
3380 - $grouped_prompts['_ungrouped_' . $prompt->id] = array($prompt);
3381 - }
3382 - }
3383 -
3384 - // Sort groups by the original URL order (newest first)
3385 - uksort($grouped_prompts, function($a, $b) use ($url_order_map) {
3386 - $order_a = isset($url_order_map[$a]) ? $url_order_map[$a] : PHP_INT_MAX;
3387 - $order_b = isset($url_order_map[$b]) ? $url_order_map[$b] : PHP_INT_MAX;
3388 - return $order_a - $order_b;
3389 - });
3390 -
3391 - // Sort each group internally by chunk_index
3392 - foreach ($grouped_prompts as $source_url => &$group) {
3393 - usort($group, function($a, $b) {
3394 - $index_a = isset($a->chunk_metadata['chunk_index']) ? intval($a->chunk_metadata['chunk_index']) : 0;
3395 - $index_b = isset($b->chunk_metadata['chunk_index']) ? intval($b->chunk_metadata['chunk_index']) : 0;
3396 - return $index_a - $index_b;
3397 - });
3398 - }
3399 - unset($group);
3400 -
3401 - // Build HTML for the table rows
3402 - ob_start();
3403 - $display_index = 0;
3404 - $current_page = $page;
3405 - $data_source = 'wordpress';
3406 - $current_bot_id = $bot_id;
3407 - $preview_length = 150;
3408 -
3409 - if (empty($grouped_prompts)) {
3410 - echo '<tr><td colspan="5" style="padding: 40px; text-align: center; color: var(--mxch-text-muted);">';
3411 - esc_html_e('No knowledge entries found. Use the Import Options to add content.', 'mxchat');
3412 - echo '</td></tr>';
3413 - } else {
3414 - foreach ($grouped_prompts as $source_url => $group) {
3415 - $chunk_count = count($group);
3416 - $first_prompt = $group[0];
3417 - $display_index++;
3418 -
3419 - if ($chunk_count > 1) {
3420 - // Multiple chunks - show grouped row with expand button
3421 - $group_id = 'group-' . md5($source_url);
3422 - ?>
3423 - <tr id="prompt-<?php echo esc_attr($first_prompt->id); ?>"
3424 - class="mxchat-chunk-group-header"
3425 - data-source="<?php echo esc_attr($data_source); ?>"
3426 - data-group-id="<?php echo esc_attr($group_id); ?>"
3427 - style="border-bottom: 1px solid var(--mxch-card-border);">
3428 - <td style="padding: 12px 16px; text-align: center;">
3429 - <input type="checkbox"
3430 - class="mxchat-entry-checkbox"
3431 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
3432 - data-source="<?php echo esc_attr($data_source); ?>"
3433 - data-source-url="<?php echo esc_attr($source_url); ?>"
3434 - data-is-group="true"
3435 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>">
3436 - </td>
3437 - <td style="padding: 12px 16px; font-size: 13px;">
3438 - <?php echo esc_html($first_prompt->id); ?>
3439 - </td>
3440 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
3441 - <div class="mxchat-chunk-group-info">
3442 - <button type="button" class="mxchat-chunk-toggle" data-group-id="<?php echo esc_attr($group_id); ?>">
3443 - <span class="dashicons dashicons-arrow-right-alt2"></span>
3444 - </button>
3445 - <span class="mxchat-chunk-badge"><?php echo esc_html($chunk_count); ?> <?php esc_html_e('chunks', 'mxchat'); ?></span>
3446 - <span class="mxchat-chunk-preview">
3447 - <?php
3448 - $parent_content = isset($first_prompt->display_content) ? $first_prompt->display_content : $first_prompt->article_content;
3449 - $content_preview = mb_substr($parent_content, 0, 100);
3450 - echo esc_html($content_preview . '...');
3451 - ?>
3452 - </span>
3453 - </div>
3454 - </td>
3455 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
3456 - <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
3457 - <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
3458 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
3459 - <?php esc_html_e('View Source', 'mxchat'); ?>
3460 - </a>
3461 - <?php else : ?>
3462 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
3463 - <?php endif; ?>
3464 - </td>
3465 - <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
3466 - <?php if ($data_source !== 'pinecone') : ?>
3467 - <button type="button"
3468 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
3469 - data-source-url="<?php echo esc_attr($source_url); ?>"
3470 - data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
3471 - data-data-source="<?php echo esc_attr($data_source); ?>"
3472 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
3473 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
3474 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
3475 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
3476 - </button>
3477 - <?php endif; ?>
3478 - <button type="button"
3479 - class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
3480 - data-source-url="<?php echo esc_attr($source_url); ?>"
3481 - data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
3482 - data-data-source="<?php echo esc_attr($data_source); ?>"
3483 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
3484 - data-nonce="<?php echo wp_create_nonce('mxchat_delete_chunks_nonce'); ?>"
3485 - style="color: var(--mxch-error);"
3486 - title="<?php esc_attr_e('Delete all chunks', 'mxchat'); ?>">
3487 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
3488 - </button>
3489 - </td>
3490 - </tr>
3491 - <?php
3492 - // Render hidden chunk rows
3493 - foreach ($group as $chunk_index => $chunk) {
3494 - $meta_chunk_index = isset($chunk->chunk_metadata['chunk_index']) ? intval($chunk->chunk_metadata['chunk_index']) : $chunk_index;
3495 - $meta_total_chunks = isset($chunk->chunk_metadata['total_chunks']) ? intval($chunk->chunk_metadata['total_chunks']) : $chunk_count;
3496 - $content = isset($chunk->display_content) ? $chunk->display_content : $chunk->article_content;
3497 - $content_preview = mb_strlen($content) > $preview_length
3498 - ? mb_substr($content, 0, $preview_length) . '...'
3499 - : $content;
3500 - ?>
3501 - <tr id="prompt-<?php echo esc_attr($chunk->id); ?>"
3502 - class="mxchat-chunk-row <?php echo esc_attr($group_id); ?>"
3503 - data-source="<?php echo esc_attr($data_source); ?>"
3504 - style="display: none; background: #f8f9fa; border-bottom: 1px solid var(--mxch-card-border);">
3505 - <td style="padding: 12px 16px; text-align: center;">
3506 - <!-- Checkbox column placeholder for chunks (managed by group) -->
3507 - </td>
3508 - <td style="padding: 12px 16px 12px 30px; font-size: 13px;">
3509 - <!-- Hidden ID column for chunks -->
3510 - </td>
3511 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
3512 - <div class="mxchat-accordion-wrapper">
3513 - <div class="mxchat-content-preview">
3514 - <span class="mxchat-chunk-indicator" style="margin-right: 10px; color: var(--mxch-text-secondary); font-size: 12px;">
3515 - <?php printf(esc_html__('Chunk %d of %d', 'mxchat'), $meta_chunk_index + 1, $meta_total_chunks); ?>
3516 - </span>
3517 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
3518 - <?php if (mb_strlen($content) > $preview_length) : ?>
3519 - <button class="mxchat-expand-toggle" type="button">
3520 - <span class="dashicons dashicons-arrow-down-alt2"></span>
3521 - </button>
3522 - <?php endif; ?>
3523 - </div>
3524 - <div class="mxchat-content-full" style="display: none;">
3525 - <div class="content-view">
3526 - <?php
3527 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
3528 - echo '<div dir="rtl" lang="he" class="rtl-content">';
3529 - echo wp_kses_post(wpautop($content));
3530 - echo '</div>';
3531 - } else {
3532 - echo wp_kses_post(wpautop($content));
3533 - }
3534 - ?>
3535 - </div>
3536 - </div>
3537 - </div>
3538 - </td>
3539 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
3540 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Same as parent', 'mxchat'); ?></span>
3541 - </td>
3542 - <td class="mxchat-actions-cell" style="padding: 12px 16px;">
3543 - <span class="mxchat-chunk-label" style="color: var(--mxch-text-muted);"><?php esc_html_e('Managed by group', 'mxchat'); ?></span>
3544 - </td>
3545 - </tr>
3546 - <?php
3547 - }
3548 - } else {
3549 - // Single entry - display normally with accordion
3550 - $prompt = $first_prompt;
3551 - $content = isset($prompt->display_content) ? $prompt->display_content : $prompt->article_content;
3552 - $content_preview = mb_strlen($content) > $preview_length
3553 - ? mb_substr($content, 0, $preview_length) . '...'
3554 - : $content;
3555 - ?>
3556 - <tr id="prompt-<?php echo esc_attr($prompt->id); ?>"
3557 - data-source="<?php echo esc_attr($data_source); ?>"
3558 - style="border-bottom: 1px solid var(--mxch-card-border);">
3559 - <td style="padding: 12px 16px; text-align: center;">
3560 - <input type="checkbox"
3561 - class="mxchat-entry-checkbox"
3562 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
3563 - data-source="<?php echo esc_attr($data_source); ?>"
3564 - data-source-url="<?php echo esc_attr($source_url); ?>"
3565 - data-is-group="false">
3566 - </td>
3567 - <td style="padding: 12px 16px; font-size: 13px;">
3568 - <?php echo esc_html($prompt->id); ?>
3569 - </td>
3570 - <td class="mxchat-content-cell" style="padding: 12px 16px; font-size: 13px;">
3571 - <div class="mxchat-accordion-wrapper">
3572 - <div class="mxchat-content-preview">
3573 - <span class="preview-text"><?php echo esc_html($content_preview); ?></span>
3574 - <?php if (mb_strlen($content) > $preview_length) : ?>
3575 - <button class="mxchat-expand-toggle" type="button">
3576 - <span class="dashicons dashicons-arrow-down-alt2"></span>
3577 - </button>
3578 - <?php endif; ?>
3579 - </div>
3580 - <div class="mxchat-content-full" style="display: none;">
3581 - <div class="content-view">
3582 - <?php
3583 - if (preg_match('/[\x{0590}-\x{05FF}]/u', $content)) {
3584 - echo '<div dir="rtl" lang="he" class="rtl-content">';
3585 - echo wp_kses_post(wpautop($content));
3586 - echo '</div>';
3587 - } else {
3588 - echo wp_kses_post(wpautop($content));
3589 - }
3590 - ?>
3591 - </div>
3592 - </div>
3593 - </div>
3594 - </td>
3595 - <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
3596 - <?php
3597 - $actual_source = $source_url;
3598 - if (strpos($source_url, '_ungrouped_') === 0) {
3599 - $actual_source = $prompt->source_url ?? '';
3600 - }
3601 - if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
3602 - <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
3603 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
3604 - <?php esc_html_e('View', 'mxchat'); ?>
3605 - </a>
3606 - <?php else : ?>
3607 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
3608 - <?php endif; ?>
3609 - </td>
3610 - <td style="padding: 12px 16px; white-space: nowrap;">
3611 - <button type="button"
3612 - class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
3613 - data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
3614 - data-entry-id="<?php echo esc_attr($prompt->id); ?>"
3615 - data-data-source="<?php echo esc_attr($data_source); ?>"
3616 - data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
3617 - data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
3618 - title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
3619 - <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
3620 - </button>
3621 - <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);">
3622 - <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
3623 - </button>
3624 - </td>
3625 - </tr>
3626 - <?php
3627 - }
3628 - }
3629 - }
3630 - $html = ob_get_clean();
3631 -
3632 - // Generate pagination HTML (include search/filter data for subsequent pages)
3633 - $pagination_html = '';
3634 - if ($total_pages > 1) {
3635 - $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) . '">';
3636 -
3637 - // Previous button
3638 - if ($page > 1) {
3639 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page - 1) . '">' . esc_html__('&laquo; Previous', 'mxchat') . '</a> ';
3640 - }
3641 -
3642 - // Page numbers
3643 - $start_page = max(1, $page - 2);
3644 - $end_page = min($total_pages, $page + 2);
3645 -
3646 - if ($start_page > 1) {
3647 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="1">1</a> ';
3648 - if ($start_page > 2) {
3649 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
3650 - }
3651 - }
3652 -
3653 - for ($i = $start_page; $i <= $end_page; $i++) {
3654 - if ($i == $page) {
3655 - $pagination_html .= '<span class="mxchat-page-current">' . $i . '</span> ';
3656 - } else {
3657 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $i . '">' . $i . '</a> ';
3658 - }
3659 - }
3660 -
3661 - if ($end_page < $total_pages) {
3662 - if ($end_page < $total_pages - 1) {
3663 - $pagination_html .= '<span class="mxchat-page-dots">...</span> ';
3664 - }
3665 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . $total_pages . '">' . $total_pages . '</a> ';
3666 - }
3667 -
3668 - // Next button
3669 - if ($page < $total_pages) {
3670 - $pagination_html .= '<a href="#" class="mxchat-page-link" data-page="' . ($page + 1) . '">' . esc_html__('Next &raquo;', 'mxchat') . '</a>';
3671 - }
3672 -
3673 - $pagination_html .= '</div>';
3674 - }
3675 -
3676 - wp_send_json_success(array(
3677 - 'html' => $html,
3678 - 'pagination_html' => $pagination_html,
3679 - 'total_count' => $total_records,
3680 - 'total_pages' => $total_pages,
3681 - 'page' => $page,
3682 - 'per_page' => $per_page,
3683 - 'data_source' => 'wordpress'
3684 - ));
3685 -}
3686 -
3687 -/**
3688 - * AJAX handler to detect available sitemaps on the site
3689 - * Optimized for speed - only checks primary sitemap indexes first
3690 - */
3691 -public function ajax_mxchat_detect_sitemaps() {
3692 - check_ajax_referer('mxchat_detect_sitemaps_nonce', 'nonce');
3693 -
3694 - if (!current_user_can('manage_options')) {
3695 - wp_send_json_error(array('message' => 'Unauthorized'));
3696 - return;
3697 - }
3698 -
3699 - $site_url = get_site_url();
3700 - $sitemaps = array();
3701 - $found_index = false;
3702 -
3703 - // Only check the main sitemap index files first (much faster)
3704 - // These are the primary entry points that contain sub-sitemaps
3705 - $primary_indexes = array(
3706 - 'sitemap_index.xml' => 'Yoast SEO / Rank Math', // Yoast & Rank Math
3707 - 'wp-sitemap.xml' => 'WordPress Core', // WordPress Core
3708 - 'sitemap.xml' => 'Standard', // Generic/AIOSEO
3709 - );
3710 -
3711 - foreach ($primary_indexes as $path => $source) {
3712 - $url = trailingslashit($site_url) . $path;
3713 -
3714 - $response = wp_remote_head($url, array(
3715 - 'timeout' => 10,
3716 - 'sslverify' => false,
3717 - 'redirection' => 1,
3718 - 'user-agent' => mxchat_ingest_user_agent(),
3719 - ));
3720 -
3721 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
3722 - // Found a sitemap index - parse it to get sub-sitemaps
3723 - $sub_sitemaps = $this->parse_sitemap_index($url);
3724 - if (!empty($sub_sitemaps)) {
3725 - $sitemaps[] = array(
3726 - 'url' => $url,
3727 - 'type' => 'index',
3728 - 'source' => $source,
3729 - 'sub_sitemaps' => $sub_sitemaps
3730 - );
3731 - $found_index = true;
3732 - // Found a valid index, no need to check others
3733 - break;
3734 - }
3735 - }
3736 - }
3737 -
3738 - // If no sitemap index found, check for standalone sitemaps
3739 - if (!$found_index) {
3740 - $standalone_sitemaps = array(
3741 - 'post-sitemap.xml' => array('type' => 'content', 'source' => 'Yoast SEO'),
3742 - 'page-sitemap.xml' => array('type' => 'content', 'source' => 'Yoast SEO'),
3743 - );
3744 -
3745 - foreach ($standalone_sitemaps as $path => $info) {
3746 - $url = trailingslashit($site_url) . $path;
3747 -
3748 - $response = wp_remote_head($url, array(
3749 - 'timeout' => 2,
3750 - 'sslverify' => false
3751 - ));
3752 -
3753 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
3754 - $sitemaps[] = array(
3755 - 'url' => $url,
3756 - 'type' => $info['type'],
3757 - 'source' => $info['source'],
3758 - 'url_count' => 0 // Skip URL count for speed
3759 - );
3760 - }
3761 - }
3762 - }
3763 -
3764 - wp_send_json_success(array(
3765 - 'sitemaps' => $sitemaps,
3766 - 'site_url' => $site_url
3767 - ));
3768 -}
3769 -
3770 -/**
3771 - * Parse a sitemap index to get sub-sitemaps
3772 - * Optimized: doesn't fetch URL count for each sub-sitemap (too slow)
3773 - */
3774 -private function parse_sitemap_index($url) {
3775 - $sub_sitemaps = array();
3776 -
3777 - $response = wp_remote_get($url, array(
3778 - 'timeout' => 30,
3779 - 'sslverify' => false,
3780 - 'user-agent' => mxchat_ingest_user_agent(),
3781 - 'headers' => array(
3782 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
3783 - ),
3784 - ));
3785 -
3786 - if (is_wp_error($response)) {
3787 - return $sub_sitemaps;
3788 - }
3789 -
3790 - $body = wp_remote_retrieve_body($response);
3791 - if (empty($body)) {
3792 - return $sub_sitemaps;
3793 - }
3794 -
3795 - // Suppress XML errors
3796 - libxml_use_internal_errors(true);
3797 - $xml = simplexml_load_string($body);
3798 - libxml_clear_errors();
3799 -
3800 - if ($xml === false) {
3801 - return $sub_sitemaps;
3802 - }
3803 -
3804 - // Check if it's a sitemap index (contains <sitemap> elements)
3805 - if (isset($xml->sitemap)) {
3806 - foreach ($xml->sitemap as $sitemap) {
3807 - $loc = (string) $sitemap->loc;
3808 - if (!empty($loc)) {
3809 - // Try to determine the type from the URL
3810 - $type = 'content';
3811 - if (strpos($loc, 'category') !== false || strpos($loc, 'tag') !== false || strpos($loc, 'taxonomy') !== false) {
3812 - $type = 'taxonomy';
3813 - } elseif (strpos($loc, 'author') !== false || strpos($loc, 'user') !== false) {
3814 - $type = 'author';
3815 - }
3816 -
3817 - // Skip URL count - too slow to fetch for each sitemap
3818 - $sub_sitemaps[] = array(
3819 - 'url' => $loc,
3820 - 'type' => $type,
3821 - 'url_count' => 0, // Don't fetch - takes too long
3822 - 'name' => basename(parse_url($loc, PHP_URL_PATH))
3823 - );
3824 - }
3825 - }
3826 - }
3827 -
3828 - return $sub_sitemaps;
3829 -}
3830 -
3831 -/**
3832 - * Get URL count from a sitemap
3833 - */
3834 -private function get_sitemap_url_count($url) {
3835 - $response = wp_remote_get($url, array(
3836 - 'timeout' => 30,
3837 - 'sslverify' => false,
3838 - 'user-agent' => mxchat_ingest_user_agent(),
3839 - 'headers' => array(
3840 - 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
3841 - ),
3842 - ));
3843 -
3844 - if (is_wp_error($response)) {
3845 - return 0;
3846 - }
3847 -
3848 - $body = wp_remote_retrieve_body($response);
3849 - if (empty($body)) {
3850 - return 0;
3851 - }
3852 -
3853 - // Count <url> or <loc> elements
3854 - $count = preg_match_all('/<url>/i', $body, $matches);
3855 - return $count ?: 0;
3856 -}
3857 -
3858 -/**
3859 - * Get sitemaps declared in robots.txt
3860 - */
3861 -private function get_sitemaps_from_robots($site_url) {
3862 - $sitemaps = array();
3863 - $robots_url = trailingslashit($site_url) . 'robots.txt';
3864 -
3865 - $response = wp_remote_get($robots_url, array(
3866 - 'timeout' => 15,
3867 - 'sslverify' => false,
3868 - 'user-agent' => mxchat_ingest_user_agent(),
3869 - ));
3870 -
3871 - if (is_wp_error($response)) {
3872 - return $sitemaps;
3873 - }
3874 -
3875 - $body = wp_remote_retrieve_body($response);
3876 - if (empty($body)) {
3877 - return $sitemaps;
3878 - }
3879 -
3880 - // Find Sitemap: declarations
3881 - if (preg_match_all('/^Sitemap:\s*(.+)$/mi', $body, $matches)) {
3882 - foreach ($matches[1] as $sitemap_url) {
3883 - $sitemap_url = trim($sitemap_url);
3884 - if (filter_var($sitemap_url, FILTER_VALIDATE_URL)) {
3885 - $sitemaps[] = $sitemap_url;
3886 - }
3887 - }
3888 - }
3889 -
3890 - return $sitemaps;
3891 -}
3892 -
3893 -public function mxchat_stop_processing() {
3894 - // Verify permissions
3895 - if (!current_user_can('manage_options')) {
3896 - wp_die(esc_html__('Unauthorized access', 'mxchat'));
3897 - }
3898 -
3899 - // Verify nonce
3900 - check_admin_referer('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce');
3901 -
3902 - global $wpdb;
3903 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
3904 -
3905 - // Get active queue IDs
3906 - $sitemap_queue_id = get_transient('mxchat_active_queue_sitemap');
3907 - $pdf_queue_id = get_transient('mxchat_active_queue_pdf');
3908 -
3909 - // Delete all pending items from active queues
3910 - if ($sitemap_queue_id) {
3911 - $wpdb->delete(
3912 - $table_name,
3913 - array(
3914 - 'queue_id' => $sitemap_queue_id,
3915 - 'status' => 'pending'
3916 - ),
3917 - array('%s', '%s')
3918 - );
3919 -
3920 - delete_transient('mxchat_active_queue_sitemap');
3921 - delete_transient('mxchat_last_sitemap_url');
3922 - }
3923 -
3924 - if ($pdf_queue_id) {
3925 - // Get PDF path before deleting
3926 - $pdf_path = $this->mxchat_get_queue_meta($pdf_queue_id, 'pdf_path');
3927 -
3928 - $wpdb->delete(
3929 - $table_name,
3930 - array(
3931 - 'queue_id' => $pdf_queue_id,
3932 - 'status' => 'pending'
3933 - ),
3934 - array('%s', '%s')
3935 - );
3936 -
3937 - // Delete PDF file
3938 - if ($pdf_path && file_exists($pdf_path)) {
3939 - wp_delete_file($pdf_path);
3940 - }
3941 -
3942 - delete_transient('mxchat_active_queue_pdf');
3943 - delete_transient('mxchat_last_pdf_url');
3944 - }
3945 -
3946 - // Redirect back with a success message
3947 - set_transient('mxchat_admin_notice_success',
3948 - esc_html__('Processing has been stopped successfully.', 'mxchat'),
3949 - 30
3950 - );
3951 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
3952 - exit;
3953 -}
3954 -
3955 -/**
3956 - * Get content list for processing
3957 - */
3958 -public function ajax_mxchat_get_content_list() {
3959 - // Verify the nonce
3960 - check_ajax_referer('mxchat_content_selector_nonce', 'nonce');
3961 -
3962 - if (!current_user_can('manage_options')) {
3963 - wp_send_json_error(__('Unauthorized access', 'mxchat'));
3964 - }
3965 -
3966 - $page = isset($_GET['page']) ? absint($_GET['page']) : 1;
3967 - $per_page = isset($_GET['per_page']) ? absint($_GET['per_page']) : 100;
3968 - $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
3969 - $post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'all';
3970 - $post_status = isset($_GET['post_status']) ? sanitize_text_field($_GET['post_status']) : 'publish';
3971 - $processed_filter = isset($_GET['processed_filter']) ? sanitize_text_field($_GET['processed_filter']) : 'all';
3972 -
3973 - // Build query args
3974 - $args = array(
3975 - 'posts_per_page' => $per_page,
3976 - 'paged' => $page,
3977 - 'post_status' => $post_status !== 'all' ? $post_status : array('publish', 'draft', 'pending'),
3978 - 'orderby' => 'date',
3979 - 'order' => 'DESC',
3980 - );
3981 -
3982 - // Handle post types - IMPROVED VERSION
3983 - if ($post_type !== 'all') {
3984 - $args['post_type'] = $post_type;
3985 - } else {
3986 - // Get all available post types that might contain content
3987 - $all_post_types = array();
3988 -
3989 - // First get all public post types
3990 - $public_types = get_post_types(array('public' => true), 'names');
3991 - $all_post_types = array_merge($all_post_types, $public_types);
3992 -
3993 - // Add common forum/community post types
3994 - $forum_types = array('topic', 'reply', 'forum', 'wpforo_topic', 'wpforo_post');
3995 - foreach ($forum_types as $forum_type) {
3996 - if (post_type_exists($forum_type)) {
3997 - $all_post_types[] = $forum_type;
3998 - }
3999 - }
4000 -
4001 - // Add other commonly used post types
4002 - $common_types = array('product', 'job_listing', 'event', 'portfolio');
4003 - foreach ($common_types as $common_type) {
4004 - if (post_type_exists($common_type)) {
4005 - $all_post_types[] = $common_type;
4006 - }
4007 - }
4008 -
4009 - // Remove duplicates and ensure we have at least some post types
4010 - $all_post_types = array_unique($all_post_types);
4011 -
4012 - if (empty($all_post_types)) {
4013 - // Fallback to basic post types
4014 - $all_post_types = array('post', 'page');
4015 - }
4016 -
4017 - $args['post_type'] = $all_post_types;
4018 -
4019 - // Debug logging to see what post types are being queried
4020 - //error_log('MxChat Debug: Querying post types: ' . implode(', ', $all_post_types));
4021 - }
4022 -
4023 - if (!empty($search)) {
4024 - $args['s'] = $search;
4025 - }
4026 -
4027 - // Get processed data from storage
4028 - $processed_data = array();
4029 -
4030 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4031 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4032 -
4033 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4034 - // Get fresh data from Pinecone - no caching
4035 - $processed_data = $this->mxchat_get_pinecone_processed_content($pinecone_options);
4036 - } else {
4037 - // WordPress DB checking with better URL matching for all post types
4038 - global $wpdb;
4039 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4040 - $processed_items = $wpdb->get_results("SELECT id, source_url, article_content, timestamp FROM {$table_name}");
4041 -
4042 - // Group items by source_url to count chunks
4043 - $url_chunk_counts = array();
4044 - $url_latest_timestamp = array();
4045 - $url_first_id = array();
4046 -
4047 - if (!empty($processed_items)) {
4048 - foreach ($processed_items as $item) {
4049 - $url = $item->source_url;
4050 - if (empty($url)) continue;
4051 -
4052 - // Count chunks per URL
4053 - if (!isset($url_chunk_counts[$url])) {
4054 - $url_chunk_counts[$url] = 0;
4055 - $url_latest_timestamp[$url] = $item->timestamp;
4056 - $url_first_id[$url] = $item->id;
4057 - }
4058 - $url_chunk_counts[$url]++;
4059 -
4060 - // Track latest timestamp
4061 - if (strtotime($item->timestamp) > strtotime($url_latest_timestamp[$url])) {
4062 - $url_latest_timestamp[$url] = $item->timestamp;
4063 - }
4064 - }
4065 -
4066 - // Now build processed_data with chunk counts
4067 - foreach ($url_chunk_counts as $url => $chunk_count) {
4068 - $post_id = $this->mxchat_url_to_post_id_improved($url);
4069 -
4070 - if ($post_id) {
4071 - $processed_data[$post_id] = array(
4072 - 'db_id' => $url_first_id[$url],
4073 - 'timestamp' => $url_latest_timestamp[$url],
4074 - 'url' => $url,
4075 - 'source' => 'wordpress',
4076 - 'chunk_count' => $chunk_count
4077 - );
4078 - }
4079 - }
4080 - }
4081 - }
4082 -
4083 - // Get processed IDs as a simple array for in_array checks
4084 - $processed_ids = array_keys($processed_data);
4085 -
4086 - // Handle processed/unprocessed filter
4087 - if ($processed_filter === 'processed' && !empty($processed_ids)) {
4088 - $args['post__in'] = $processed_ids;
4089 - } elseif ($processed_filter === 'unprocessed' && !empty($processed_ids)) {
4090 - $args['post__not_in'] = $processed_ids;
4091 - }
4092 -
4093 - // Run the query
4094 - $query = new WP_Query($args);
4095 - $content_items = array();
4096 -
4097 - if ($query->have_posts()) {
4098 - while ($query->have_posts()) {
4099 - $query->the_post();
4100 - $id = get_the_ID();
4101 - $post_date = get_the_date();
4102 - $excerpt = wp_trim_words(get_the_excerpt(), 20, '...');
4103 - $word_count = str_word_count(strip_tags(get_the_content()));
4104 -
4105 - $is_processed = in_array($id, $processed_ids);
4106 - $processed_date = '';
4107 - $db_record_id = 0;
4108 - $data_source = 'none';
4109 -
4110 - if ($is_processed && isset($processed_data[$id])) {
4111 - $item_data = $processed_data[$id];
4112 - $data_source = $item_data['source'];
4113 -
4114 - if ($data_source === 'wordpress' && isset($item_data['timestamp'])) {
4115 - // WordPress DB format
4116 - $timestamp = strtotime($item_data['timestamp']);
4117 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
4118 - $db_record_id = $item_data['db_id'];
4119 - } elseif ($data_source === 'pinecone') {
4120 - // Pinecone format
4121 - $processed_date = $item_data['processed_date'];
4122 - $db_record_id = $item_data['db_id'];
4123 - }
4124 - }
4125 -
4126 - // Get chunk count for this item
4127 - $chunk_count = 0;
4128 - if ($is_processed && isset($processed_data[$id]['chunk_count'])) {
4129 - $chunk_count = intval($processed_data[$id]['chunk_count']);
4130 - }
4131 -
4132 - $content_items[] = array(
4133 - 'id' => $id,
4134 - 'title' => get_the_title(),
4135 - 'permalink' => get_permalink(),
4136 - 'date' => $post_date,
4137 - 'type' => get_post_type(),
4138 - 'status' => get_post_status(),
4139 - 'excerpt' => $excerpt,
4140 - 'word_count' => $word_count,
4141 - 'already_processed' => $is_processed,
4142 - 'processed_date' => $processed_date,
4143 - 'db_record_id' => $db_record_id,
4144 - 'data_source' => $data_source,
4145 - 'chunk_count' => $chunk_count
4146 - );
4147 - }
4148 - wp_reset_postdata();
4149 - }
4150 -
4151 - $response = array(
4152 - 'items' => $content_items,
4153 - 'total' => $query->found_posts,
4154 - 'total_pages' => $query->max_num_pages,
4155 - 'current_page' => $page,
4156 - 'processed_count' => count($processed_ids)
4157 - );
4158 -
4159 - wp_send_json_success($response);
4160 - exit;
4161 -}
4162 -
4163 -
4164 -/**
4165 - * This function handles various WooCommerce URL formats and permalink structures
4166 - */
4167 -private function mxchat_url_to_post_id_improved($url) {
4168 - // First try the standard WordPress function
4169 - $post_id = url_to_postid($url);
4170 -
4171 - if ($post_id > 0) {
4172 - return $post_id;
4173 - }
4174 -
4175 - // If that fails, try more aggressive URL matching
4176 - // Remove trailing slashes and query parameters for better matching
4177 - $clean_url = rtrim($url, '/');
4178 - $clean_url = strtok($clean_url, '?'); // Remove query parameters
4179 -
4180 - // Try again with cleaned URL
4181 - $post_id = url_to_postid($clean_url);
4182 - if ($post_id > 0) {
4183 - return $post_id;
4184 - }
4185 -
4186 - // For bbPress forum topics, try extracting slug from URL
4187 - if (strpos($url, '/topic/') !== false || strpos($url, '/forums/') !== false) {
4188 - // Handle bbPress URLs: /forums/topic/topic-name/
4189 - if (preg_match('/\/forums\/topic\/([^\/\?]+)/', $url, $matches)) {
4190 - $topic_slug = $matches[1];
4191 -
4192 - // Look up topic by slug
4193 - $topic = get_page_by_path($topic_slug, OBJECT, 'topic');
4194 - if ($topic) {
4195 - return $topic->ID;
4196 - }
4197 -
4198 - // Alternative method: query by post_name
4199 - global $wpdb;
4200 - $post_id = $wpdb->get_var($wpdb->prepare(
4201 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
4202 - $topic_slug
4203 - ));
4204 -
4205 - if ($post_id) {
4206 - return intval($post_id);
4207 - }
4208 - }
4209 -
4210 - // Handle simpler topic URLs: /topic/topic-name/
4211 - if (preg_match('/\/topic\/([^\/\?]+)/', $url, $matches)) {
4212 - $topic_slug = $matches[1];
4213 -
4214 - global $wpdb;
4215 - $post_id = $wpdb->get_var($wpdb->prepare(
4216 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
4217 - $topic_slug
4218 - ));
4219 -
4220 - if ($post_id) {
4221 - return intval($post_id);
4222 - }
4223 - }
4224 - }
4225 -
4226 - // For WooCommerce products
4227 - if (strpos($url, '/product/') !== false || strpos($url, 'product=') !== false) {
4228 - // Extract product slug from various URL formats
4229 - $product_slug = '';
4230 -
4231 - // Handle pretty permalinks: /product/product-name/
4232 - if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
4233 - $product_slug = $matches[1];
4234 - }
4235 - // Handle query parameters: ?product=product-name
4236 - elseif (preg_match('/[\?&]product=([^&]+)/', $url, $matches)) {
4237 - $product_slug = $matches[1];
4238 - }
4239 -
4240 - if (!empty($product_slug)) {
4241 - // Look up product by slug
4242 - $product = get_page_by_path($product_slug, OBJECT, 'product');
4243 - if ($product) {
4244 - return $product->ID;
4245 - }
4246 -
4247 - // Alternative method: query by post_name
4248 - global $wpdb;
4249 - $post_id = $wpdb->get_var($wpdb->prepare(
4250 - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'product' AND post_status = 'publish'",
4251 - $product_slug
4252 - ));
4253 -
4254 - if ($post_id) {
4255 - return intval($post_id);
4256 - }
4257 - }
4258 - }
4259 -
4260 - // Generic approach: try to extract slug and match against all post types
4261 - $parsed_url = wp_parse_url($clean_url);
4262 - $path = $parsed_url['path'] ?? '';
4263 -
4264 - if (!empty($path)) {
4265 - // Get the last part of the path as potential slug
4266 - $path_parts = array_filter(explode('/', trim($path, '/')));
4267 - $potential_slug = end($path_parts);
4268 -
4269 - if (!empty($potential_slug)) {
4270 - global $wpdb;
4271 -
4272 - // Try to find any post with this slug
4273 - $post_id = $wpdb->get_var($wpdb->prepare(
4274 - "SELECT ID FROM {$wpdb->posts}
4275 - WHERE post_name = %s
4276 - AND post_status IN ('publish', 'closed', 'private')
4277 - AND post_type NOT IN ('revision', 'attachment', 'nav_menu_item')
4278 - ORDER BY CASE
4279 - WHEN post_type = 'post' THEN 1
4280 - WHEN post_type = 'page' THEN 2
4281 - WHEN post_type = 'topic' THEN 3
4282 - WHEN post_type = 'product' THEN 4
4283 - ELSE 5
4284 - END
4285 - LIMIT 1",
4286 - $potential_slug
4287 - ));
4288 -
4289 - if ($post_id) {
4290 - return intval($post_id);
4291 - }
4292 - }
4293 - }
4294 -
4295 - // ADDITIONAL: Try direct database lookup by URL variations
4296 - global $wpdb;
4297 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4298 -
4299 - // Try variations of the URL (with/without trailing slash, http/https)
4300 - $url_variations = array(
4301 - $url,
4302 - rtrim($url, '/'),
4303 - $url . '/',
4304 - str_replace('http://', 'https://', $url),
4305 - str_replace('https://', 'http://', $url),
4306 - str_replace('http://', 'https://', rtrim($url, '/')),
4307 - str_replace('https://', 'http://', rtrim($url, '/'))
4308 - );
4309 -
4310 - // Remove duplicates
4311 - $url_variations = array_unique($url_variations);
4312 -
4313 - foreach ($url_variations as $variation) {
4314 - $existing_record = $wpdb->get_row($wpdb->prepare(
4315 - "SELECT id, source_url FROM $table_name WHERE source_url = %s",
4316 - $variation
4317 - ));
4318 -
4319 - if ($existing_record) {
4320 - // Try to get post ID from this stored URL
4321 - $stored_post_id = url_to_postid($existing_record->source_url);
4322 - if ($stored_post_id > 0) {
4323 - return $stored_post_id;
4324 - }
4325 - }
4326 - }
4327 -
4328 - return 0; // No match found
4329 -}
4330 -/**
4331 - * Process selected content via AJAX
4332 - */
4333 -public function ajax_mxchat_process_selected_content() {
4334 - // Basic request validation
4335 - if (!check_ajax_referer('mxchat_content_selector_nonce', 'nonce', false)) {
4336 - wp_send_json_error('Invalid nonce');
4337 - exit;
4338 - }
4339 -
4340 - if (!current_user_can('manage_options')) {
4341 - wp_send_json_error('Unauthorized access');
4342 - exit;
4343 - }
4344 -
4345 - // Get post IDs - safely parse the array
4346 - $post_ids = array();
4347 - if (isset($_POST['post_ids']) && is_array($_POST['post_ids'])) {
4348 - foreach ($_POST['post_ids'] as $id) {
4349 - $post_ids[] = absint($id);
4350 - }
4351 - }
4352 -
4353 - if (empty($post_ids)) {
4354 - wp_send_json_error('No content selected');
4355 - exit;
4356 - }
4357 -
4358 - // Get bot_id from request
4359 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
4360 -
4361 - // ACF→PDF extraction is an install-level setting (Knowledge → ACF Fields,
4362 - // plan 11720c). The import modal shows a passive status line pointing
4363 - // there; the old per-batch checkbox and its remembered default are gone.
4364 - $extract_acf_pdfs = get_option('mxchat_acf_pdf_extraction', '0') === '1';
4365 -
4366 - // Process only ONE post at a time to avoid request size issues
4367 - $post_id = reset($post_ids);
4368 - $post = get_post($post_id);
4369 -
4370 - if (!$post) {
4371 - wp_send_json_error('Post not found');
4372 - exit;
4373 - }
4374 -
4375 - /**
4376 - * Allow developers to modify post data before processing into the knowledge base.
4377 - * Applied on BOTH content-preparation paths (this manual bulk import and the
4378 - * auto-sync path in mxchat_handle_post_update) with the same signature, so a
4379 - * callback registered once covers every indexing route. Purely additive —
4380 - * zero behaviour change when unhooked.
4381 - *
4382 - * @param WP_Post $post The post about to be indexed.
4383 - * @param string $bot_id Bot context for this import.
4384 - */
4385 - $post = apply_filters('mxchat_before_process_post', $post, $bot_id);
4386 - if (!($post instanceof WP_Post)) {
4387 - $post = get_post($post_id); // defend against a bad callback return
4388 - }
4389 -
4390 - // Assemble the indexable text via the shared post-kind assembler (a3d60c).
4391 - // Bulk import reads raw post fields, has always included product custom tabs,
4392 - // and passes the install-level ACF→PDF option.
4393 - $prepared = $this->mxchat_prepare_post_content_for_indexing($post_id, $post, array(
4394 - 'read_display' => false,
4395 - 'extract_acf_pdfs' => $extract_acf_pdfs,
4396 - 'include_product_tabs' => true,
4397 - ));
4398 - $content = $prepared['content'];
4399 - $pdf_extracted_count = $prepared['pdf_extracted_count'];
4400 -
4401 - // Debug logging for WordPress Import content
4402 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Post ID: ' . $post_id . ' Title: ' . $post->post_title);
4403 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Raw post_content length: ' . strlen($post->post_content));
4404 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Final content length: ' . strlen($content));
4405 - //error_log('[MXCHAT-WP-IMPORT-DEBUG] Content preview: ' . substr($content, 0, 300));
4406 -
4407 - // Note: Removed 10,000 char limit - chunking now handles large content properly
4408 -
4409 - // Get bot-specific embedding decision — custom-provider-aware (plan cbd5fd)
4410 - $bot_options = $this->get_bot_options($bot_id);
4411 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
4412 -
4413 - $preflight = MxChat_Utils::embedding_preflight($options);
4414 - if (!$preflight['ok']) {
4415 - MxChat_Admin::mxchat_log_debug('api_error', $preflight['reason'] . ' (knowledge processing)');
4416 - wp_send_json_error($preflight['reason']);
4417 - exit;
4418 - }
4419 - $api_key = $preflight['api_key'];
4420 -
4421 - $source_url = get_permalink($post_id);
4422 - $vector_id = md5($source_url); // Vector ID for Pinecone
4423 -
4424 - // Check for existing content in bot-specific storage
4425 - $is_update = false;
4426 -
4427 - // Get bot-specific Pinecone configuration
4428 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
4429 - $use_pinecone = !empty($bot_pinecone_config) && ($bot_pinecone_config['use_pinecone'] ?? false);
4430 -
4431 - if ($use_pinecone && !empty($bot_pinecone_config['api_key'])) {
4432 - // Check Pinecone for this bot
4433 - $pinecone_data = $this->mxchat_get_pinecone_processed_content($bot_pinecone_config);
4434 - if (isset($pinecone_data[$post_id])) {
4435 - $is_update = true;
4436 - }
4437 - } else {
4438 - // Check WordPress DB (same as before since it's shared)
4439 - global $wpdb;
4440 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4441 - $existing_record = $wpdb->get_row($wpdb->prepare(
4442 - "SELECT id FROM $table_name WHERE source_url = %s",
4443 - $source_url
4444 - ));
4445 -
4446 - if ($existing_record) {
4447 - $is_update = true;
4448 - }
4449 - }
4450 -
4451 - // UPDATED 2.5.6: Determine content type based on post_type
4452 - $post_type = $post->post_type;
4453 - $content_type = 'content'; // Default fallback
4454 -
4455 - // Map WordPress post types to content types
4456 - switch ($post_type) {
4457 - case 'post':
4458 - $content_type = 'post';
4459 - break;
4460 - case 'page':
4461 - $content_type = 'page';
4462 - break;
4463 - case 'product':
4464 - $content_type = 'product';
4465 - break;
4466 - default:
4467 - // For custom post types, use the post type name
4468 - $content_type = sanitize_key($post_type);
4469 - break;
4470 - }
4471 -
4472 - // Use the centralized utility function with bot_id and content_type
4473 - $result = MxChat_Utils::submit_content_to_db(
4474 - $content,
4475 - $source_url,
4476 - $api_key,
4477 - $vector_id,
4478 - $bot_id,
4479 - $content_type
4480 - );
4481 -
4482 - if (is_wp_error($result)) {
4483 - MxChat_Admin::mxchat_log_debug('storage_error', 'Knowledge storage failed: ' . $result->get_error_message(), array('source_url' => $source_url));
4484 - wp_send_json_error('Storage failed: ' . $result->get_error_message());
4485 - exit;
4486 - }
4487 -
4488 - // Automatically apply role restriction based on tags
4489 - $this->apply_role_restriction_to_post($post_id, $source_url);
4490 -
4491 - $operation_type = $is_update ? 'update' : 'new';
4492 -
4493 - // Count ACF fields for debugging
4494 - $acf_field_count = $prepared['acf_fields_found'];
4495 -
4496 - // Success response with minimal data
4497 - wp_send_json_success(array(
4498 - 'message' => $operation_type === 'update' ? 'Content updated successfully' : 'Content processed successfully',
4499 - 'post_id' => $post_id,
4500 - 'title' => $post->post_title,
4501 - 'operation_type' => $operation_type,
4502 - 'vector_id' => $vector_id,
4503 - 'acf_fields_found' => $acf_field_count,
4504 - 'pdf_extracted_count' => (int) $pdf_extracted_count,
4505 - 'content_preview' => substr($content, 0, 100) . '...',
4506 - 'bot_id' => $bot_id
4507 - ));
4508 - exit;
4509 -}
4510 -
4511 -private function apply_role_restriction_to_post($post_id, $source_url) {
4512 - // Get tag-role mappings
4513 - $mappings = get_option('mxchat_tag_role_mappings', array());
4514 -
4515 - if (empty($mappings)) {
4516 - return; // No mappings, leave as public
4517 - }
4518 -
4519 - // Get all tags for the post
4520 - $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
4521 -
4522 - if (empty($post_tags)) {
4523 - return; // No tags, leave as public
4524 - }
4525 -
4526 - // Determine the highest role restriction based on tags
4527 - $highest_role = 'public';
4528 - $role_hierarchy = array(
4529 - 'public' => 0,
4530 - 'logged_in' => 1,
4531 - 'subscriber' => 2,
4532 - 'contributor' => 3,
4533 - 'author' => 4,
4534 - 'editor' => 5,
4535 - 'administrator' => 6
4536 - );
4537 -
4538 - foreach ($post_tags as $tag_slug) {
4539 - if (isset($mappings[$tag_slug])) {
4540 - $role = $mappings[$tag_slug];
4541 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
4542 - $highest_role = $role;
4543 - }
4544 - }
4545 - }
4546 -
4547 - // If no restricted tags found, return (leave as public)
4548 - if ($highest_role === 'public') {
4549 - return;
4550 - }
4551 -
4552 - // Update the role restriction in the database
4553 - global $wpdb;
4554 -
4555 - // Check if using Pinecone
4556 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4557 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4558 -
4559 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4560 - // Update Pinecone role restriction
4561 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
4562 - $vector_id = md5($source_url);
4563 -
4564 - $wpdb->replace(
4565 - $roles_table,
4566 - array(
4567 - 'vector_id' => $vector_id,
4568 - 'role_restriction' => $highest_role,
4569 - 'updated_at' => current_time('mysql')
4570 - ),
4571 - array('%s', '%s', '%s')
4572 - );
4573 - } else {
4574 - // Update WordPress DB
4575 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4576 -
4577 - $wpdb->update(
4578 - $table_name,
4579 - array('role_restriction' => $highest_role),
4580 - array('source_url' => $source_url),
4581 - array('%s'),
4582 - array('%s')
4583 - );
4584 - }
4585 -
4586 - // The entry's restriction just changed — keep the OpenAI Vector Store
4587 - // mirror consistent: non-public pulls the file (file_search has no
4588 - // per-role filtering), public re-mirrors it (plan 15b5c6).
4589 - if (class_exists('MxChat_Vectorstore_Manager')) {
4590 - MxChat_Vectorstore_Manager::handle_role_change($source_url, 'default', $highest_role);
4591 - }
4592 -}
4593 -
4594 -public function mxchat_get_public_post_types() {
4595 - // Get all public post types
4596 - $post_types = get_post_types(array('public' => true), 'objects');
4597 - $post_type_options = array();
4598 -
4599 - foreach ($post_types as $post_type) {
4600 - $post_type_options[$post_type->name] = $post_type->label;
4601 - }
4602 -
4603 - // Also include common forum/community post types that might not be marked as public
4604 - $additional_types = array(
4605 - 'topic' => 'Forum Topics (bbPress)',
4606 - 'reply' => 'Forum Replies (bbPress)',
4607 - 'forum' => 'Forums (bbPress)',
4608 - 'wpforo_topic' => 'wpForo Topics',
4609 - 'wpforo_post' => 'wpForo Posts'
4610 - );
4611 -
4612 - foreach ($additional_types as $type_name => $type_label) {
4613 - if (post_type_exists($type_name) && !isset($post_type_options[$type_name])) {
4614 - $post_type_options[$type_name] = $type_label;
4615 - }
4616 - }
4617 -
4618 - return $post_type_options;
4619 -}
4620 -
4621 -/**
4622 - * Retrieves processed content from Pinecone API
4623 - */
4624 -public function mxchat_get_pinecone_processed_content($pinecone_options) {
4625 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4626 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4627 -
4628 - if (empty($api_key) || empty($host)) {
4629 - return array();
4630 - }
4631 -
4632 - $pinecone_data = array();
4633 -
4634 - try {
4635 - // Always get fresh data from Pinecone
4636 - $pinecone_data = $this->mxchat_scan_pinecone_for_processed_content($pinecone_options);
4637 -
4638 - // Method 2: Final fallback - try stats endpoint (if available)
4639 - if (empty($pinecone_data)) {
4640 - $stats_url = "https://{$host}/describe_index_stats";
4641 -
4642 - $response = wp_remote_post($stats_url, array(
4643 - 'headers' => array(
4644 - 'Api-Key' => $api_key,
4645 - 'Content-Type' => 'application/json'
4646 - ),
4647 - 'body' => json_encode(array()),
4648 - 'timeout' => 30
4649 - ));
4650 -
4651 - if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
4652 - $body = wp_remote_retrieve_body($response);
4653 - $stats_data = json_decode($body, true);
4654 - }
4655 - }
4656 -
4657 - } catch (Exception $e) {
4658 - // Log error but return fresh data only
4659 - }
4660 -
4661 - return $pinecone_data;
4662 -}
4663 -public function mxchat_fetch_pinecone_vectors_by_ids($pinecone_options, $vector_ids) {
4664 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4665 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4666 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
4667 -
4668 - if (empty($api_key) || empty($host) || empty($vector_ids)) {
4669 - return array();
4670 - }
4671 -
4672 - try {
4673 - // NOTE (plan 793b82): /vectors/fetch is a GET endpoint with the ids
4674 - // repeated in the query string (ids=a&ids=b — http_build_query would
4675 - // emit ids[0]=a); the old POST here was answered 200-with-an-empty-body,
4676 - // which read as "nothing indexed". Chunked at 100 ids to stay well
4677 - // under the measured HTTP 414 URL-length boundary.
4678 - $vectors = array();
4679 - foreach (array_chunk(array_values($vector_ids), 100) as $chunk) {
4680 - $fetch_query = array();
4681 - foreach ($chunk as $fetch_vid) {
4682 - $fetch_query[] = 'ids=' . rawurlencode($fetch_vid);
4683 - }
4684 - if (!empty($namespace)) {
4685 - $fetch_query[] = 'namespace=' . rawurlencode($namespace);
4686 - }
4687 -
4688 - $response = wp_remote_get("https://{$host}/vectors/fetch?" . implode('&', $fetch_query), array(
4689 - 'headers' => array(
4690 - 'Api-Key' => $api_key,
4691 - 'accept' => 'application/json'
4692 - ),
4693 - 'timeout' => 30
4694 - ));
4695 -
4696 - if (is_wp_error($response)) {
4697 - error_log('MxChat Pinecone: mxchat_fetch_pinecone_vectors_by_ids GET failed: ' . $response->get_error_message());
4698 - continue;
4699 - }
4700 -
4701 - if (wp_remote_retrieve_response_code($response) !== 200) {
4702 - error_log('MxChat Pinecone: mxchat_fetch_pinecone_vectors_by_ids GET returned HTTP ' . wp_remote_retrieve_response_code($response));
4703 - continue;
4704 - }
4705 -
4706 - $data = json_decode(wp_remote_retrieve_body($response), true);
4707 - if (isset($data['vectors']) && is_array($data['vectors'])) {
4708 - $vectors += $data['vectors'];
4709 - }
4710 - }
4711 -
4712 - if (empty($vectors)) {
4713 - return array();
4714 - }
4715 -
4716 - $processed_data = array();
4717 -
4718 - foreach ($vectors as $vector_id => $vector_data) {
4719 - $metadata = $vector_data['metadata'] ?? array();
4720 - $source_url = $metadata['source_url'] ?? '';
4721 -
4722 - if (!empty($source_url)) {
4723 - $post_id = url_to_postid($source_url);
4724 - if ($post_id) {
4725 - $created_at = $metadata['created_at'] ?? '';
4726 - $processed_date = 'Recently';
4727 -
4728 - if (!empty($created_at)) {
4729 - $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
4730 - if ($timestamp) {
4731 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
4732 - }
4733 - }
4734 -
4735 - $processed_data[$post_id] = array(
4736 - 'db_id' => $vector_id,
4737 - 'processed_date' => $processed_date,
4738 - 'url' => $source_url,
4739 - 'source' => 'pinecone',
4740 - 'timestamp' => $timestamp ?? current_time('timestamp')
4741 - );
4742 - }
4743 - }
4744 - }
4745 -
4746 - //error_log('DEBUG: Processed ' . count($processed_data) . ' vectors from fetch');
4747 - return $processed_data;
4748 -
4749 - } catch (Exception $e) {
4750 - //error_log('DEBUG: Exception in fetch_pinecone_vectors_by_ids: ' . $e->getMessage());
4751 - return array();
4752 - }
4753 -}
4754 -
4755 -/**
4756 - * Get embedding dimensions based on the selected model.
4757 - */
4758 -private function mxchat_get_embedding_dimensions() {
4759 - $options = get_option('mxchat_options', array());
4760 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4761 -
4762 - $model_dimensions = array(
4763 - 'text-embedding-ada-002' => 1536,
4764 - 'text-embedding-3-small' => 1536,
4765 - 'text-embedding-3-large' => 3072,
4766 - 'voyage-2' => 1024,
4767 - 'voyage-large-2' => 1536,
4768 - 'voyage-3-large' => 2048,
4769 - 'gemini-embedding-001' => 1536,
4770 - );
4771 -
4772 - if (strpos($selected_model, 'voyage-3-large') === 0) {
4773 - $custom_dimensions = $options['voyage_output_dimension'] ?? 2048;
4774 - return intval($custom_dimensions);
4775 - }
4776 -
4777 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4778 - $custom_dimensions = $options['gemini_output_dimension'] ?? 1536;
4779 - return intval($custom_dimensions);
4780 - }
4781 -
4782 - return $model_dimensions[$selected_model] ?? 1536;
4783 -}
4784 -
4785 -/**
4786 - * Scan Pinecone for processed content
4787 - */
4788 -public function mxchat_scan_pinecone_for_processed_content($pinecone_options) {
4789 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4790 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4791 - // plan 793b82: this scan was namespace-blind — on a namespaced setup it
4792 - // surveyed the default namespace and reported the wrong content as indexed.
4793 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
4794 -
4795 - if (empty($api_key) || empty($host)) {
4796 - return array();
4797 - }
4798 -
4799 - try {
4800 - // Use multiple random vectors to get better coverage
4801 - $all_matches = array();
4802 - $seen_ids = array();
4803 -
4804 - // Get correct dimensions for the configured embedding model
4805 - $dimensions = $this->mxchat_get_embedding_dimensions();
4806 -
4807 - // Try 3 different random vectors to get better coverage
4808 - for ($i = 0; $i < 3; $i++) {
4809 - $query_url = "https://{$host}/query";
4810 -
4811 - // Generate a random unit vector instead of zeros
4812 - $random_vector = array();
4813 - for ($j = 0; $j < $dimensions; $j++) {
4814 - $random_vector[] = (rand(-1000, 1000) / 1000.0);
4815 - }
4816 -
4817 - // Normalize the vector to unit length
4818 - $magnitude = sqrt(array_sum(array_map(function($x) { return $x * $x; }, $random_vector)));
4819 - if ($magnitude > 0) {
4820 - $random_vector = array_map(function($x) use ($magnitude) { return $x / $magnitude; }, $random_vector);
4821 - }
4822 -
4823 - $query_data = array(
4824 - 'includeMetadata' => true,
4825 - 'includeValues' => false,
4826 - 'topK' => 10000,
4827 - 'vector' => $random_vector
4828 - );
4829 -
4830 - if (!empty($namespace)) {
4831 - $query_data['namespace'] = $namespace;
4832 - }
4833 -
4834 - $response = wp_remote_post($query_url, array(
4835 - 'headers' => array(
4836 - 'Api-Key' => $api_key,
4837 - 'Content-Type' => 'application/json'
4838 - ),
4839 - 'body' => json_encode($query_data),
4840 - 'timeout' => 30
4841 - ));
4842 -
4843 - if (is_wp_error($response)) {
4844 - continue;
4845 - }
4846 -
4847 - $response_code = wp_remote_retrieve_response_code($response);
4848 -
4849 - if ($response_code !== 200) {
4850 - continue;
4851 - }
4852 -
4853 - $body = wp_remote_retrieve_body($response);
4854 - $data = json_decode($body, true);
4855 -
4856 - if (isset($data['matches'])) {
4857 - foreach ($data['matches'] as $match) {
4858 - $match_id = $match['id'] ?? '';
4859 - if (!empty($match_id) && !isset($seen_ids[$match_id])) {
4860 - $all_matches[] = $match;
4861 - $seen_ids[$match_id] = true;
4862 - }
4863 - }
4864 - }
4865 - }
4866 -
4867 - // Convert matches to processed data format, grouping by URL to count chunks
4868 - $processed_data = array();
4869 - $url_chunk_counts = array();
4870 -
4871 - foreach ($all_matches as $match) {
4872 - $metadata = $match['metadata'] ?? array();
4873 - $source_url = $metadata['source_url'] ?? '';
4874 - $match_id = $match['id'] ?? '';
4875 -
4876 - if (!empty($source_url) && !empty($match_id)) {
4877 - $post_id = url_to_postid($source_url);
4878 - if ($post_id) {
4879 - // Count chunks per post_id
4880 - if (!isset($url_chunk_counts[$post_id])) {
4881 - $url_chunk_counts[$post_id] = 0;
4882 - }
4883 - $url_chunk_counts[$post_id]++;
4884 -
4885 - $created_at = $metadata['created_at'] ?? '';
4886 - $processed_date = 'Recently';
4887 -
4888 - if (!empty($created_at)) {
4889 - $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
4890 - if ($timestamp) {
4891 - $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
4892 - }
4893 - }
4894 -
4895 - // Only store if not already set, or update with newer timestamp
4896 - if (!isset($processed_data[$post_id]) ||
4897 - ($timestamp ?? 0) > ($processed_data[$post_id]['timestamp'] ?? 0)) {
4898 - $processed_data[$post_id] = array(
4899 - 'db_id' => $match_id,
4900 - 'processed_date' => $processed_date,
4901 - 'url' => $source_url,
4902 - 'source' => 'pinecone',
4903 - 'timestamp' => $timestamp ?? current_time('timestamp')
4904 - );
4905 - }
4906 - }
4907 - }
4908 - }
4909 -
4910 - // Add chunk counts to processed data
4911 - foreach ($url_chunk_counts as $post_id => $chunk_count) {
4912 - if (isset($processed_data[$post_id])) {
4913 - $processed_data[$post_id]['chunk_count'] = $chunk_count;
4914 - }
4915 - }
4916 -
4917 - return $processed_data;
4918 -
4919 - } catch (Exception $e) {
4920 - return array();
4921 - }
4922 -}
4923 -/**
4924 - * Generate embeddings from input text for MXChat with bot support
4925 - */
4926 -private function mxchat_generate_embedding($text, $bot_id = 'default') {
4927 - // Enable detailed logging for debugging
4928 - //error_log('[MXCHAT-EMBED] Starting embedding generation for bot: ' . $bot_id . '. Text length: ' . strlen($text) . ' bytes');
4929 - //error_log('[MXCHAT-EMBED] Text preview: ' . substr($text, 0, 100) . '...');
4930 -
4931 - // Get bot-specific options
4932 - $bot_options = $this->get_bot_options($bot_id);
4933 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
4934 -
4935 - // Opt-in: when the custom provider is selected for embeddings, index through
4936 - // the same custom endpoint the query path uses so stored vectors and query
4937 - // vectors share a model. Returns the vector array on success, or an error
4938 - // string on failure (this function's existing failure contract).
4939 - if (isset($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
4940 - if (!class_exists('MxChat_Utils')) {
4941 - require_once dirname(__FILE__) . '/../includes/class-mxchat-utils.php';
4942 - }
4943 - return MxChat_Utils::generate_embedding_custom($text, $options);
4944 - }
4945 -
4946 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4947 - //error_log('[MXCHAT-EMBED] Selected embedding model for bot ' . $bot_id . ': ' . $selected_model);
4948 -
4949 - // Determine provider and endpoint
4950 - if (strpos($selected_model, 'voyage') === 0) {
4951 - $api_key = $options['voyage_api_key'] ?? '';
4952 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
4953 - $provider_name = 'Voyage AI';
4954 - //error_log('[MXCHAT-EMBED] Using Voyage AI API for bot ' . $bot_id);
4955 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4956 - $api_key = $options['gemini_api_key'] ?? '';
4957 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
4958 - $provider_name = 'Google Gemini';
4959 - //error_log('[MXCHAT-EMBED] Using Google Gemini API for bot ' . $bot_id);
4960 - } else {
4961 - $api_key = $options['api_key'] ?? '';
4962 - $endpoint = 'https://api.openai.com/v1/embeddings';
4963 - $provider_name = 'OpenAI';
4964 - //error_log('[MXCHAT-EMBED] Using OpenAI API for bot ' . $bot_id);
4965 - }
4966 -
4967 - //error_log('[MXCHAT-EMBED] Using endpoint: ' . $endpoint);
4968 -
4969 - if (empty($api_key)) {
4970 - $error_message = sprintf('Missing %s API key for bot %s. Please configure your API key in the bot settings.', $provider_name, $bot_id);
4971 - //error_log('[MXCHAT-EMBED] Error: ' . $error_message);
4972 - return $error_message;
4973 - }
4974 -
4975 - // Check if text is too long (for OpenAI, roughly estimate tokens as words/0.75)
4976 - $estimated_tokens = ceil(str_word_count($text) / 0.75);
4977 - //error_log('[MXCHAT-EMBED] Estimated token count: ~' . $estimated_tokens);
4978 -
4979 - if ($estimated_tokens > 8000 && strpos($selected_model, 'voyage') === false && strpos($selected_model, 'gemini-embedding') === false) {
4980 - //error_log('[MXCHAT-EMBED] Warning: Text may exceed OpenAI token limits (8K for most models)');
4981 - // Consider truncating text here
4982 - }
4983 -
4984 - // Prepare request body based on provider
4985 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4986 - // Gemini API format
4987 - $request_body = array(
4988 - 'model' => 'models/' . $selected_model,
4989 - 'content' => array(
4990 - 'parts' => array(
4991 - array('text' => $text)
4992 - )
4993 - )
4994 - );
4995 -
4996 - // Set output dimensionality to 1536 for consistency with other models
4997 - $request_body['outputDimensionality'] = 1536;
4998 - } else {
4999 - // OpenAI/Voyage API format
5000 - $request_body = array(
5001 - 'model' => $selected_model,
5002 - 'input' => $text
5003 - );
5004 -
5005 - // Add output_dimension for voyage-3-large model
5006 - if ($selected_model === 'voyage-3-large') {
5007 - $request_body['output_dimension'] = 2048;
5008 - }
5009 - }
5010 -
5011 - //error_log('[MXCHAT-EMBED] Request prepared with model: ' . $selected_model);
5012 -
5013 - // Prepare headers based on provider
5014 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5015 - // Gemini uses API key as query parameter
5016 - $endpoint .= '?key=' . $api_key;
5017 - $headers = array(
5018 - 'Content-Type' => 'application/json'
5019 - );
5020 - } else {
5021 - // OpenAI/Voyage use Bearer token
5022 - $headers = array(
5023 - 'Authorization' => 'Bearer ' . $api_key,
5024 - 'Content-Type' => 'application/json'
5025 - );
5026 - }
5027 -
5028 - // Make API request
5029 - //error_log('[MXCHAT-EMBED] Sending API request to: ' . $endpoint);
5030 - $response = wp_remote_post($endpoint, array(
5031 - 'body' => wp_json_encode($request_body),
5032 - 'headers' => $headers,
5033 - 'timeout' => 60 // Increased timeout for large inputs
5034 - ));
5035 -
5036 - // Handle wp_remote_post errors
5037 - if (is_wp_error($response)) {
5038 - $error_message = $response->get_error_message();
5039 - //error_log('[MXCHAT-EMBED] WP Remote Post Error: ' . $error_message);
5040 - return 'Connection error: ' . $error_message;
5041 - }
5042 -
5043 - // Get and check HTTP response code
5044 - $http_code = wp_remote_retrieve_response_code($response);
5045 - //error_log('[MXCHAT-EMBED] API Response Code: ' . $http_code);
5046 -
5047 - if ($http_code !== 200) {
5048 - $error_body = wp_remote_retrieve_body($response);
5049 - //error_log('[MXCHAT-EMBED] API Error Response Body: ' . $error_body);
5050 -
5051 - // Try to parse error for more details
5052 - $error_json = json_decode($error_body, true);
5053 - if (json_last_error() === JSON_ERROR_NONE && isset($error_json['error'])) {
5054 - $error_type = $error_json['error']['type'] ?? 'unknown';
5055 - $error_message = $error_json['error']['message'] ?? 'No message';
5056 - //error_log('[MXCHAT-EMBED] API Error Type: ' . $error_type);
5057 - //error_log('[MXCHAT-EMBED] API Error Message: ' . $error_message);
5058 -
5059 - // Keep the provider's own diagnostic — a restricted-key 401 names the
5060 - // exact missing scope, and replacing it with "check your API key" sent
5061 - // a customer to regenerate two keys (plan 46b596). Same shape as
5062 - // MxChat_Utils::embedding_failure_error() so both ingestion paths read
5063 - // identically. Key never appears in provider messages, but scrub anyway.
5064 - if ($error_type === 'invalid_request_error' || $error_type === 'authentication_error') {
5065 - if (is_string($api_key) && $api_key !== '') {
5066 - $error_message = str_replace($api_key, '[redacted]', $error_message);
5067 - }
5068 - $error_message = sprintf(
5069 - 'Embedding failed (%s, HTTP %d): %s',
5070 - $selected_model,
5071 - $http_code,
5072 - substr($error_message, 0, 300)
5073 - );
5074 - }
5075 -
5076 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
5077 - return $error_message;
5078 - }
5079 -
5080 - $error_message = sprintf("API Error (HTTP %d): Unable to generate embedding for bot %s", $http_code, $bot_id);
5081 - //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
5082 - return $error_message;
5083 - }
5084 -
5085 - // Parse response body
5086 - $response_body = wp_remote_retrieve_body($response);
5087 - //error_log('[MXCHAT-EMBED] Received response length: ' . strlen($response_body) . ' bytes');
5088 -
5089 - $response_data = json_decode($response_body, true);
5090 -
5091 - if (json_last_error() !== JSON_ERROR_NONE) {
5092 - $error = json_last_error_msg();
5093 - //error_log('[MXCHAT-EMBED] JSON Parse Error: ' . $error);
5094 - //error_log('[MXCHAT-EMBED] Response preview: ' . substr($response_body, 0, 200));
5095 - return "Failed to parse API response: $error";
5096 - }
5097 -
5098 - // Handle different response formats based on provider
5099 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5100 - // Gemini API response format
5101 - if (isset($response_data['embedding']['values'])) {
5102 - $embedding_dimensions = count($response_data['embedding']['values']);
5103 - //error_log('[MXCHAT-EMBED] Successfully extracted Gemini embedding with ' . $embedding_dimensions . ' dimensions');
5104 -
5105 - // Check if embedding dimensions are as expected (should be 1536)
5106 - if ($embedding_dimensions !== 1536) {
5107 - //error_log('[MXCHAT-EMBED] Warning: Unexpected Gemini embedding dimensions: ' . $embedding_dimensions);
5108 - }
5109 -
5110 - return $response_data['embedding']['values'];
5111 - } else {
5112 - //error_log('[MXCHAT-EMBED] Error: No embedding found in Gemini response');
5113 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
5114 -
5115 - if (isset($response_data['error'])) {
5116 - $error_message = "Gemini API Error in response: " . wp_json_encode($response_data['error']);
5117 - //error_log('[MXCHAT-EMBED] ' . $error_message);
5118 - return $error_message;
5119 - }
5120 -
5121 - $error_message = "Invalid Gemini API response format: No embedding found";
5122 - //error_log('[MXCHAT-EMBED] ' . $error_message);
5123 - return $error_message;
5124 - }
5125 - } else {
5126 - // OpenAI/Voyage API response format
5127 - if (isset($response_data['data'][0]['embedding'])) {
5128 - $embedding_dimensions = count($response_data['data'][0]['embedding']);
5129 - //error_log('[MXCHAT-EMBED] Successfully extracted embedding with ' . $embedding_dimensions . ' dimensions');
5130 -
5131 - // Check if embedding dimensions are as expected
5132 - if (($selected_model === 'text-embedding-ada-002' && $embedding_dimensions !== 1536) ||
5133 - ($selected_model === 'voyage-3-large' && $embedding_dimensions !== 2048)) {
5134 - //error_log('[MXCHAT-EMBED] Warning: Unexpected embedding dimensions');
5135 - }
5136 -
5137 - return $response_data['data'][0]['embedding'];
5138 - } else {
5139 - //error_log('[MXCHAT-EMBED] Error: No embedding found in response');
5140 - //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
5141 -
5142 - if (isset($response_data['error'])) {
5143 - $error_message = "API Error in response: " . wp_json_encode($response_data['error']);
5144 - //error_log('[MXCHAT-EMBED] ' . $error_message);
5145 - return $error_message;
5146 - }
5147 -
5148 - $error_message = "Invalid API response format: No embedding found";
5149 - //error_log('[MXCHAT-EMBED] ' . $error_message);
5150 - return $error_message;
5151 - }
5152 - }
5153 -}
5154 -
5155 -/**
5156 - * Get bot-specific options for multi-bot functionality
5157 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
5158 - */
5159 -private function get_bot_options($bot_id = 'default') {
5160 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
5161 -
5162 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
5163 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
5164 - return array();
5165 - }
5166 -
5167 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
5168 -
5169 - if (!empty($bot_options)) {
5170 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
5171 - if (isset($bot_options['similarity_threshold'])) {
5172 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
5173 - }
5174 - }
5175 -
5176 - return is_array($bot_options) ? $bot_options : array();
5177 -}
5178 -
5179 -/**
5180 - * Get bot-specific Pinecone configuration
5181 - * Used in the knowledge retrieval functions
5182 - */
5183 -// Also add debugging to your get_bot_pinecone_config function
5184 -private function get_bot_pinecone_config($bot_id = 'default') {
5185 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
5186 -
5187 - // If default bot or multi-bot add-on not active, use default Pinecone config
5188 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
5189 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
5190 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
5191 - $config = array(
5192 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
5193 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
5194 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
5195 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
5196 - );
5197 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
5198 - return $config;
5199 - }
5200 -
5201 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
5202 -
5203 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
5204 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
5205 -
5206 - if (!empty($bot_pinecone_config)) {
5207 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
5208 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
5209 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
5210 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
5211 - } else {
5212 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
5213 - }
5214 -
5215 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
5216 -}
5217 -
5218 -
5219 -public function mxchat_ajax_dismiss_completed_status() {
5220 - try {
5221 - // Verify the request
5222 - check_ajax_referer('mxchat_status_nonce', 'nonce');
5223 -
5224 - if (!current_user_can('manage_options')) {
5225 - wp_send_json_error('Unauthorized access');
5226 - exit;
5227 - }
5228 -
5229 - $card_type = isset($_POST['card_type']) ? sanitize_text_field($_POST['card_type']) : '';
5230 -
5231 - if ($card_type === 'pdf') {
5232 - // Clear PDF status
5233 - $pdf_url = get_transient('mxchat_last_pdf_url');
5234 - if ($pdf_url) {
5235 - delete_transient('mxchat_pdf_status_' . md5($pdf_url));
5236 - delete_transient('mxchat_last_pdf_url');
5237 - }
5238 - } elseif ($card_type === 'sitemap') {
5239 - // Clear sitemap status
5240 - $sitemap_url = get_transient('mxchat_last_sitemap_url');
5241 - if ($sitemap_url) {
5242 - delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
5243 - delete_transient('mxchat_last_sitemap_url');
5244 - }
5245 - }
5246 -
5247 - wp_send_json_success(array('message' => 'Status dismissed successfully'));
5248 -
5249 - } catch (Exception $e) {
5250 - wp_send_json_error(array('message' => 'Error dismissing status: ' . $e->getMessage()));
5251 - }
5252 -}
5253 -
5254 -/**
5255 - * Render completed status cards on page load
5256 - * This ensures completed processing status persists through page refreshes
5257 - */
5258 -public function mxchat_render_completed_status_cards() {
5259 - $output = '';
5260 -
5261 - // Check for completed PDF status
5262 - $pdf_url = get_transient('mxchat_last_pdf_url');
5263 - if ($pdf_url) {
5264 - $pdf_status = $this->mxchat_get_pdf_processing_status($pdf_url);
5265 - if ($pdf_status && ($pdf_status['status'] === 'complete' || $pdf_status['status'] === 'error')) {
5266 - $output .= $this->mxchat_render_pdf_status_card($pdf_status, $pdf_url);
5267 - }
5268 - }
5269 -
5270 - // Check for completed sitemap status
5271 - $sitemap_url = get_transient('mxchat_last_sitemap_url');
5272 - if ($sitemap_url) {
5273 - $sitemap_status = $this->mxchat_get_sitemap_processing_status($sitemap_url);
5274 - if ($sitemap_status && ($sitemap_status['status'] === 'complete' || $sitemap_status['status'] === 'error')) {
5275 - $output .= $this->mxchat_render_sitemap_status_card($sitemap_status, $sitemap_url);
5276 - }
5277 - }
5278 -
5279 - return $output;
5280 -}
5281 -
5282 -/**
5283 - * Render PDF status card HTML
5284 - */
5285 -private function mxchat_render_pdf_status_card($status, $pdf_url) {
5286 - $html = '<div class="mxchat-status-card" data-card-type="pdf">';
5287 - $html .= '<div class="mxchat-status-header">';
5288 - $html .= '<h4>' . esc_html__('PDF Processing Status', 'mxchat') . '</h4>';
5289 -
5290 - // Add dismiss button for completed status
5291 - if ($status['status'] === 'complete' || $status['status'] === 'error') {
5292 - $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
5293 - }
5294 -
5295 - // Process Batch button for processing status
5296 - if ($status['status'] === 'processing') {
5297 - $html .= '<button type="button" class="mxchat-manual-batch-btn"
5298 - data-process-type="pdf"
5299 - data-url="' . esc_attr($pdf_url) . '">
5300 - ' . esc_html__('Process Batch', 'mxchat') . '</button>';
5301 - }
5302 -
5303 - // Add status badges
5304 - if ($status['status'] === 'error') {
5305 - $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
5306 - } elseif ($status['status'] === 'complete') {
5307 - if ($status['failed_pages'] > 0) {
5308 - $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
5309 - sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_pages']) . '</span>';
5310 - } else {
5311 - $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
5312 - }
5313 - }
5314 -
5315 - $html .= '</div>'; // End header
5316 -
5317 - // Progress bar
5318 - $html .= '<div class="mxchat-progress-bar">';
5319 - $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
5320 - $html .= '</div>';
5321 -
5322 - // Status details
5323 - $html .= '<div class="mxchat-status-details">';
5324 - $html .= '<p>' . sprintf(
5325 - esc_html__('Progress: %d of %d pages (%d%%)', 'mxchat'),
5326 - $status['processed_pages'],
5327 - $status['total_pages'],
5328 - $status['percentage']
5329 - ) . '</p>';
5330 -
5331 - // Show failed pages count if any
5332 - if ($status['failed_pages'] > 0) {
5333 - $html .= '<p><strong>' . esc_html__('Failed pages:', 'mxchat') . '</strong> ' . esc_html($status['failed_pages']) . '</p>';
5334 - }
5335 -
5336 - $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
5337 - $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
5338 -
5339 - // Add completion summary if available AND it's an array
5340 - if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
5341 - $summary = $status['completion_summary'];
5342 - $html .= '<div class="mxchat-completion-summary">';
5343 - $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
5344 - $html .= '<p><strong>' . esc_html__('Total Pages:', 'mxchat') . '</strong> ' . esc_html($summary['total_pages']) . '</p>';
5345 - $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_pages']) . '</p>';
5346 - $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_pages']) . '</p>';
5347 - $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
5348 - $html .= '</div>';
5349 - }
5350 -
5351 - // Add failed pages list if any AND it's an array
5352 - if (isset($status['failed_pages_list']) && is_array($status['failed_pages_list']) && !empty($status['failed_pages_list'])) {
5353 - $html .= $this->mxchat_render_failed_pages_list($status['failed_pages_list']);
5354 - }
5355 -
5356 - // Add error message if any
5357 - if (isset($status['error']) && !empty($status['error'])) {
5358 - $html .= '<div class="mxchat-error-notice">';
5359 - $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
5360 - $html .= '</div>';
5361 - }
5362 -
5363 - $html .= '</div>'; // End details
5364 - $html .= '</div>'; // End card
5365 -
5366 - return $html;
5367 -}
5368 -/**
5369 - * Render sitemap status card HTML
5370 - */
5371 -private function mxchat_render_sitemap_status_card($status, $sitemap_url) {
5372 - $html = '<div class="mxchat-status-card" data-card-type="sitemap">';
5373 - $html .= '<div class="mxchat-status-header">';
5374 - $html .= '<h4>' . esc_html__('Sitemap Processing Status', 'mxchat') . '</h4>';
5375 -
5376 - // Add dismiss button for completed status
5377 - if ($status['status'] === 'complete' || $status['status'] === 'error') {
5378 - $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
5379 - }
5380 -
5381 - // Process Batch button for processing status
5382 - if ($status['status'] === 'processing') {
5383 - $html .= '<button type="button" class="mxchat-manual-batch-btn"
5384 - data-process-type="sitemap"
5385 - data-url="' . esc_attr($sitemap_url) . '">
5386 - ' . esc_html__('Process Batch', 'mxchat') . '</button>';
5387 - }
5388 -
5389 - // Add status badges
5390 - if ($status['status'] === 'error') {
5391 - $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
5392 - } elseif ($status['status'] === 'complete') {
5393 - if ($status['failed_urls'] > 0) {
5394 - $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
5395 - sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_urls']) . '</span>';
5396 - } else {
5397 - $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
5398 - }
5399 - }
5400 -
5401 - $html .= '</div>'; // End header
5402 -
5403 - // Progress bar
5404 - $html .= '<div class="mxchat-progress-bar">';
5405 - $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
5406 - $html .= '</div>';
5407 -
5408 - // Status details
5409 - $html .= '<div class="mxchat-status-details">';
5410 - $html .= '<p>' . sprintf(
5411 - esc_html__('Progress: %d of %d URLs (%d%%)', 'mxchat'),
5412 - $status['processed_urls'],
5413 - $status['total_urls'],
5414 - $status['percentage']
5415 - ) . '</p>';
5416 -
5417 - // Show failed URLs count if any
5418 - if ($status['failed_urls'] > 0) {
5419 - $html .= '<p><strong>' . esc_html__('Failed URLs:', 'mxchat') . '</strong> ' . esc_html($status['failed_urls']) . '</p>';
5420 - }
5421 -
5422 - $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
5423 - $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
5424 -
5425 - // Add completion summary if available AND it's an array
5426 - if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
5427 - $summary = $status['completion_summary'];
5428 - $html .= '<div class="mxchat-completion-summary">';
5429 - $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
5430 - $html .= '<p><strong>' . esc_html__('Total URLs:', 'mxchat') . '</strong> ' . esc_html($summary['total_urls']) . '</p>';
5431 - $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_urls']) . '</p>';
5432 - $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_urls']) . '</p>';
5433 - $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
5434 - $html .= '</div>';
5435 - }
5436 -
5437 - // Add error messages if any (but not the failed URLs list)
5438 - if (!empty($status['error']) || !empty($status['last_error'])) {
5439 - $html .= '<div class="mxchat-error-notice">';
5440 -
5441 - if (!empty($status['error'])) {
5442 - $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
5443 - }
5444 -
5445 - if (!empty($status['last_error'])) {
5446 - $html .= '<p class="last-error">' . esc_html__('Last error:', 'mxchat') . ' ' . esc_html($status['last_error']) . '</p>';
5447 - }
5448 -
5449 - $html .= '</div>';
5450 - }
5451 -
5452 - $html .= '</div>'; // End details
5453 - $html .= '</div>'; // End card
5454 -
5455 - return $html;
5456 -}
5457 -
5458 -
5459 -/**
5460 - * Render failed pages list
5461 - */
5462 -private function mxchat_render_failed_pages_list($failed_pages_list) {
5463 - // Validate that $failed_pages_list is an array and not empty
5464 - if (!is_array($failed_pages_list) || empty($failed_pages_list)) {
5465 - return '';
5466 - }
5467 -
5468 - $html = '<div class="mxchat-error-notice">';
5469 - $html .= '<div class="mxchat-failed-pages-container">';
5470 - $html .= '<h5>' . sprintf(esc_html__('Failed Pages (%d)', 'mxchat'), count($failed_pages_list)) . '</h5>';
5471 - $html .= '<details>';
5472 - $html .= '<summary>' . esc_html__('Show Failed Pages', 'mxchat') . '</summary>';
5473 - $html .= '<div class="mxchat-failed-pages-list">';
5474 -
5475 - // Create table for failed pages
5476 - $html .= '<table class="widefat striped">';
5477 - $html .= '<thead><tr>';
5478 - $html .= '<th>' . esc_html__('Page', 'mxchat') . '</th>';
5479 - $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
5480 - $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
5481 - $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
5482 - $html .= '</tr></thead><tbody>';
5483 -
5484 - // Sort failed pages by most recent
5485 - $sorted_failed_pages = $failed_pages_list;
5486 - usort($sorted_failed_pages, function($a, $b) {
5487 - return ($b['time'] ?? 0) - ($a['time'] ?? 0);
5488 - });
5489 -
5490 - foreach ($sorted_failed_pages as $item) {
5491 - // Ensure $item is an array before accessing its elements
5492 - if (!is_array($item)) {
5493 - continue;
5494 - }
5495 -
5496 - $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
5497 - $html .= '<tr>';
5498 - $html .= '<td>' . esc_html__('Page', 'mxchat') . ' ' . esc_html($item['page'] ?? 'Unknown') . '</td>';
5499 - $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
5500 - $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
5501 - $html .= '<td>' . esc_html($time_ago) . '</td>';
5502 - $html .= '</tr>';
5503 - }
5504 -
5505 - $html .= '</tbody></table>';
5506 - $html .= '</div></details></div></div>';
5507 -
5508 - return $html;
5509 -}
5510 -
5511 -/**
5512 - * Render failed URLs list
5513 - */
5514 -private function mxchat_render_failed_urls_list($failed_urls_list) {
5515 - // Validate that $failed_urls_list is an array and not empty
5516 - if (!is_array($failed_urls_list) || empty($failed_urls_list)) {
5517 - return '';
5518 - }
5519 -
5520 - $html = '<div class="mxchat-failed-urls-container">';
5521 - $html .= '<h5>' . sprintf(esc_html__('Failed URLs (%d)', 'mxchat'), count($failed_urls_list)) . '</h5>';
5522 - $html .= '<details>';
5523 - $html .= '<summary>' . esc_html__('Show Failed URLs', 'mxchat') . '</summary>';
5524 - $html .= '<div class="mxchat-failed-urls-list">';
5525 -
5526 - // Create table for failed URLs
5527 - $html .= '<table class="widefat striped">';
5528 - $html .= '<thead><tr>';
5529 - $html .= '<th>' . esc_html__('URL', 'mxchat') . '</th>';
5530 - $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
5531 - $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
5532 - $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
5533 - $html .= '</tr></thead><tbody>';
5534 -
5535 - // Sort failed URLs by most recent
5536 - $sorted_failed_urls = $failed_urls_list;
5537 - usort($sorted_failed_urls, function($a, $b) {
5538 - return ($b['time'] ?? 0) - ($a['time'] ?? 0);
5539 - });
5540 -
5541 - // Show up to 50 failed URLs
5542 - $display_urls = array_slice($sorted_failed_urls, 0, 50);
5543 -
5544 - foreach ($display_urls as $item) {
5545 - // Ensure $item is an array before accessing its elements
5546 - if (!is_array($item)) {
5547 - continue;
5548 - }
5549 -
5550 - $url = $item['url'] ?? '';
5551 - $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
5552 -
5553 - // Truncate URL for display
5554 - $display_url = strlen($url) > 50 ? substr($url, 0, 47) . '...' : $url;
5555 -
5556 - $html .= '<tr>';
5557 - $html .= '<td style="word-break: break-all;">';
5558 - if (!empty($url)) {
5559 - $html .= '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($display_url) . '</a>';
5560 - } else {
5561 - $html .= esc_html__('Unknown URL', 'mxchat');
5562 - }
5563 - $html .= '</td>';
5564 - $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
5565 - $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
5566 - $html .= '<td>' . esc_html($time_ago) . '</td>';
5567 - $html .= '</tr>';
5568 - }
5569 -
5570 - $html .= '</tbody></table>';
5571 -
5572 - if (count($failed_urls_list) > 50) {
5573 - $html .= '<div class="mxchat-failed-urls-more">+ ' .
5574 - (count($failed_urls_list) - 50) .
5575 - ' ' . esc_html__('more failed URLs not shown', 'mxchat') . '</div>';
5576 - }
5577 -
5578 - $html .= '</div></details></div>';
5579 -
5580 - return $html;
5581 -}
5582 -
5583 -/**
5584 - * Get all ACF fields for a specific post, excluding any fields the user has disabled
5585 - */
5586 -public function mxchat_get_acf_fields_for_post($post_id) {
5587 - if (!function_exists('get_field_objects')) {
5588 - return array();
5589 - }
5590 -
5591 - // Field OBJECTS, not get_fields(): exclusion matches on the field KEY
5592 - // (unique per field) rather than the name (shared across groups — plan
5593 - // 30e81f). ACF's own get_fields() is implemented as get_field_objects()
5594 - // reduced to name => value, so the un-excluded reduction below is the
5595 - // identical shape and order the previous get_fields() call produced.
5596 - $field_objects = get_field_objects($post_id);
5597 - if (!$field_objects || !is_array($field_objects)) {
5598 - return array();
5599 - }
5600 -
5601 - $excluded_fields = get_option('mxchat_acf_excluded_fields', array());
5602 - if (!is_array($excluded_fields)) {
5603 - $excluded_fields = array();
5604 - }
5605 -
5606 - $fields = array();
5607 - foreach ($field_objects as $field_name => $field_object) {
5608 - if (!empty($excluded_fields)) {
5609 - $field_key = isset($field_object['key']) ? $field_object['key'] : '';
5610 - // Legacy name entries stay honored: a stored name whose group was
5611 - // inactive at migration time still excludes every field wearing it.
5612 - if (in_array($field_key, $excluded_fields, true) || in_array($field_name, $excluded_fields, true)) {
5613 - continue;
5614 - }
5615 - }
5616 - $fields[$field_name] = isset($field_object['value']) ? $field_object['value'] : null;
5617 - }
5618 -
5619 - return $fields;
5620 -}
5621 -
5622 -/**
5623 - * Get all registered ACF field groups and their fields for the settings UI
5624 - */
5625 -public function mxchat_get_all_acf_fields() {
5626 - if (!function_exists('acf_get_field_groups') || !function_exists('acf_get_fields')) {
5627 - return array();
5628 - }
5629 -
5630 - // Keyed by GROUP KEY, not title (titles are not unique), and each field
5631 - // entry carries its ACF field key — the unique identifier every toggle,
5632 - // save, and index-time exclusion now runs on (plans 30e81f / bf57e0).
5633 - $all_fields = array();
5634 - $field_groups = acf_get_field_groups();
5635 -
5636 - if (!empty($field_groups)) {
5637 - foreach ($field_groups as $group) {
5638 - $group_fields = acf_get_fields($group['key']);
5639 - if (!empty($group_fields)) {
5640 - $entry = array(
5641 - 'title' => $group['title'],
5642 - 'fields' => array(),
5643 - );
5644 - foreach ($group_fields as $field) {
5645 - $entry['fields'][] = array(
5646 - 'key' => $field['key'],
5647 - 'name' => $field['name'],
5648 - 'label' => $field['label'],
5649 - 'type' => $field['type']
5650 - );
5651 - }
5652 - $all_fields[$group['key']] = $entry;
5653 - }
5654 - }
5655 - }
5656 -
5657 - return $all_fields;
5658 -}
5659 -
5660 -/**
5661 - * Get whitelisted custom post meta for a given post
5662 - * This allows non-ACF meta fields (like OptionTree, theme meta boxes) to be included in embeddings
5663 - */
5664 -public function mxchat_get_whitelisted_post_meta($post_id) {
5665 - $whitelist = get_option('mxchat_custom_meta_whitelist', '');
5666 -
5667 - if (empty($whitelist)) {
5668 - return array();
5669 - }
5670 -
5671 - // Parse the whitelist - one meta key per line
5672 - $meta_keys = array_filter(array_map('trim', explode("\n", $whitelist)));
5673 -
5674 - if (empty($meta_keys)) {
5675 - return array();
5676 - }
5677 -
5678 - $result = array();
5679 -
5680 - foreach ($meta_keys as $key) {
5681 - // Skip empty keys
5682 - if (empty($key)) {
5683 - continue;
5684 - }
5685 -
5686 - $value = get_post_meta($post_id, $key, true);
5687 -
5688 - // Only include non-empty string values
5689 - if (!empty($value) && is_string($value)) {
5690 - $result[$key] = $value;
5691 - } elseif (!empty($value) && is_array($value)) {
5692 - // Handle array values by joining them
5693 - $flat_value = $this->mxchat_flatten_meta_array($value);
5694 - if (!empty($flat_value)) {
5695 - $result[$key] = $flat_value;
5696 - }
5697 - }
5698 - }
5699 -
5700 - return $result;
5701 -}
5702 -
5703 -/**
5704 - * Flatten array meta values into a readable string
5705 - */
5706 -private function mxchat_flatten_meta_array($array, $depth = 0) {
5707 - if ($depth > 3) {
5708 - return ''; // Prevent infinite recursion
5709 - }
5710 -
5711 - $parts = array();
5712 -
5713 - foreach ($array as $key => $value) {
5714 - if (is_string($value) && !empty($value)) {
5715 - $parts[] = $value;
5716 - } elseif (is_array($value)) {
5717 - $nested = $this->mxchat_flatten_meta_array($value, $depth + 1);
5718 - if (!empty($nested)) {
5719 - $parts[] = $nested;
5720 - }
5721 - }
5722 - }
5723 -
5724 - return implode(', ', $parts);
5725 -}
5726 -
5727 -/**
5728 - * Format ACF field values for content extraction
5729 - */
5730 -public function mxchat_format_acf_field_value($value, $field_name = '', $post_id = 0) {
5731 - if (empty($value)) {
5732 - return '';
5733 - }
5734 -
5735 - // Handle WP_Post objects first (THIS IS THE KEY FIX)
5736 - if ($value instanceof WP_Post) {
5737 - return $value->post_title ?: '';
5738 - }
5739 -
5740 - // Handle other WP objects
5741 - if (is_object($value)) {
5742 - if (isset($value->post_title)) {
5743 - return $value->post_title;
5744 - } elseif (isset($value->display_name)) {
5745 - return $value->display_name;
5746 - } elseif (isset($value->name)) {
5747 - return $value->name;
5748 - } elseif (method_exists($value, '__toString')) {
5749 - try {
5750 - return (string) $value;
5751 - } catch (Exception $e) {
5752 - return '';
5753 - }
5754 - }
5755 - // For any other objects, return empty string
5756 - return '';
5757 - }
5758 -
5759 - // Handle different ACF field types
5760 - if (is_array($value)) {
5761 - // Check if it's an image/file field
5762 - if (isset($value['url'])) {
5763 - // Image field - return alt text, title, or caption
5764 - if (!empty($value['alt'])) {
5765 - return $value['alt'];
5766 - } elseif (!empty($value['title'])) {
5767 - return $value['title'];
5768 - } elseif (!empty($value['caption'])) {
5769 - return $value['caption'];
5770 - } else {
5771 - return ''; // Don't include just the URL
5772 - }
5773 - }
5774 -
5775 - // Check if it's a post object or relationship field
5776 - if (isset($value['post_title'])) {
5777 - return $value['post_title'];
5778 - }
5779 -
5780 - // Check if it's a user field
5781 - if (isset($value['display_name'])) {
5782 - return $value['display_name'];
5783 - }
5784 -
5785 - // Check if it's a taxonomy term
5786 - if (isset($value['name']) && isset($value['taxonomy'])) {
5787 - return $value['name'];
5788 - }
5789 -
5790 - // Check if it's a select field with label
5791 - if (isset($value['label'])) {
5792 - return $value['label'];
5793 - }
5794 -
5795 - // Check for repeater field or flexible content
5796 - if (is_numeric(key($value))) {
5797 - $sub_values = array();
5798 - foreach ($value as $sub_item) {
5799 - if (is_array($sub_item)) {
5800 - // For repeater/flexible content, extract text values
5801 - $sub_text = $this->mxchat_extract_text_from_acf_array($sub_item);
5802 - if (!empty($sub_text)) {
5803 - $sub_values[] = $sub_text;
5804 - }
5805 - } elseif ($sub_item instanceof WP_Post) {
5806 - // Handle WP_Post objects in arrays
5807 - $sub_values[] = $sub_item->post_title ?: '';
5808 - } else {
5809 - $sub_values[] = (string) $sub_item;
5810 - }
5811 - }
5812 - return implode(', ', array_filter($sub_values));
5813 - }
5814 -
5815 - // For other arrays, try to extract meaningful text
5816 - $text_values = array();
5817 - foreach ($value as $key => $val) {
5818 - if (is_string($val) && !empty(trim($val))) {
5819 - $text_values[] = trim($val);
5820 - } elseif ($val instanceof WP_Post) {
5821 - // Handle WP_Post objects in associative arrays
5822 - $text_values[] = $val->post_title ?: '';
5823 - } elseif (is_array($val) && isset($val['post_title'])) {
5824 - $text_values[] = $val['post_title'];
5825 - } elseif (is_array($val) && isset($val['name'])) {
5826 - $text_values[] = $val['name'];
5827 - }
5828 - }
5829 -
5830 - return implode(', ', array_filter($text_values));
5831 - }
5832 -
5833 - // Handle boolean values
5834 - if (is_bool($value)) {
5835 - return $value ? 'Yes' : 'No';
5836 - }
5837 -
5838 - // Handle numeric values
5839 - if (is_numeric($value)) {
5840 - return (string) $value;
5841 - }
5842 -
5843 - // Handle string values
5844 - if (is_string($value)) {
5845 - return trim($value);
5846 - }
5847 -
5848 - // For anything else that we can't handle, return empty string
5849 - // This prevents the "Object could not be converted to string" error
5850 - return '';
5851 -}
5852 -
5853 -/**
5854 - * Extract text from complex ACF array structures
5855 - */
5856 -private function mxchat_extract_text_from_acf_array($array) {
5857 - if (!is_array($array)) {
5858 - return '';
5859 - }
5860 -
5861 - $text_parts = array();
5862 -
5863 - foreach ($array as $key => $value) {
5864 - if (is_string($value) && !empty(trim($value))) {
5865 - // Skip keys that are likely to be IDs or technical values
5866 - if (!is_numeric($value) || strlen($value) > 10) {
5867 - $text_parts[] = trim($value);
5868 - }
5869 - } elseif ($value instanceof WP_Post) {
5870 - // Handle WP_Post objects
5871 - $text_parts[] = $value->post_title ?: '';
5872 - } elseif (is_array($value)) {
5873 - if (isset($value['post_title'])) {
5874 - $text_parts[] = $value['post_title'];
5875 - } elseif (isset($value['name'])) {
5876 - $text_parts[] = $value['name'];
5877 - } elseif (isset($value['label'])) {
5878 - $text_parts[] = $value['label'];
5879 - }
5880 - } elseif (is_object($value)) {
5881 - // Handle other objects safely
5882 - if (isset($value->post_title)) {
5883 - $text_parts[] = $value->post_title;
5884 - } elseif (isset($value->name)) {
5885 - $text_parts[] = $value->name;
5886 - } elseif (isset($value->display_name)) {
5887 - $text_parts[] = $value->display_name;
5888 - }
5889 - }
5890 - }
5891 -
5892 - return implode(', ', array_filter($text_parts));
5893 -}
5894 -
5895 -/**
5896 - * Walk an ACF field value tree and collect attachment IDs for any value that
5897 - * resolves to a PDF in the WordPress media library. Handles the three shapes
5898 - * ACF returns for File/Image/URL fields (array with ID+url, integer attachment ID,
5899 - * plain URL string), and recurses through repeater/group/flexible content.
5900 - *
5901 - * @param mixed $value The ACF field value (any depth)
5902 - * @param array $out Accumulator (passed by reference) for attachment IDs
5903 - * @param int $depth Recursion guard
5904 - */
5905 -private function mxchat_collect_pdf_attachment_ids_from_acf_value($value, &$out, $depth = 0) {
5906 - if ($depth > 6) {
5907 - return; // prevent runaway recursion on circular/very-deep structures
5908 - }
5909 -
5910 - if (empty($value)) {
5911 - return;
5912 - }
5913 -
5914 - // Array shapes: ACF File/Image return value=array; repeaters/groups are arrays of arrays
5915 - if (is_array($value)) {
5916 - // Direct File/Image-style array (has 'url' and usually 'ID' + 'mime_type')
5917 - $looks_like_attachment = isset($value['url']) || isset($value['ID']) || isset($value['id']);
5918 - if ($looks_like_attachment) {
5919 - $att_id = 0;
5920 - if (!empty($value['ID']) && is_numeric($value['ID'])) {
5921 - $att_id = (int) $value['ID'];
5922 - } elseif (!empty($value['id']) && is_numeric($value['id'])) {
5923 - $att_id = (int) $value['id'];
5924 - } elseif (!empty($value['url']) && is_string($value['url'])) {
5925 - $att_id = (int) attachment_url_to_postid($value['url']);
5926 - }
5927 -
5928 - $is_pdf = false;
5929 - if (!empty($value['mime_type']) && $value['mime_type'] === 'application/pdf') {
5930 - $is_pdf = true;
5931 - } elseif (!empty($value['subtype']) && strtolower((string) $value['subtype']) === 'pdf') {
5932 - $is_pdf = true;
5933 - } elseif (!empty($value['url']) && is_string($value['url']) && $this->mxchat_url_looks_like_pdf($value['url'])) {
5934 - $is_pdf = true;
5935 - } elseif ($att_id && get_post_mime_type($att_id) === 'application/pdf') {
5936 - $is_pdf = true;
5937 - }
5938 -
5939 - if ($is_pdf && $att_id && get_post_mime_type($att_id) === 'application/pdf') {
5940 - $out[] = $att_id;
5941 - }
5942 - // An array node that represents one attachment doesn't contain other
5943 - // attachments inside it — done with this branch.
5944 - return;
5945 - }
5946 -
5947 - // Recurse: repeater rows, flexible-content layouts, groups, etc.
5948 - foreach ($value as $sub) {
5949 - $this->mxchat_collect_pdf_attachment_ids_from_acf_value($sub, $out, $depth + 1);
5950 - }
5951 - return;
5952 - }
5953 -
5954 - // Plain numeric attachment ID (ACF File field set to "Return: ID")
5955 - if (is_numeric($value)) {
5956 - $att_id = (int) $value;
5957 - if ($att_id > 0 && get_post_mime_type($att_id) === 'application/pdf') {
5958 - $out[] = $att_id;
5959 - }
5960 - return;
5961 - }
5962 -
5963 - // Plain string — URL pointing at a PDF (ACF File field set to "Return: URL", or a custom URL/text field)
5964 - if (is_string($value)) {
5965 - $trimmed = trim($value);
5966 - if ($trimmed !== '' && $this->mxchat_url_looks_like_pdf($trimmed)) {
5967 - $att_id = (int) attachment_url_to_postid($trimmed);
5968 - if ($att_id > 0 && get_post_mime_type($att_id) === 'application/pdf') {
5969 - $out[] = $att_id;
5970 - }
5971 - }
5972 - return;
5973 - }
5974 -}
5975 -
5976 -/**
5977 - * Heuristic: does this URL/string look like a PDF reference?
5978 - * Tolerates query strings and fragments (#page=2).
5979 - */
5980 -private function mxchat_url_looks_like_pdf($url) {
5981 - if (!is_string($url) || $url === '') {
5982 - return false;
5983 - }
5984 - // Strip query + fragment before checking extension
5985 - $path = preg_replace('/[?#].*$/', '', $url);
5986 - return (bool) preg_match('/\.pdf$/i', $path);
5987 -}
5988 -
5989 -/**
5990 - * Extract text from a PDF attachment by ID using the bundled Smalot parser.
5991 - * Reads the file directly from disk via get_attached_file (no HTTP fetch).
5992 - * Result is cached on the attachment as post_meta keyed by file mtime so we
5993 - * only parse the same PDF once unless the file changes on disk.
5994 - *
5995 - * @param int $attachment_id
5996 - * @return string Extracted plain text, or '' on failure.
5997 - */
5998 -private function mxchat_extract_pdf_text_by_attachment_id($attachment_id) {
5999 - $attachment_id = (int) $attachment_id;
6000 - if ($attachment_id <= 0) {
6001 - return '';
6002 - }
6003 - if (get_post_mime_type($attachment_id) !== 'application/pdf') {
6004 - return '';
6005 - }
6006 -
6007 - $pdf_path = get_attached_file($attachment_id);
6008 - if (empty($pdf_path) || !file_exists($pdf_path) || !is_readable($pdf_path)) {
6009 - return '';
6010 - }
6011 -
6012 - // Raw-file size cap. Parsing very large PDFs can OOM the request; skip with a log entry
6013 - // and let the rest of the ACF content land in the KB. Filterable for users who need it bigger.
6014 - $default_max_bytes = 25 * 1024 * 1024;
6015 - $max_bytes = (int) apply_filters('mxchat_acf_pdf_max_bytes', $default_max_bytes, $attachment_id, $pdf_path);
6016 - if ($max_bytes > 0) {
6017 - $file_size = @filesize($pdf_path);
6018 - if ($file_size !== false && $file_size > $max_bytes) {
6019 - error_log(sprintf(
6020 - '[mxchat] ACF PDF skipped (over size cap): attachment %d "%s" %d bytes > cap %d',
6021 - $attachment_id,
6022 - basename($pdf_path),
6023 - $file_size,
6024 - $max_bytes
6025 - ));
6026 - return '';
6027 - }
6028 - }
6029 -
6030 - $mtime = @filemtime($pdf_path);
6031 - $cache_meta_key = '_mxchat_acf_pdf_text_v1';
6032 - $cached = get_post_meta($attachment_id, $cache_meta_key, true);
6033 - if (is_array($cached) && isset($cached['mtime'], $cached['text']) && (int) $cached['mtime'] === (int) $mtime) {
6034 - return (string) $cached['text'];
6035 - }
6036 -
6037 - $text = '';
6038 - try {
6039 - if (function_exists('mxchat_load_pdf_parser')) {
6040 - mxchat_load_pdf_parser();
6041 - }
6042 - if (!class_exists('\\Smalot\\PdfParser\\Parser')) {
6043 - return '';
6044 - }
6045 - $parser = new \Smalot\PdfParser\Parser();
6046 - $pdf = $parser->parseFile($pdf_path);
6047 - $pages = $pdf->getPages();
6048 - $page_texts = array();
6049 - $acf_page_num = 0;
6050 - foreach ($pages as $page) {
6051 - $acf_page_num++;
6052 - $page_text = '';
6053 - try {
6054 - $page_text = $page->getText();
6055 - } catch (\Exception $e) {
6056 - $page_text = '';
6057 - }
6058 - if (!empty($page_text)) {
6059 - $page_text = MxChat_Utils::normalize_pdf_rtl($page_text, 'acf_pdf attachment ' . $attachment_id . ' page ' . $acf_page_num);
6060 - $page_texts[] = $page_text;
6061 - }
6062 - }
6063 - $text = trim(implode("\n\n", $page_texts));
6064 - } catch (\Exception $e) {
6065 - error_log('[mxchat] ACF PDF extraction failed for attachment ' . $attachment_id . ': ' . $e->getMessage());
6066 - return '';
6067 - } catch (\Throwable $e) {
6068 - error_log('[mxchat] ACF PDF extraction error for attachment ' . $attachment_id . ': ' . $e->getMessage());
6069 - return '';
6070 - }
6071 -
6072 - // Cap per-PDF text to avoid blowing up the embedding payload on enormous PDFs.
6073 - // The chunker downstream will still split this into multiple vectors.
6074 - $max_len = (int) apply_filters('mxchat_acf_pdf_text_max_length', 50000);
6075 - if ($max_len > 0 && strlen($text) > $max_len) {
6076 - $text = substr($text, 0, $max_len);
6077 - }
6078 -
6079 - update_post_meta($attachment_id, $cache_meta_key, array(
6080 - 'mtime' => (int) $mtime,
6081 - 'text' => $text,
6082 - ));
6083 -
6084 - return $text;
6085 -}
6086 -
6087 -/**
6088 - * Handle ACF save - fires after ACF fields are saved
6089 - * This ensures ACF field data is available when syncing to knowledge base
6090 - */
6091 -public function mxchat_handle_acf_save($post_id) {
6092 - // Skip if not a valid post
6093 - if (!$post_id || $post_id === 'options') {
6094 - return;
6095 - }
6096 -
6097 - // Skip autosaves and revisions
6098 - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
6099 - return;
6100 - }
6101 -
6102 - $post = get_post($post_id);
6103 - if (!$post) {
6104 - return;
6105 - }
6106 -
6107 - $post_type = $post->post_type;
6108 -
6109 - // Check if sync is enabled for this post type
6110 - $should_sync = false;
6111 -
6112 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
6113 - $should_sync = true;
6114 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
6115 - $should_sync = true;
6116 - } else if ($post_type === 'product' && class_exists('WooCommerce')) {
6117 - // WooCommerce products - check if WooCommerce integration is enabled
6118 - $options = get_option('mxchat_options', array());
6119 - if (isset($options['enable_woocommerce_integration']) &&
6120 - ($options['enable_woocommerce_integration'] === '1' || $options['enable_woocommerce_integration'] === 'on')) {
6121 - $should_sync = true;
6122 - }
6123 - } else {
6124 - // Check custom post types
6125 - $option_name = 'mxchat_auto_sync_' . $post_type;
6126 - if (get_option($option_name) === '1') {
6127 - $should_sync = true;
6128 - }
6129 - }
6130 -
6131 - if (!$should_sync) {
6132 - return;
6133 - }
6134 -
6135 - // Only process published posts
6136 - if ($post->post_status !== 'publish') {
6137 - return;
6138 - }
6139 -
6140 - // Check if this post has any ACF fields - if not, no need to re-sync
6141 - $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
6142 - if (empty($acf_fields)) {
6143 - return;
6144 - }
6145 -
6146 - // Use a transient to prevent duplicate processing (post_updated may have already run)
6147 - $transient_key = 'mxchat_acf_synced_' . $post_id;
6148 - if (get_transient($transient_key)) {
6149 - return;
6150 - }
6151 - set_transient($transient_key, true, 60); // Prevent re-processing for 60 seconds
6152 -
6153 - // Re-run the sync with ACF data now available
6154 - // We pass $update=true since this is effectively an update with ACF data
6155 - $this->mxchat_handle_post_update($post_id, $post, true);
6156 -}
6157 -
6158 -public function mxchat_handle_post_update($post_id, $post, $update) {
6159 - // The in-flight-update marker has done its job the moment post_updated runs; drop it
6160 - // before any early return so it can never outlive its own save (a failed $wpdb->update
6161 - // inside wp_insert_post returns after pre_post_update but before the transition).
6162 - unset($this->pending_post_update[$post_id]);
6163 -
6164 - // Basic validation checks
6165 - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
6166 - return;
6167 - }
6168 -
6169 - $post_type = $post->post_type;
6170 -
6171 - // Check if sync is enabled for this post type
6172 - $should_sync = false;
6173 -
6174 - // Check built-in post types first
6175 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
6176 - $should_sync = true;
6177 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
6178 - $should_sync = true;
6179 - } else {
6180 - // Check custom post types
6181 - $option_name = 'mxchat_auto_sync_' . $post_type;
6182 - if (get_option($option_name) === '1') {
6183 - $should_sync = true;
6184 - }
6185 - }
6186 -
6187 - if (!$should_sync) {
6188 - return;
6189 - }
6190 -
6191 - // Check if we have stored the previous status and URL in our transients
6192 - $previous_status_key = 'mxchat_prev_status_' . $post_id;
6193 - $previous_status = get_transient($previous_status_key);
6194 -
6195 - $previous_url_key = 'mxchat_prev_url_' . $post_id;
6196 - $previous_url = get_transient($previous_url_key);
6197 -
6198 - // If the post was previously published but is now not published, remove from knowledge base
6199 - if ($previous_status === 'publish' && $post->post_status !== 'publish') {
6200 - // Use the stored URL from when it was published, or fall back to current permalink
6201 - $source_url = $previous_url ?: get_permalink($post_id);
6202 -
6203 - // mxchat_handle_status_transition already deleted for this post earlier in this
6204 - // request (it fires first inside wp_insert_post); skip the redundant round-trip.
6205 - if ($source_url && empty($this->transition_deleted_posts[$post_id])) {
6206 - // Chunk-aware deletion (routes to Pinecone or WP DB and removes base + all chunks)
6207 - MxChat_Utils::delete_chunks_for_url($source_url, 'default');
6208 - }
6209 -
6210 - // Clean up the transients and exit early
6211 - delete_transient($previous_status_key);
6212 - delete_transient($previous_url_key);
6213 - return;
6214 - }
6215 -
6216 - // Slug/permalink rename while still published: delete the old vectors before upserting new ones.
6217 - // Without this, md5(old_url) vectors (base + chunks) would be orphaned under the stale URL.
6218 - if ($post->post_status === 'publish' && !empty($previous_url)) {
6219 - $current_url = get_permalink($post_id);
6220 - if ($current_url && $current_url !== $previous_url) {
6221 - MxChat_Utils::delete_chunks_for_url($previous_url, 'default');
6222 - }
6223 - }
6224 -
6225 - // Store the current status for next time (if this is an update)
6226 - if ($update) {
6227 - set_transient($previous_status_key, $post->post_status, DAY_IN_SECONDS);
6228 -
6229 - // If the post is currently published, also store its URL
6230 - if ($post->post_status === 'publish') {
6231 - $current_url = get_permalink($post_id);
6232 - set_transient($previous_url_key, $current_url, DAY_IN_SECONDS);
6233 - }
6234 - }
6235 -
6236 - // Only process currently published content for adding/updating.
6237 - // transition_indexed_posts: mxchat_handle_status_transition's arrival edge may have
6238 - // already indexed this post earlier in this request (editor publishes fire
6239 - // transition_post_status first, then post_updated) — skip the duplicate embed.
6240 - // Consume-once: the flag is cleared when honoured, so a LATER save of the same
6241 - // post in one long-running process (WP-CLI scripts, importers) re-indexes normally.
6242 - if ($post->post_status === 'publish') {
6243 - if (!empty($this->transition_indexed_posts[$post_id])) {
6244 - unset($this->transition_indexed_posts[$post_id]);
6245 - } else {
6246 - $this->mxchat_index_published_post($post_id, $post);
6247 - }
6248 - }
6249 -
6250 - // Clean up the stored previous status if not used above
6251 - if ($previous_status !== 'publish' || $post->post_status === 'publish') {
6252 - delete_transient($previous_status_key);
6253 - delete_transient($previous_url_key);
6254 - }
6255 -}
6256 -
6257 -/**
6258 - * Index a published post into the knowledge base: preprocessing filter, content
6259 - * assembly (title/excerpt/body), WooCommerce product enrichment, job_listing meta,
6260 - * ACF fields (+ optional PDF extraction), whitelisted custom meta, embedding and
6261 - * upsert, then tag-based role restriction.
6262 - *
6263 - * Shared by the post_updated auto-sync path (mxchat_handle_post_update) and the
6264 - * transition_post_status arrival edge (mxchat_handle_status_transition), so
6265 - * scheduled publishes (wp_publish_post) and direct status=publish inserts index
6266 - * identically to editor saves (plan 3055e1). Pure extraction of the former
6267 - * publish branch — body indentation retained to keep the diff reviewable.
6268 - */
6269 -private function mxchat_index_published_post($post_id, $post) {
6270 - $post_type = $post->post_type;
6271 -
6272 - // WooCommerce products are owned by the WC-object assembler (plan a3d60c):
6273 - // whenever WooCommerce is active AND the integration is enabled, every
6274 - // product save also fires save_post_product, which queues
6275 - // mxchat_store_product_embedding on shutdown — and that writer runs LAST,
6276 - // overwriting the same md5(permalink) row this path would write. Assembling
6277 - // and embedding the product here was pure duplicate spend (measured: two
6278 - // embedding calls per product save, second one wins). Skip ONLY under the
6279 - // exact conditions the shutdown writer runs — same option read as its own
6280 - // gate — because with the integration off (or WooCommerce inactive) this
6281 - // path is the sole product indexer and must keep working.
6282 - if ($post_type === 'product'
6283 - && class_exists('WooCommerce')
6284 - && isset($this->options['enable_woocommerce_integration'])
6285 - && in_array($this->options['enable_woocommerce_integration'], ['1', 'on'])) {
6286 - return;
6287 - }
6288 -
6289 - // Get the source URL
6290 - $source_url = get_permalink($post_id);
6291 -
6292 - // A draft published programmatically (wp_publish_post) can reach this
6293 - // point with an EMPTY post_name — wp_insert_post skips slug generation
6294 - // for draft/pending — and get_permalink() then resolves to the bare
6295 - // site root. A knowledge row keyed to the homepage cites the wrong URL
6296 - // and answers homepage questions with this post's body, so refuse to
6297 - // write it; the post indexes correctly on its next save, once the slug
6298 - // exists. The empty-post_name test is what keeps a legitimate static
6299 - // front page (which has a slug but a root permalink) indexable.
6300 - // (Plan d138c4.)
6301 - if ('' === $post->post_name
6302 - && untrailingslashit($source_url) === untrailingslashit(home_url())) {
6303 - return;
6304 - }
6305 -
6306 - /**
6307 - * Allow developers to modify post data before processing into the knowledge base.
6308 - * Same filter and signature as the manual bulk-import path
6309 - * (ajax_mxchat_process_selected_content), so a callback registered once covers
6310 - * every indexing route. Purely additive — zero behaviour change when unhooked.
6311 - * Auto-sync runs under the 'default' bot context, matching the rest of this
6312 - * function.
6313 - *
6314 - * @param WP_Post $post The post about to be indexed.
6315 - * @param string $bot_id Bot context ('default' on auto-sync).
6316 - */
6317 - $post = apply_filters('mxchat_before_process_post', $post, 'default');
6318 - if (!($post instanceof WP_Post)) {
6319 - $post = get_post($post_id); // defend against a bad callback return
6320 - }
6321 -
6322 - // Assemble the indexable text via the shared post-kind assembler (a3d60c),
6323 - // reading from the FILTERED post object — not re-fetched by ID, which would
6324 - // discard it. Auto-sync reads content/excerpt in its historical
6325 - // get_post_field() display context, never appended product custom tabs
6326 - // (its product branch is reachable only with the WooCommerce integration
6327 - // off), and gates ACF→PDF extraction behind its own opt-in option —
6328 - // default OFF, because re-parsing every ACF PDF on every editor save is
6329 - // expensive and most sites don't want it (the 25 MB size cap lives in the
6330 - // shared extractor either way).
6331 - $prepared = $this->mxchat_prepare_post_content_for_indexing($post_id, $post, array(
6332 - 'read_display' => true,
6333 - 'extract_acf_pdfs' => get_option('mxchat_auto_sync_acf_pdfs', '0') === '1',
6334 - 'include_product_tabs' => false,
6335 - ));
6336 - $final_content = $prepared['content'];
6337 -
6338 - // Embedding decision — custom-provider-aware. Gating on a cloud API key
6339 - // here silently killed auto-sync on keyless custom-embeddings sites,
6340 - // because generate_embedding() routes custom FIRST and never needs the
6341 - // key (plan cbd5fd). Silent-return shape preserved.
6342 - $preflight = MxChat_Utils::embedding_preflight(get_option('mxchat_options'));
6343 - if (!$preflight['ok']) {
6344 - return;
6345 - }
6346 - $api_key = $preflight['api_key'];
6347 -
6348 - // Use the centralized utility function for storage
6349 - $result = MxChat_Utils::submit_content_to_db(
6350 - $final_content,
6351 - $source_url,
6352 - $api_key,
6353 - md5($source_url) // Vector ID for Pinecone
6354 - );
6355 -
6356 - // After successful storage, apply role restriction based on tags
6357 - if (!is_wp_error($result)) {
6358 - $this->apply_role_restriction_to_post($post_id, $source_url);
6359 - }
6360 -}
6361 -
6362 -/**
6363 - * Shared post-fields content assembler (plan a3d60c) — the ONE body behind both
6364 - * post-kind ingestion paths: manual bulk import (ajax_mxchat_process_selected_content)
6365 - * and auto-sync (mxchat_index_published_post). Behavior-preserving extraction; the
6366 - * measured per-caller differences ride $args instead of living as drifting copies:
6367 - *
6368 - * 'read_display' bool Auto-sync historically reads content/excerpt via
6369 - * get_post_field() in its default 'display' context
6370 - * (the post_content / post_excerpt display filters
6371 - * fire); bulk import reads the raw properties. Inert
6372 - * on a stock install — preserved per-path, not converged.
6373 - * 'extract_acf_pdfs' bool Each caller passes its OWN option (bulk:
6374 - * mxchat_acf_pdf_extraction; auto-sync:
6375 - * mxchat_auto_sync_acf_pdfs) — the two-option design
6376 - * is deliberate (plan 11720c). Gates BOTH the PDF-id
6377 - * collection walk and the extraction loop; the ids are
6378 - * only ever read inside the extraction branch, so
6379 - * gating collection is output-identical on every install.
6380 - * 'include_product_tabs' bool The bulk path has always appended yikes_woo custom
6381 - * tabs to product content; the auto-sync product branch
6382 - * (reachable only with the WooCommerce integration off)
6383 - * never did. Preserved per-path — converging it would be
6384 - * a behavior change, recorded on the plan instead.
6385 - *
6386 - * Returns array: 'content' (the assembled indexable text), 'acf_fields_found' and
6387 - * 'pdf_extracted_count' (the bulk path reports both in its AJAX response).
6388 - */
6389 -private function mxchat_prepare_post_content_for_indexing($post_id, $post, $args) {
6390 - $read_display = !empty($args['read_display']);
6391 - $extract_acf_pdfs = !empty($args['extract_acf_pdfs']);
6392 - $include_product_tabs = !empty($args['include_product_tabs']);
6393 -
6394 - // Raw post_title, NOT get_the_title(): the_title applies wptexturize +
6395 - // convert_chars and prepends the "Protected:" / "Private:" display chrome.
6396 - // The knowledge base stores facts, not display strings. Entity decode at
6397 - // output time (single-pass, shared helper) — a stored `&amp;` embeds worse
6398 - // than `&` and gets quoted back to visitors (d2c92e).
6399 - $content = $this->mxchat_decode_entities_for_indexing($post->post_title) . "\n\n";
6400 -
6401 - $raw_excerpt = $read_display ? get_post_field('post_excerpt', $post) : $post->post_excerpt;
6402 - $raw_content = $read_display ? get_post_field('post_content', $post) : $post->post_content;
6403 -
6404 - // Add short description if it exists (WooCommerce products use post_excerpt for short description)
6405 - // Strip FIRST, then test: an excerpt that is nothing but shortcodes strips to
6406 - // empty, and testing the raw value emitted a bare "Short Description: " label
6407 - // with no value after it. trim() only in the TEST — the emitted value is
6408 - // untouched, so a populated excerpt is byte-identical to before. A
6409 - // whitespace-only excerpt is an empty excerpt and must not produce a labelled
6410 - // line with nothing after it.
6411 - $clean_excerpt = $this->strip_shortcode_tags_preserve_content($raw_excerpt);
6412 - if (trim($clean_excerpt) !== '') {
6413 - $content .= "Short Description: " . $this->mxchat_decode_entities_for_indexing(wp_strip_all_tags($clean_excerpt)) . "\n\n";
6414 - }
6415 -
6416 - // Main content — remove shortcode tags but preserve content inside them, then
6417 - // strip tags (don't use 'the_content' filter as it may re-add shortcodes).
6418 - $clean_content = $this->strip_shortcode_tags_preserve_content($raw_content);
6419 - $content .= $this->mxchat_decode_entities_for_indexing(wp_strip_all_tags($clean_content));
6420 -
6421 - // WooCommerce product enrichment (post-fields kind). The WC-object assembler
6422 - // (mxchat_prepare_product_content_for_indexing) owns product rows whenever the
6423 - // integration is on; this branch serves the bulk import (all configurations)
6424 - // and auto-sync with the integration off.
6425 - if (get_post_type($post_id) === 'product' && class_exists('WooCommerce')) {
6426 - $product = wc_get_product($post_id);
6427 -
6428 - if ($product) {
6429 - $content .= "\n";
6430 - $content .= $this->mxchat_woo_product_summary_lines($product);
6431 - }
6432 -
6433 - if ($include_product_tabs) {
6434 - $content .= $this->mxchat_woo_custom_tabs_text($post_id);
6435 - }
6436 - }
6437 -
6438 - // For custom post types like job_listing, include additional fields
6439 - if (get_post_type($post_id) === 'job_listing') {
6440 - // Add job-specific meta if available
6441 - $job_location = get_post_meta($post_id, '_job_location', true);
6442 - if (!empty($job_location)) {
6443 - $content .= "\n\nLocation: " . $job_location;
6444 - }
6445 -
6446 - // Get job type terms
6447 - $job_types = get_the_terms($post_id, 'job_listing_type');
6448 - if (!empty($job_types) && !is_wp_error($job_types)) {
6449 - $types = array();
6450 - foreach ($job_types as $type) {
6451 - $types[] = $type->name;
6452 - }
6453 - $content .= "\n\nJob Type: " . implode(', ', $types);
6454 - }
6455 -
6456 - // Get company name if available
6457 - $company_name = get_post_meta($post_id, '_company_name', true);
6458 - if (!empty($company_name)) {
6459 - $content .= "\n\nCompany: " . $company_name;
6460 - }
6461 - }
6462 -
6463 - // ADD ACF FIELDS SUPPORT
6464 - $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
6465 - $pdf_extracted_count = 0;
6466 - if (!empty($acf_fields)) {
6467 - $acf_content_parts = array();
6468 - $pdf_attachment_ids = array();
6469 -
6470 - foreach ($acf_fields as $field_name => $field_value) {
6471 - $formatted_value = $this->mxchat_format_acf_field_value($field_value, $field_name, $post_id);
6472 -
6473 - if (!empty($formatted_value)) {
6474 - // Both separators: a hyphenated ACF name should read as words.
6475 - $field_label = ucwords(str_replace(['_', '-'], ' ', $field_name));
6476 - $acf_content_parts[] = $field_label . ": " . $formatted_value;
6477 - }
6478 -
6479 - // Walk this field's value tree for any PDF attachment references and
6480 - // queue them for extraction — only when this caller's PDF option is on.
6481 - if ($extract_acf_pdfs) {
6482 - $this->mxchat_collect_pdf_attachment_ids_from_acf_value($field_value, $pdf_attachment_ids);
6483 - }
6484 - }
6485 -
6486 - if (!empty($acf_content_parts)) {
6487 - $content .= "\n\n" . implode("\n", $acf_content_parts);
6488 - }
6489 -
6490 - // Extract text from each unique PDF found in ACF fields and append as a labeled section
6491 - if ($extract_acf_pdfs && !empty($pdf_attachment_ids)) {
6492 - $pdf_attachment_ids = array_unique(array_filter(array_map('intval', $pdf_attachment_ids)));
6493 - $pdf_sections = array();
6494 - foreach ($pdf_attachment_ids as $att_id) {
6495 - $pdf_text = $this->mxchat_extract_pdf_text_by_attachment_id($att_id);
6496 - if (!empty($pdf_text)) {
6497 - $pdf_title = get_the_title($att_id);
6498 - $pdf_url = wp_get_attachment_url($att_id);
6499 - $header = 'PDF Attachment';
6500 - if (!empty($pdf_title)) {
6501 - $header .= ': ' . $pdf_title;
6502 - }
6503 - if (!empty($pdf_url)) {
6504 - $header .= ' (' . $pdf_url . ')';
6505 - }
6506 - $pdf_sections[] = $header . "\n" . $pdf_text;
6507 - $pdf_extracted_count++;
6508 - }
6509 - }
6510 - if (!empty($pdf_sections)) {
6511 - $content .= "\n\nAttached PDFs:\n" . implode("\n\n", $pdf_sections);
6512 - }
6513 - }
6514 - }
6515 -
6516 - // ADD CUSTOM POST META SUPPORT (whitelisted non-ACF meta fields)
6517 - $custom_meta = $this->mxchat_get_whitelisted_post_meta($post_id);
6518 - if (!empty($custom_meta)) {
6519 - $meta_content_parts = array();
6520 -
6521 - foreach ($custom_meta as $meta_key => $meta_value) {
6522 - // Convert meta key to readable label
6523 - $meta_label = ucwords(str_replace(array('_', '-'), ' ', $meta_key));
6524 - $meta_content_parts[] = $meta_label . ": " . $meta_value;
6525 - }
6526 -
6527 - if (!empty($meta_content_parts)) {
6528 - $content .= "\n\n" . implode("\n", $meta_content_parts);
6529 - }
6530 - }
6531 -
6532 - return array(
6533 - 'content' => $content,
6534 - 'acf_fields_found' => count($acf_fields),
6535 - 'pdf_extracted_count' => $pdf_extracted_count,
6536 - );
6537 -}
6538 -
6539 -/**
6540 - * Shared WC-object product assembler (plan a3d60c) — the ONE body behind the two
6541 - * WooCommerce-object ingestion paths: the auto-sync product writer
6542 - * (mxchat_store_product_embedding) and the URL/sitemap product import
6543 - * (mxchat_extract_woocommerce_product_content). Assembles from the WC_Product,
6544 - * the authoritative source for product rows (scope decision on the plan).
6545 - */
6546 -private function mxchat_prepare_product_content_for_indexing($product) {
6547 - $title = $product->get_name();
6548 - $description = $product->get_description();
6549 - $short_description = $product->get_short_description();
6550 -
6551 - // Format content consistently
6552 - $content = $title . "\n\n";
6553 -
6554 - if (!empty($short_description)) {
6555 - $content .= "Short Description: " . wp_strip_all_tags($short_description) . "\n\n";
6556 - }
6557 -
6558 - if (!empty($description)) {
6559 - $content .= wp_strip_all_tags($description) . "\n\n";
6560 - }
6561 -
6562 - $content .= $this->mxchat_woo_product_summary_lines($product);
6563 - $content .= $this->mxchat_woo_custom_tabs_text($product->get_id());
6564 -
6565 - return $content;
6566 -}
6567 -
6568 -/**
6569 - * Pricing + SKU + categories lines for a product — shared by both assembler kinds
6570 - * (the post-fields product enrichment and the WC-object assembler).
6571 - */
6572 -private function mxchat_woo_product_summary_lines($product) {
6573 - // Add pricing information (base-currency pinned — see mxchat_product_price_lines)
6574 - $lines = $this->mxchat_product_price_lines($product);
6575 -
6576 - $sku = $product->get_sku();
6577 - if (!empty($sku)) {
6578 - $lines .= "SKU: " . $sku . "\n";
6579 - }
6580 -
6581 - // Get product categories
6582 - $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'names'));
6583 - if (!empty($categories) && !is_wp_error($categories)) {
6584 - $lines .= "Categories: " . implode(', ', $categories) . "\n";
6585 - }
6586 -
6587 - return $lines;
6588 -}
6589 -
6590 -/**
6591 - * Custom Product Tabs text (supports "Custom Product Tabs for WooCommerce" by
6592 - * Code Parrots) — direct tabs plus applied reusable/saved tabs. The ONE copy of
6593 - * the yikes_woo logic; three sites carried byte-identical clones before a3d60c.
6594 - */
6595 -private function mxchat_woo_custom_tabs_text($product_id) {
6596 - $text = '';
6597 -
6598 - $custom_tabs = get_post_meta($product_id, 'yikes_woo_products_tabs', true);
6599 - if (!empty($custom_tabs) && is_array($custom_tabs)) {
6600 - foreach ($custom_tabs as $tab) {
6601 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
6602 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
6603 -
6604 - if (!empty($tab_title) && !empty($tab_content)) {
6605 - $text .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
6606 - }
6607 - }
6608 - }
6609 -
6610 - // Also check for reusable/saved tabs applied to this product
6611 - $applied_saved_tabs = get_post_meta($product_id, 'yikes_woo_reusable_products_tabs_applied', true);
6612 - if (!empty($applied_saved_tabs) && is_array($applied_saved_tabs)) {
6613 - $saved_tabs = get_option('yikes_woo_reusable_products_tabs', array());
6614 - if (!empty($saved_tabs) && is_array($saved_tabs)) {
6615 - foreach ($applied_saved_tabs as $saved_tab_id) {
6616 - if (isset($saved_tabs[$saved_tab_id])) {
6617 - $tab = $saved_tabs[$saved_tab_id];
6618 - $tab_title = isset($tab['title']) ? $tab['title'] : (isset($tab['tab_title']) ? $tab['tab_title'] : '');
6619 - $tab_content = isset($tab['content']) ? $tab['content'] : '';
6620 -
6621 - if (!empty($tab_title) && !empty($tab_content)) {
6622 - $text .= "\n" . $tab_title . ": " . wp_strip_all_tags($tab_content) . "\n";
6623 - }
6624 - }
6625 - }
6626 - }
6627 - }
6628 -
6629 - return $text;
6630 -}
6631 -
6632 -/**
6633 - * Store the post status and URL before update to detect status transitions
6634 - * This runs before the post is actually updated in the database
6635 - */
6636 -public function mxchat_store_pre_update_status($post_id, $data) {
6637 - // Core is inside wp_insert_post's update branch, so a post_updated WILL fire later
6638 - // this request and can consume the arrival-edge guard (plan a664f3).
6639 - $this->pending_post_update[$post_id] = true;
6640 -
6641 - // Get the current post from database (before update)
6642 - $current_post = get_post($post_id);
6643 -
6644 - if ($current_post) {
6645 - // Store the current status temporarily
6646 - $status_key = 'mxchat_prev_status_' . $post_id;
6647 - set_transient($status_key, $current_post->post_status, HOUR_IN_SECONDS);
6648 -
6649 - // If the post is currently published, also store its URL
6650 - if ($current_post->post_status === 'publish') {
6651 - $url_key = 'mxchat_prev_url_' . $post_id;
6652 - $current_url = get_permalink($post_id);
6653 - set_transient($url_key, $current_url, HOUR_IN_SECONDS);
6654 - }
6655 - }
6656 -}
6657 -
6658 -/**
6659 - * Whether auto-sync is enabled for a post type (mirrors the checks used by the
6660 - * update/delete handlers; kept as one helper so new call sites cannot drift).
6661 - */
6662 -private function mxchat_is_auto_sync_enabled($post_type) {
6663 - if ($post_type === 'post') {
6664 - return get_option('mxchat_auto_sync_posts') === '1';
6665 - }
6666 - if ($post_type === 'page') {
6667 - return get_option('mxchat_auto_sync_pages') === '1';
6668 - }
6669 - return get_option('mxchat_auto_sync_' . $post_type) === '1';
6670 -}
6671 -
6672 -/**
6673 - * Remove a post's vectors the moment it leaves 'publish', using the authoritative
6674 - * old status core passes to transition_post_status — no transient involved (plan 816fb1).
6675 - *
6676 - * Covers status changes that never route through wp_update_post (scheduled-expiry
6677 - * plugins and others that flip post_status directly and call wp_transition_post_status),
6678 - * where neither pre_post_update nor post_updated fires and the old detection missed.
6679 - */
6680 -public function mxchat_handle_status_transition($new_status, $old_status, $post) {
6681 - if (!($post instanceof WP_Post) || wp_is_post_revision($post->ID)) {
6682 - return;
6683 - }
6684 -
6685 - // Arrival edge (plan 3055e1): a post BECOMING published is indexed here, because
6686 - // wp_publish_post() — the path scheduled posts take via check_and_publish_future_post —
6687 - // and direct wp_insert_post(status=publish) creates never fire post_updated, so the
6688 - // auto-sync ADD path alone misses them. Editor publishes also pass through here;
6689 - // the transition_indexed_posts guard keeps mxchat_handle_post_update from embedding
6690 - // a second time in the same request.
6691 - if ($new_status === 'publish' && $old_status !== 'publish') {
6692 - if ($this->mxchat_is_auto_sync_enabled($post->post_type)) {
6693 - $this->mxchat_index_published_post($post->ID, $post);
6694 -
6695 - // Arm the double-fire guard ONLY when a post_updated is actually coming to
6696 - // consume it (plan a664f3). Two publish paths never fire post_updated at all:
6697 - // a direct wp_insert_post(status=publish) create, and wp_publish_post() — the
6698 - // call check_and_publish_future_post() makes for scheduled posts. Arming the
6699 - // guard unconditionally left it set with nothing to consume it, so the NEXT
6700 - // update of that post was swallowed entirely: zero embed calls, no knowledge
6701 - // -base row, silently. Consume-once on this side too, so a guard can never
6702 - // outlive the single save it was armed for.
6703 - if (!empty($this->pending_post_update[$post->ID])) {
6704 - unset($this->pending_post_update[$post->ID]);
6705 - $this->transition_indexed_posts[$post->ID] = true;
6706 - }
6707 - }
6708 - return;
6709 - }
6710 -
6711 - // Only the publish -> not-publish edge matters here.
6712 - if ($old_status !== 'publish' || $new_status === 'publish') {
6713 - return;
6714 - }
6715 - // Trash is handled by mxchat_handle_post_delete (wp_trash_post) with pre-trash URL
6716 - // resolution; skip to avoid a second network round-trip per trash.
6717 - if ($new_status === 'trash') {
6718 - return;
6719 - }
6720 - if (!$this->mxchat_is_auto_sync_enabled($post->post_type)) {
6721 - return;
6722 - }
6723 -
6724 - $urls = array();
6725 -
6726 - // The DB may already hold the new status when this fires, so get_permalink() on the
6727 - // live post could build a draft-style URL whose md5 misses the stored vector IDs.
6728 - // Reconstruct the published permalink from a clone instead.
6729 - $published_clone = clone $post;
6730 - $published_clone->post_status = 'publish';
6731 - $published_url = get_permalink($published_clone);
6732 - if ($published_url) {
6733 - $urls[] = $published_url;
6734 - }
6735 -
6736 - // Honour the pre-update capture when present (covers a slug change in the same save).
6737 - $previous_url = get_transient('mxchat_prev_url_' . $post->ID);
6738 - if (!empty($previous_url)) {
6739 - $urls[] = $previous_url;
6740 - }
6741 -
6742 - foreach (array_unique($urls) as $url) {
6743 - MxChat_Utils::delete_chunks_for_url($url, 'default');
6744 - }
6745 -
6746 - if (!empty($urls)) {
6747 - $this->transition_deleted_posts[$post->ID] = true;
6748 - }
6749 -}
6750 -
6751 -/**
6752 - * WP-CLI: remove knowledge-base entries left behind by posts that were unpublished,
6753 - * trashed, or made private before the transition_post_status handler existed.
6754 - *
6755 - * Walks every auto-synced post type's non-published posts, reconstructs each one's
6756 - * published-era permalink, and deletes its vectors (routes to Pinecone or the WP table).
6757 - * Deletion is idempotent, so never-indexed posts are a cheap no-op.
6758 - *
6759 - * ## OPTIONS
6760 - *
6761 - * [--dry-run]
6762 - * : Report what would be removed without deleting anything.
6763 - *
6764 - * ## EXAMPLES
6765 - *
6766 - * wp mxchat prune-unpublished --dry-run
6767 - * wp mxchat prune-unpublished
6768 - */
6769 -public function cli_prune_unpublished($args, $assoc_args) {
6770 - global $wpdb;
6771 - $dry_run = !empty($assoc_args['dry-run']);
6772 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
6773 -
6774 - $candidate_types = array_merge(array('post', 'page'), array_values(get_post_types(array('_builtin' => false), 'names')));
6775 - $synced_types = array();
6776 - foreach ($candidate_types as $type) {
6777 - if ($this->mxchat_is_auto_sync_enabled($type)) {
6778 - $synced_types[] = $type;
6779 - }
6780 - }
6781 - if (empty($synced_types)) {
6782 - WP_CLI::success('No post types have auto-sync enabled; nothing to prune.');
6783 - return;
6784 - }
6785 -
6786 - $scanned = 0;
6787 - $pruned = 0;
6788 - $paged = 1;
6789 - do {
6790 - $query = new WP_Query(array(
6791 - 'post_type' => $synced_types,
6792 - 'post_status' => array('draft', 'pending', 'private', 'future', 'trash'),
6793 - 'posts_per_page' => 100,
6794 - 'paged' => $paged,
6795 - 'fields' => 'ids',
6796 - ));
6797 - foreach ($query->posts as $post_id) {
6798 - $post = get_post($post_id);
6799 - if (!$post) {
6800 - continue;
6801 - }
6802 - $scanned++;
6803 -
6804 - // Rebuild the permalink the post had while published: publish-status clone,
6805 - // with wp_trash_post's __trashed slug suffix stripped for trashed posts.
6806 - $clone = clone $post;
6807 - $clone->post_status = 'publish';
6808 - if (substr($clone->post_name, -9) === '__trashed') {
6809 - $clone->post_name = substr($clone->post_name, 0, -9);
6810 - }
6811 - $url = get_permalink($clone);
6812 - if (!$url) {
6813 - continue;
6814 - }
6815 -
6816 - // Local-table row count is exact in WordPress-DB mode; in Pinecone mode it
6817 - // reads 0 but the delete below still routes to Pinecone and is idempotent.
6818 - $local_rows = (int) $wpdb->get_var($wpdb->prepare(
6819 - "SELECT COUNT(*) FROM {$table} WHERE source_url = %s", $url
6820 - ));
6821 -
6822 - if ($dry_run) {
6823 - if ($local_rows > 0) {
6824 - WP_CLI::log(sprintf('Would remove %d row(s): %s (post %d, %s)', $local_rows, $url, $post_id, $post->post_status));
6825 - $pruned += $local_rows;
6826 - }
6827 - continue;
6828 - }
6829 -
6830 - MxChat_Utils::delete_chunks_for_url($url, 'default');
6831 - if ($local_rows > 0) {
6832 - WP_CLI::log(sprintf('Removed %d row(s): %s (post %d, %s)', $local_rows, $url, $post_id, $post->post_status));
6833 - $pruned += $local_rows;
6834 - }
6835 - }
6836 - $more = $paged < $query->max_num_pages;
6837 - $paged++;
6838 - } while ($more);
6839 -
6840 - WP_CLI::success(sprintf(
6841 - '%s %d local knowledge row(s) across %d non-published post(s) scanned.%s',
6842 - $dry_run ? 'Would remove' : 'Removed',
6843 - $pruned,
6844 - $scanned,
6845 - ' (Pinecone-mode deletions are not counted locally.)'
6846 - ));
6847 -}
6848 -
6849 -/**
6850 - * WP-CLI: repair knowledge-base rows whose PDF text was imported in visual
6851 - * (reversed) order before the RTL normalizer existed. 32bf9e fixed new
6852 - * imports only; this fixes rows already in the table without the customer
6853 - * having to re-source and re-upload the original PDFs (plan d1e6f7).
6854 - *
6855 - * Detection reuses MxChat_Utils::normalize_pdf_rtl() on the stored text: a
6856 - * row is a candidate exactly when the normalizer would change it, so the
6857 - * import-time heuristic and the repair heuristic can never disagree.
6858 - * Repaired rows are RE-EMBEDDED — the stored vector was computed over
6859 - * reversed text and is as broken as the text — so a wet run calls the
6860 - * embedding provider once per repaired row on the site's API key. Runs
6861 - * beyond 25 rows therefore require --yes.
6862 - *
6863 - * Scope notes:
6864 - * - Scans the WordPress knowledge table. Pinecone-mode entries live in
6865 - * Pinecone, not this table, and are not scanned; if a scanned row's bot
6866 - * ALSO has Pinecone enabled (hybrid drift), the repaired entry is
6867 - * re-submitted through the normal import path so the md5-keyed Pinecone
6868 - * vector is replaced too.
6869 - * - Knowledge rows do not carry a bot id; --bot only selects whose
6870 - * embedding configuration (model + key) is used for re-embedding.
6871 - * - The mxchat_pdf_rtl_normalize filter is honoured: a site that disabled
6872 - * normalization gets detections of zero, not surprise rewrites.
6873 - * - The metadata header the PDF importer stores before the text separator
6874 - * is preserved byte-identical; only the text segment is repaired.
6875 - *
6876 - * ## OPTIONS
6877 - *
6878 - * [--dry-run]
6879 - * : List the rows that would be repaired without changing anything.
6880 - *
6881 - * [--bot=<id>]
6882 - * : Embedding configuration to use for re-embedding. Default: default.
6883 - *
6884 - * [--all-content]
6885 - * : Scan every row containing right-to-left text, not just rows with PDF
6886 - * provenance (a page anchor in the source URL, or pdf content type).
6887 - *
6888 - * [--yes]
6889 - * : Proceed even when more than 25 rows need re-embedding (API cost gate).
6890 - *
6891 - * ## EXAMPLES
6892 - *
6893 - * wp mxchat rtl-repair --dry-run
6894 - * wp mxchat rtl-repair
6895 - * wp mxchat rtl-repair --all-content --yes
6896 - */
6897 -public function cli_rtl_repair($args, $assoc_args) {
6898 - global $wpdb;
6899 - $dry_run = !empty($assoc_args['dry-run']);
6900 - $all = !empty($assoc_args['all-content']);
6901 - $yes = !empty($assoc_args['yes']);
6902 - $bot_id = isset($assoc_args['bot']) ? sanitize_key($assoc_args['bot']) : 'default';
6903 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
6904 -
6905 - // Detection pass — no API calls. Walk the table in id batches so a large
6906 - // knowledge base never loads at once.
6907 - $rtl_re = '/[\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0750}-\x{077F}\x{FB50}-\x{FDFF}\x{FE70}-\x{FEFF}]/u';
6908 - $candidates = array();
6909 - $scanned = 0;
6910 - $last_id = 0;
6911 - do {
6912 - if ($all) {
6913 - $rows = $wpdb->get_results($wpdb->prepare(
6914 - "SELECT id, article_content, source_url, content_type FROM {$table}
6915 - WHERE id > %d ORDER BY id ASC LIMIT 200",
6916 - $last_id
6917 - ));
6918 - } else {
6919 - $rows = $wpdb->get_results($wpdb->prepare(
6920 - "SELECT id, article_content, source_url, content_type FROM {$table}
6921 - WHERE id > %d AND (source_url LIKE %s OR content_type = 'pdf')
6922 - ORDER BY id ASC LIMIT 200",
6923 - $last_id,
6924 - '%' . $wpdb->esc_like('#page=') . '%'
6925 - ));
6926 - }
6927 - foreach ($rows as $row) {
6928 - $last_id = (int) $row->id;
6929 - $scanned++;
6930 - $content = (string) $row->article_content;
6931 - if (!preg_match($rtl_re, $content)) {
6932 - continue;
6933 - }
6934 - list($header, $text) = $this->mxchat_rtl_repair_split($content);
6935 - $normalized = MxChat_Utils::normalize_pdf_rtl($text, 'rtl-repair row ' . $row->id);
6936 - if (is_string($normalized) && $normalized !== $text) {
6937 - $candidates[] = array(
6938 - 'id' => (int) $row->id,
6939 - 'source_url' => (string) $row->source_url,
6940 - 'content_type' => (string) $row->content_type,
6941 - 'new_content' => $header . $normalized,
6942 - );
6943 - }
6944 - }
6945 - } while (count($rows) === 200);
6946 -
6947 - WP_CLI::log(sprintf('Scanned %d row(s); %d stored in reversed (visual) order.', $scanned, count($candidates)));
6948 - if (empty($candidates)) {
6949 - WP_CLI::success('No reversed RTL rows found — nothing to repair.');
6950 - return;
6951 - }
6952 -
6953 - foreach ($candidates as $c) {
6954 - WP_CLI::log(sprintf('%s row %d %s', $dry_run ? 'Would repair' : 'Will repair', $c['id'], $c['source_url']));
6955 - }
6956 - if ($dry_run) {
6957 - WP_CLI::success(sprintf('Dry run: %d row(s) would be repaired and re-embedded. Run without --dry-run to apply.', count($candidates)));
6958 - return;
6959 - }
6960 -
6961 - // Cost gate: re-embedding spends the customer's API budget.
6962 - WP_CLI::log(sprintf('Re-embedding will call the embedding provider once per row — %d call(s) on this site\'s API key.', count($candidates)));
6963 - if (count($candidates) > 25 && !$yes) {
6964 - WP_CLI::error(sprintf('%d rows need re-embedding (more than 25). Re-run with --yes to confirm the API cost. No rows were changed.', count($candidates)));
6965 - }
6966 -
6967 - $bot_options = $this->get_bot_options($bot_id);
6968 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
6969 - $preflight = MxChat_Utils::embedding_preflight($options);
6970 - if (!$preflight['ok']) {
6971 - WP_CLI::error('Embedding configuration problem: ' . $preflight['reason']);
6972 - }
6973 - $api_key = $preflight['api_key'];
6974 -
6975 - $pinecone_hybrid = $this->mxchat_rtl_repair_pinecone_enabled($bot_id);
6976 - $repaired = 0;
6977 - $failed = 0;
6978 - foreach ($candidates as $c) {
6979 - $vector = MxChat_Utils::regenerate_embedding($c['new_content'], $api_key, $bot_id);
6980 - if (!is_array($vector)) {
6981 - $failed++;
6982 - $reason = is_wp_error($vector) ? $vector->get_error_message() : 'embedding request failed';
6983 - // Text and vector must stay consistent: never write repaired text
6984 - // beside the stale reversed-text vector.
6985 - WP_CLI::warning(sprintf('Row %d NOT repaired — %s. Row left unchanged.', $c['id'], $reason));
6986 - continue;
6987 - }
6988 - $wpdb->update(
6989 - $table,
6990 - array(
6991 - 'article_content' => $c['new_content'],
6992 - 'embedding_vector' => maybe_serialize($vector),
6993 - ),
6994 - array('id' => $c['id']),
6995 - array('%s', '%s'),
6996 - array('%d')
6997 - );
6998 - $repaired++;
6999 - if (class_exists('MxChat_Admin')) {
7000 - MxChat_Admin::mxchat_log_debug('pdf_rtl_repaired', 'Stored KB row restored to logical order and re-embedded', array(
7001 - 'row_id' => $c['id'],
7002 - 'source_url' => $c['source_url'],
7003 - 'bot' => $bot_id,
7004 - ));
7005 - }
7006 - // Hybrid drift: the bot indexes into Pinecone but this row sat in the
7007 - // WP table — push the repaired entry through the normal import path so
7008 - // the md5(source_url)-keyed Pinecone vector is replaced as well.
7009 - if ($pinecone_hybrid) {
7010 - MxChat_Utils::submit_content_to_db(
7011 - $c['new_content'],
7012 - $c['source_url'],
7013 - $api_key,
7014 - null,
7015 - $bot_id,
7016 - $c['content_type'] !== '' ? $c['content_type'] : 'pdf'
7017 - );
7018 - }
7019 - }
7020 -
7021 - WP_CLI::success(sprintf('Repaired + re-embedded %d row(s); %d failed; %d scanned.', $repaired, $failed, $scanned));
7022 -}
7023 -
7024 -/**
7025 - * Split a stored KB row into (metadata header incl. separator, text segment).
7026 - * The PDF importer stores wp_json_encode($metadata) . "\n---\n" . $text —
7027 - * repair must touch only the text and keep the header byte-identical.
7028 - */
7029 -private function mxchat_rtl_repair_split($content) {
7030 - $sep = "\n---\n";
7031 - $pos = strpos($content, $sep);
7032 - if ($pos !== false && $pos > 0 && $content[0] === '{') {
7033 - $maybe_json = substr($content, 0, $pos);
7034 - if (json_decode($maybe_json) !== null) {
7035 - return array(substr($content, 0, $pos + strlen($sep)), substr($content, $pos + strlen($sep)));
7036 - }
7037 - }
7038 - return array('', $content);
7039 -}
7040 -
7041 -/**
7042 - * Mirror of MxChat_Utils::is_pinecone_enabled_for_bot() (private there) for
7043 - * the repair CLI's hybrid-drift check.
7044 - */
7045 -private function mxchat_rtl_repair_pinecone_enabled($bot_id) {
7046 - if ($bot_id !== 'default' && class_exists('MxChat_Multi_Bot_Manager')) {
7047 - $cfg = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
7048 - if (!empty($cfg)) {
7049 - return !empty($cfg['use_pinecone']) && !empty($cfg['api_key']) && !empty($cfg['host']);
7050 - }
7051 - }
7052 - $po = get_option('mxchat_pinecone_addon_options');
7053 - return !empty($po['mxchat_use_pinecone']) && $po['mxchat_use_pinecone'] !== '0'
7054 - && !empty($po['mxchat_pinecone_api_key']) && !empty($po['mxchat_pinecone_host']);
7055 -}
7056 -
7057 -public function mxchat_handle_post_delete($post_id) {
7058 - // Get post data before it's deleted
7059 - $post = get_post($post_id);
7060 -
7061 - // Basic validation
7062 - if (!$post || wp_is_post_revision($post_id)) {
7063 - return;
7064 - }
7065 -
7066 - $post_type = $post->post_type;
7067 -
7068 - // Check if sync is enabled for this post type
7069 - $should_sync = false;
7070 -
7071 - // Check built-in post types first
7072 - if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
7073 - $should_sync = true;
7074 - } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
7075 - $should_sync = true;
7076 - } else {
7077 - // Check custom post types
7078 - $option_name = 'mxchat_auto_sync_' . $post_type;
7079 - if (get_option($option_name) === '1') {
7080 - $should_sync = true;
7081 - }
7082 - }
7083 -
7084 - if (!$should_sync) {
7085 - return;
7086 - }
7087 -
7088 - // Resolve the pre-trash URL. wp_trash_post renames the slug with "__trashed" before firing
7089 - // this hook, so get_permalink() here would return the trashed URL and md5() would miss the
7090 - // real vector IDs stored under the original URL.
7091 - $source_url = $this->mxchat_resolve_pre_trash_url($post_id);
7092 - if (!$source_url) {
7093 - //error_log('MXChat: Failed to resolve source URL for post ' . $post_id);
7094 - return;
7095 - }
7096 -
7097 - // Use chunk-aware deletion (handles both chunked and non-chunked content)
7098 - $delete_result = MxChat_Utils::delete_chunks_for_url($source_url, 'default');
7099 -
7100 - if (is_wp_error($delete_result)) {
7101 - //error_log('MXChat: Chunk-aware deletion failed for URL: ' . $source_url . ' - ' . $delete_result->get_error_message());
7102 - }
7103 -
7104 - delete_transient('mxchat_prev_url_' . $post_id);
7105 - delete_transient('mxchat_prev_status_' . $post_id);
7106 -}
7107 -
7108 -/**
7109 - * Resolve the source URL for a post being trashed/deleted.
7110 - *
7111 - * Why: wp_trash_post appends "__trashed" to the slug before the wp_trash_post action fires, so
7112 - * get_permalink() returns a URL whose md5() won't match the vector IDs stored in Pinecone or
7113 - * the source_url rows in the WP DB. Prefer the URL captured by mxchat_store_pre_update_status
7114 - * (runs on pre_post_update, before the rename); fall back to stripping the __trashed suffix.
7115 - */
7116 -private function mxchat_resolve_pre_trash_url($post_id) {
7117 - $previous_url = get_transient('mxchat_prev_url_' . $post_id);
7118 - if (!empty($previous_url)) {
7119 - return $previous_url;
7120 - }
7121 -
7122 - $current = get_permalink($post_id);
7123 - if (!$current) {
7124 - return '';
7125 - }
7126 - return preg_replace('#__trashed(/?)$#', '$1', $current);
7127 -}
7128 -
7129 -
7130 -
7131 -public function mxchat_handle_product_change($post_id, $post, $update) {
7132 - if ($post->post_type !== 'product') {
7133 - return;
7134 - }
7135 -
7136 - if ($post->post_status === 'publish') {
7137 - add_action('shutdown', function() use ($post_id) {
7138 - $product = wc_get_product($post_id);
7139 - if ($product) {
7140 - $this->mxchat_store_product_embedding($product);
7141 - }
7142 - });
7143 - }
7144 -}
7145 -
7146 -/**
7147 - * Store WooCommerce product embeddings
7148 - */
7149 -private function mxchat_store_product_embedding($product) {
7150 - if (!isset($this->options['enable_woocommerce_integration']) ||
7151 - !in_array($this->options['enable_woocommerce_integration'], ['1', 'on'])) {
7152 - return;
7153 - }
7154 -
7155 - $source_url = get_permalink($product->get_id());
7156 - $product_id = $product->get_id();
7157 -
7158 - // Build product content via the shared WC-object assembler (a3d60c) — this
7159 - // writer owns product rows whenever the integration is on.
7160 - $content = $this->mxchat_prepare_product_content_for_indexing($product);
7161 -
7162 - // Embedding decision — custom-provider-aware (plan cbd5fd); silent-return
7163 - // shape preserved.
7164 - $preflight = MxChat_Utils::embedding_preflight(get_option('mxchat_options'));
7165 - if (!$preflight['ok']) {
7166 - //error_log('MxChat Auto-sync: embedding pre-flight failed: ' . $preflight['reason']);
7167 - return;
7168 - }
7169 - $api_key = $preflight['api_key'];
7170 -
7171 - // Use the centralized utility function for storage
7172 - $result = MxChat_Utils::submit_content_to_db(
7173 - $content,
7174 - $source_url,
7175 - $api_key,
7176 - md5($source_url) // Vector ID for Pinecone
7177 - );
7178 -
7179 - // After successful storage, apply role restriction based on tags
7180 - if (!is_wp_error($result)) {
7181 - $this->apply_role_restriction_to_post($product_id, $source_url);
7182 - }
7183 -
7184 - if (is_wp_error($result)) {
7185 - //error_log('MxChat WooCommerce sync failed for product ' . $product_id . ': ' . $result->get_error_message());
7186 - }
7187 -}
7188 -
7189 -public function mxchat_handle_product_delete($post_id) {
7190 - if (get_post_type($post_id) !== 'product') {
7191 - return;
7192 - }
7193 -
7194 - $source_url = $this->mxchat_resolve_pre_trash_url($post_id);
7195 - if (!$source_url) {
7196 - return;
7197 - }
7198 -
7199 - // Chunk-aware deletion (routes to Pinecone or WP DB and removes base + all chunks)
7200 - MxChat_Utils::delete_chunks_for_url($source_url, 'default');
7201 -
7202 - delete_transient('mxchat_prev_url_' . $post_id);
7203 - delete_transient('mxchat_prev_status_' . $post_id);
7204 -}
7205 -
7206 -/**
7207 - * Handle individual Pinecone content deletion
7208 - */
7209 -public function mxchat_handle_pinecone_prompt_delete() {
7210 - // Check permissions
7211 - if (!current_user_can('manage_options')) {
7212 - wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
7213 - }
7214 -
7215 - // Verify nonce
7216 - if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'mxchat_delete_pinecone_prompt_nonce')) {
7217 - wp_die(esc_html__('Security check failed.', 'mxchat'));
7218 - }
7219 -
7220 - $vector_id = isset($_GET['vector_id']) ? sanitize_text_field($_GET['vector_id']) : '';
7221 -
7222 - if (empty($vector_id)) {
7223 - set_transient('mxchat_admin_notice_error',
7224 - esc_html__('Invalid vector ID.', 'mxchat'),
7225 - 30
7226 - );
7227 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
7228 - exit;
7229 - }
7230 -
7231 - // Get Pinecone settings
7232 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
7233 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7234 -
7235 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
7236 - set_transient('mxchat_admin_notice_error',
7237 - esc_html__('Pinecone is not properly configured.', 'mxchat'),
7238 - 30
7239 - );
7240 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
7241 - exit;
7242 - }
7243 -
7244 - // Delete from Pinecone
7245 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
7246 - $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
7247 - $vector_id,
7248 - $pinecone_options['mxchat_pinecone_api_key'],
7249 - $pinecone_options['mxchat_pinecone_host'],
7250 - $pinecone_options['mxchat_pinecone_namespace'] ?? ''
7251 - );
7252 -
7253 - if ($result['success']) {
7254 - // Mirror the removal to the OpenAI Vector Store mapping (plan 15b5c6);
7255 - // a chunk vector id reduces to its base entry there.
7256 - if (class_exists('MxChat_Vectorstore_Manager')) {
7257 - MxChat_Vectorstore_Manager::sync_delete_by_key($vector_id, 'default');
7258 - }
7259 - // No cache clearing needed since we removed caching
7260 - set_transient('mxchat_admin_notice_success',
7261 - esc_html__('Entry deleted successfully from Pinecone.', 'mxchat'),
7262 - 30
7263 - );
7264 - } else {
7265 - set_transient('mxchat_admin_notice_error',
7266 - esc_html__('Failed to delete entry: ', 'mxchat') . $result['message'],
7267 - 30
7268 - );
7269 - }
7270 -
7271 - wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
7272 - exit;
7273 -}
7274 -/**
7275 - * Handle individual Pinecone content deletion via AJAX
7276 - */
7277 -public function ajax_mxchat_delete_pinecone_prompt() {
7278 - // Verify nonce and permissions
7279 - if (!check_ajax_referer('mxchat_delete_pinecone_prompt_nonce', 'nonce', false)) {
7280 - wp_send_json_error('Invalid nonce');
7281 - exit;
7282 - }
7283 -
7284 - if (!current_user_can('manage_options')) {
7285 - wp_send_json_error('Unauthorized access');
7286 - exit;
7287 - }
7288 -
7289 - $vector_id = isset($_POST['vector_id']) ? sanitize_text_field($_POST['vector_id']) : '';
7290 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
7291 -
7292 - if (empty($vector_id)) {
7293 - wp_send_json_error('Missing vector ID');
7294 - exit;
7295 - }
7296 -
7297 - // Get bot-specific Pinecone settings
7298 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
7299 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
7300 -
7301 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7302 -
7303 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
7304 - wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
7305 - exit;
7306 - }
7307 -
7308 - // Delete from the correct Pinecone index and namespace
7309 - $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
7310 - $vector_id,
7311 - $pinecone_options['mxchat_pinecone_api_key'],
7312 - $pinecone_options['mxchat_pinecone_host'],
7313 - $pinecone_options['mxchat_pinecone_namespace'] ?? ''
7314 - );
7315 -
7316 - if ($result['success']) {
7317 - // Mirror the removal to the OpenAI Vector Store mapping (plan 15b5c6)
7318 - if (class_exists('MxChat_Vectorstore_Manager')) {
7319 - MxChat_Vectorstore_Manager::sync_delete_by_key($vector_id, $bot_id);
7320 - }
7321 - // No cache clearing needed since we removed caching
7322 - wp_send_json_success(array(
7323 - 'message' => 'Entry deleted successfully from Pinecone',
7324 - 'vector_id' => $vector_id,
7325 - 'bot_id' => $bot_id
7326 - ));
7327 - } else {
7328 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Failed to delete from Pinecone: ' . $result['message'], array('vector_id' => $vector_id, 'bot_id' => $bot_id));
7329 - wp_send_json_error('Failed to delete from Pinecone: ' . $result['message']);
7330 - }
7331 -
7332 - exit;
7333 -}
7334 -
7335 -/**
7336 - * Handle deletion of all chunks for a given source URL via AJAX
7337 - * Follows the same pattern as ajax_mxchat_delete_pinecone_prompt
7338 - */
7339 -public function ajax_mxchat_delete_chunks_by_url() {
7340 - // Verify nonce and permissions
7341 - if (!check_ajax_referer('mxchat_delete_chunks_nonce', 'nonce', false)) {
7342 - wp_send_json_error('Invalid nonce');
7343 - exit;
7344 - }
7345 -
7346 - if (!current_user_can('manage_options')) {
7347 - wp_send_json_error('Unauthorized access');
7348 - exit;
7349 - }
7350 -
7351 - $source_url = isset($_POST['source_url']) ? esc_url_raw($_POST['source_url']) : '';
7352 - $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
7353 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
7354 -
7355 - if (empty($source_url)) {
7356 - wp_send_json_error('Missing source URL');
7357 - exit;
7358 - }
7359 -
7360 - // Generate the base vector ID from the source URL (same as how chunks are created)
7361 - $base_vector_id = md5($source_url);
7362 -
7363 - if ($data_source === 'pinecone') {
7364 - // Get bot-specific Pinecone settings (same as working delete function)
7365 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
7366 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
7367 -
7368 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7369 -
7370 - if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
7371 - wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
7372 - exit;
7373 - }
7374 -
7375 - $api_key = $pinecone_options['mxchat_pinecone_api_key'];
7376 - $host = $pinecone_options['mxchat_pinecone_host'];
7377 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
7378 -
7379 - // Collect all vector IDs to delete
7380 - $vectors_to_delete = array();
7381 -
7382 - // Add the original single-vector ID (for non-chunked content)
7383 - $vectors_to_delete[] = $base_vector_id;
7384 -
7385 - // Use Pinecone list API to find all chunk vectors with this prefix
7386 - // NOTE: Pinecone List API is a GET request with query parameters, not POST
7387 - $prefix = $base_vector_id . '_chunk_';
7388 -
7389 - $query_params = array(
7390 - 'prefix' => $prefix,
7391 - 'limit' => 100
7392 - );
7393 -
7394 - if (!empty($namespace)) {
7395 - $query_params['namespace'] = $namespace;
7396 - }
7397 -
7398 - $list_url = "https://{$host}/vectors/list?" . http_build_query($query_params);
7399 -
7400 - $list_response = wp_remote_get($list_url, array(
7401 - 'headers' => array(
7402 - 'Api-Key' => $api_key,
7403 - 'accept' => 'application/json'
7404 - ),
7405 - 'timeout' => 30
7406 - ));
7407 -
7408 - if (!is_wp_error($list_response)) {
7409 - $list_body_response = wp_remote_retrieve_body($list_response);
7410 - $list_data = json_decode($list_body_response, true);
7411 - if (!empty($list_data['vectors'])) {
7412 - foreach ($list_data['vectors'] as $vector) {
7413 - if (isset($vector['id'])) {
7414 - $vectors_to_delete[] = $vector['id'];
7415 - }
7416 - }
7417 - }
7418 - }
7419 -
7420 - if (empty($vectors_to_delete)) {
7421 - // Entry already gone from Pinecone — still clear any mirrored
7422 - // Vector Store file so it can't outlive the entry (plan 15b5c6).
7423 - if (class_exists('MxChat_Vectorstore_Manager')) {
7424 - MxChat_Vectorstore_Manager::sync_delete_entry($source_url, $bot_id);
7425 - }
7426 - wp_send_json_success(array(
7427 - 'message' => 'No vectors found to delete',
7428 - 'source_url' => $source_url
7429 - ));
7430 - exit;
7431 - }
7432 -
7433 - // Delete all vectors using the same endpoint as the working function
7434 - $delete_url = "https://{$host}/vectors/delete";
7435 -
7436 - $delete_body = array(
7437 - 'ids' => $vectors_to_delete
7438 - );
7439 -
7440 - if (!empty($namespace)) {
7441 - $delete_body['namespace'] = $namespace;
7442 - }
7443 -
7444 - $delete_response = wp_remote_post($delete_url, array(
7445 - 'headers' => array(
7446 - 'Api-Key' => $api_key,
7447 - 'accept' => 'application/json',
7448 - 'content-type' => 'application/json'
7449 - ),
7450 - 'body' => wp_json_encode($delete_body),
7451 - 'timeout' => 30
7452 - ));
7453 -
7454 - if (is_wp_error($delete_response)) {
7455 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Failed to delete chunks from Pinecone: ' . $delete_response->get_error_message(), array('source_url' => $source_url));
7456 - wp_send_json_error('Failed to delete from Pinecone: ' . $delete_response->get_error_message());
7457 - exit;
7458 - }
7459 -
7460 - $response_code = wp_remote_retrieve_response_code($delete_response);
7461 -
7462 - if ($response_code !== 200) {
7463 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Pinecone API error (HTTP ' . $response_code . ')', array('source_url' => $source_url));
7464 - wp_send_json_error('Pinecone API error (HTTP ' . $response_code . ')');
7465 - exit;
7466 - }
7467 -
7468 - // Mirror the removal to the OpenAI Vector Store (plan 15b5c6)
7469 - if (class_exists('MxChat_Vectorstore_Manager')) {
7470 - MxChat_Vectorstore_Manager::sync_delete_entry($source_url, $bot_id);
7471 - }
7472 -
7473 - wp_send_json_success(array(
7474 - 'message' => 'All chunks deleted successfully from Pinecone',
7475 - 'source_url' => $source_url,
7476 - 'deleted_count' => count($vectors_to_delete)
7477 - ));
7478 -
7479 - } else {
7480 - // WordPress database deletion
7481 - global $wpdb;
7482 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7483 -
7484 - $result = $wpdb->delete(
7485 - $table_name,
7486 - array('source_url' => $source_url),
7487 - array('%s')
7488 - );
7489 -
7490 - if ($result === false) {
7491 - MxChat_Admin::mxchat_log_debug('database_error', 'Failed to delete from database: ' . $wpdb->last_error, array('source_url' => $source_url));
7492 - wp_send_json_error('Failed to delete from database: ' . $wpdb->last_error);
7493 - exit;
7494 - }
7495 -
7496 - // Mirror the removal to the OpenAI Vector Store (plan 15b5c6)
7497 - if (class_exists('MxChat_Vectorstore_Manager')) {
7498 - MxChat_Vectorstore_Manager::sync_delete_entry($source_url, $bot_id);
7499 - }
7500 -
7501 - wp_send_json_success(array(
7502 - 'message' => 'All chunks deleted successfully from database',
7503 - 'source_url' => $source_url,
7504 - 'deleted_count' => $result
7505 - ));
7506 - }
7507 -
7508 - exit;
7509 -}
7510 -
7511 -/**
7512 - * Handle individual WordPress database content deletion via AJAX
7513 - * Mirrors the Pinecone delete handler but for WordPress database entries
7514 - */
7515 -public function ajax_mxchat_delete_wordpress_prompt() {
7516 - // Verify nonce and permissions
7517 - if (!check_ajax_referer('mxchat_delete_wordpress_prompt_nonce', 'nonce', false)) {
7518 - wp_send_json_error('Invalid nonce');
7519 - exit;
7520 - }
7521 -
7522 - if (!current_user_can('manage_options')) {
7523 - wp_send_json_error('Unauthorized access');
7524 - exit;
7525 - }
7526 -
7527 - $entry_id = isset($_POST['entry_id']) ? intval($_POST['entry_id']) : 0;
7528 -
7529 - if (empty($entry_id)) {
7530 - wp_send_json_error('Missing entry ID');
7531 - exit;
7532 - }
7533 -
7534 - global $wpdb;
7535 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7536 -
7537 - // Capture the identity BEFORE the row disappears — needed to mirror the
7538 - // change into the Vector Store (plan 15b5c6).
7539 - $source_url = $wpdb->get_var($wpdb->prepare(
7540 - "SELECT source_url FROM {$table_name} WHERE id = %d",
7541 - $entry_id
7542 - ));
7543 -
7544 - // Clear cache for this entry
7545 - wp_cache_delete('prompt_' . $entry_id, 'mxchat_prompts');
7546 -
7547 - // Delete from database
7548 - $result = $wpdb->delete(
7549 - $table_name,
7550 - array('id' => $entry_id),
7551 - array('%d')
7552 - );
7553 -
7554 - if ($result !== false) {
7555 - // Mirror to the Vector Store: if sibling rows remain (this was one
7556 - // chunk of a larger entry) the entry's file is REFRESHED from what's
7557 - // left; if none remain, the file is removed.
7558 - if (!empty($source_url) && class_exists('MxChat_Vectorstore_Manager')) {
7559 - $remaining = (int) $wpdb->get_var($wpdb->prepare(
7560 - "SELECT COUNT(*) FROM {$table_name} WHERE source_url = %s",
7561 - $source_url
7562 - ));
7563 - if ($remaining > 0) {
7564 - MxChat_Vectorstore_Manager::sync_upsert_entry($source_url, '', 'default');
7565 - } else {
7566 - MxChat_Vectorstore_Manager::sync_delete_entry($source_url, 'default');
7567 - }
7568 - }
7569 -
7570 - wp_send_json_success(array(
7571 - 'message' => 'Entry deleted successfully',
7572 - 'entry_id' => $entry_id
7573 - ));
7574 - } else {
7575 - wp_send_json_error('Failed to delete entry from database');
7576 - }
7577 -
7578 - exit;
7579 -}
7580 -
7581 -/**
7582 - * Handle bulk deletion of knowledge entries via AJAX
7583 - * Supports both Pinecone and WordPress database entries
7584 - */
7585 -public function ajax_mxchat_bulk_delete_knowledge() {
7586 - // Verify nonce and permissions
7587 - if (!check_ajax_referer('mxchat_bulk_delete_knowledge_nonce', 'nonce', false)) {
7588 - wp_send_json_error('Invalid nonce');
7589 - exit;
7590 - }
7591 -
7592 - if (!current_user_can('manage_options')) {
7593 - wp_send_json_error('Unauthorized access');
7594 - exit;
7595 - }
7596 -
7597 - $entries = isset($_POST['entries']) ? $_POST['entries'] : array();
7598 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
7599 -
7600 - if (empty($entries) || !is_array($entries)) {
7601 - wp_send_json_error('No entries provided');
7602 - exit;
7603 - }
7604 -
7605 - // Extend execution time — bulk Pinecone operations can take a while
7606 - if (function_exists('set_time_limit')) {
7607 - set_time_limit(120);
7608 - }
7609 -
7610 - $success_ids = array();
7611 - $failed_ids = array();
7612 - $errors = array();
7613 -
7614 - global $wpdb;
7615 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7616 -
7617 - // Get Pinecone manager for Pinecone deletions
7618 - $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
7619 - $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
7620 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
7621 -
7622 - $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
7623 - $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
7624 - $namespace = $pinecone_options['mxchat_pinecone_namespace'] ?? '';
7625 -
7626 - // =============================================
7627 - // PHASE 1: Collect all Pinecone vector IDs
7628 - // and separate WordPress entries
7629 - // =============================================
7630 - $pinecone_entry_ids = array(); // entry IDs that are pinecone-sourced
7631 - $wordpress_entries = array(); // entries for WordPress DB deletion
7632 - $all_vector_ids = array(); // all pinecone vector IDs to delete in one batch
7633 - $vs_mirror_urls = array(); // Vector Store mirror: URLs to delete (plan 15b5c6)
7634 - $vs_mirror_keys = array(); // Vector Store mirror: bare vector ids to delete
7635 -
7636 - foreach ($entries as $entry) {
7637 - $entry_id = sanitize_text_field($entry['id'] ?? '');
7638 - $source = sanitize_text_field($entry['source'] ?? 'wordpress');
7639 - $source_url = isset($entry['sourceUrl']) ? esc_url_raw($entry['sourceUrl']) : '';
7640 - $is_group = isset($entry['isGroup']) && ($entry['isGroup'] === true || $entry['isGroup'] === 'true');
7641 -
7642 - if (empty($entry_id)) {
7643 - continue;
7644 - }
7645 -
7646 - if ($source === 'pinecone') {
7647 - if (!$use_pinecone || empty($api_key)) {
7648 - $failed_ids[] = $entry_id;
7649 - $errors[] = "Pinecone not configured for entry: $entry_id";
7650 - continue;
7651 - }
7652 -
7653 - $pinecone_entry_ids[] = $entry_id;
7654 -
7655 - if ($is_group && !empty($source_url)) {
7656 - // Grouped/chunked entry: collect base ID + chunk IDs via List API
7657 - $base_vector_id = md5($source_url);
7658 - $all_vector_ids[] = $base_vector_id;
7659 -
7660 - $list_url = 'https://' . $host . '/vectors/list?prefix=' . urlencode($base_vector_id . '_chunk_') . '&limit=100';
7661 - if (!empty($namespace)) {
7662 - $list_url .= '&namespace=' . rawurlencode($namespace);
7663 - }
7664 - $list_response = wp_remote_get($list_url, array(
7665 - 'headers' => array(
7666 - 'Api-Key' => $api_key,
7667 - 'accept' => 'application/json'
7668 - ),
7669 - 'timeout' => 30
7670 - ));
7671 -
7672 - if (!is_wp_error($list_response)) {
7673 - $list_body = json_decode(wp_remote_retrieve_body($list_response), true);
7674 - if (!empty($list_body['vectors']) && is_array($list_body['vectors'])) {
7675 - foreach ($list_body['vectors'] as $vector) {
7676 - if (isset($vector['id'])) {
7677 - $all_vector_ids[] = $vector['id'];
7678 - }
7679 - }
7680 - }
7681 - }
7682 - } else {
7683 - // Single entry: the entry_id IS the vector ID
7684 - $all_vector_ids[] = $entry_id;
7685 - }
7686 -
7687 - if (!empty($source_url)) {
7688 - $vs_mirror_urls[] = $source_url;
7689 - } else {
7690 - $vs_mirror_keys[] = $entry_id;
7691 - }
7692 - } else {
7693 - $wordpress_entries[] = $entry;
7694 - }
7695 - }
7696 -
7697 - // =============================================
7698 - // PHASE 2: Single batch delete to Pinecone
7699 - // =============================================
7700 - if (!empty($all_vector_ids)) {
7701 - $all_vector_ids = array_values(array_unique($all_vector_ids));
7702 - $pinecone_success = true;
7703 - $batches = array_chunk($all_vector_ids, 100);
7704 -
7705 - foreach ($batches as $batch) {
7706 - $delete_body = array('ids' => $batch);
7707 - if (!empty($namespace)) {
7708 - $delete_body['namespace'] = $namespace;
7709 - }
7710 - $delete_response = wp_remote_post("https://{$host}/vectors/delete", array(
7711 - 'headers' => array(
7712 - 'Api-Key' => $api_key,
7713 - 'accept' => 'application/json',
7714 - 'content-type' => 'application/json'
7715 - ),
7716 - 'body' => wp_json_encode($delete_body),
7717 - 'timeout' => 60
7718 - ));
7719 -
7720 - if (is_wp_error($delete_response)) {
7721 - $pinecone_success = false;
7722 - $errors[] = 'Pinecone batch deletion failed: ' . $delete_response->get_error_message();
7723 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Bulk delete batch failed: ' . $delete_response->get_error_message());
7724 - } else {
7725 - $response_code = wp_remote_retrieve_response_code($delete_response);
7726 - if ($response_code !== 200) {
7727 - $pinecone_success = false;
7728 - $response_body = wp_remote_retrieve_body($delete_response);
7729 - $errors[] = "Pinecone API error (HTTP $response_code)";
7730 - MxChat_Admin::mxchat_log_debug('pinecone_error', 'Bulk delete batch failed (HTTP ' . $response_code . ')', array('response' => substr($response_body, 0, 200)));
7731 - }
7732 - }
7733 - }
7734 -
7735 - // Mark all pinecone entries based on batch result
7736 - foreach ($pinecone_entry_ids as $eid) {
7737 - if ($pinecone_success) {
7738 - $success_ids[] = $eid;
7739 - } else {
7740 - $failed_ids[] = $eid;
7741 - }
7742 - }
7743 -
7744 - // Mirror the removals to the OpenAI Vector Store (plan 15b5c6)
7745 - if ($pinecone_success && class_exists('MxChat_Vectorstore_Manager')) {
7746 - foreach (array_unique($vs_mirror_urls) as $vs_url) {
7747 - MxChat_Vectorstore_Manager::sync_delete_entry($vs_url, $bot_id);
7748 - }
7749 - foreach (array_unique($vs_mirror_keys) as $vs_key) {
7750 - MxChat_Vectorstore_Manager::sync_delete_by_key($vs_key, $bot_id);
7751 - }
7752 - }
7753 - }
7754 -
7755 - // =============================================
7756 - // PHASE 3: WordPress database deletions
7757 - // =============================================
7758 - foreach ($wordpress_entries as $entry) {
7759 - $entry_id = sanitize_text_field($entry['id'] ?? '');
7760 - $source_url = isset($entry['sourceUrl']) ? esc_url_raw($entry['sourceUrl']) : '';
7761 - $is_group = isset($entry['isGroup']) && ($entry['isGroup'] === true || $entry['isGroup'] === 'true');
7762 -
7763 - if (empty($entry_id)) {
7764 - continue;
7765 - }
7766 -
7767 - try {
7768 - if ($is_group && !empty($source_url)) {
7769 - $result = $wpdb->delete(
7770 - $table_name,
7771 - array('source_url' => $source_url),
7772 - array('%s')
7773 - );
7774 - $row_url = $source_url;
7775 - } else {
7776 - // Identity captured pre-delete for the Vector Store mirror
7777 - $row_url = $wpdb->get_var($wpdb->prepare(
7778 - "SELECT source_url FROM {$table_name} WHERE id = %d",
7779 - intval($entry_id)
7780 - ));
7781 - wp_cache_delete('prompt_' . $entry_id, 'mxchat_prompts');
7782 - $result = $wpdb->delete(
7783 - $table_name,
7784 - array('id' => intval($entry_id)),
7785 - array('%d')
7786 - );
7787 - }
7788 -
7789 - if ($result !== false) {
7790 - $success_ids[] = $entry_id;
7791 - // Mirror to the Vector Store: refresh the entry's file when
7792 - // sibling chunk rows survive, remove it when none do.
7793 - if (!empty($row_url) && class_exists('MxChat_Vectorstore_Manager')) {
7794 - $remaining = (int) $wpdb->get_var($wpdb->prepare(
7795 - "SELECT COUNT(*) FROM {$table_name} WHERE source_url = %s",
7796 - $row_url
7797 - ));
7798 - if ($remaining > 0) {
7799 - MxChat_Vectorstore_Manager::sync_upsert_entry($row_url, '', $bot_id);
7800 - } else {
7801 - MxChat_Vectorstore_Manager::sync_delete_entry($row_url, $bot_id);
7802 - }
7803 - }
7804 - } else {
7805 - $failed_ids[] = $entry_id;
7806 - $errors[] = "Database error for entry: $entry_id";
7807 - }
7808 - } catch (Exception $e) {
7809 - $failed_ids[] = $entry_id;
7810 - $errors[] = $e->getMessage();
7811 - }
7812 - }
7813 -
7814 - wp_send_json_success(array(
7815 - 'success_ids' => $success_ids,
7816 - 'failed_ids' => $failed_ids,
7817 - 'errors' => $errors,
7818 - 'total_processed' => count($success_ids) + count($failed_ids)
7819 - ));
7820 -
7821 - exit;
7822 -}
7823 -
7824 -/**
7825 - * Get hierarchical roles for dropdown
7826 - */
7827 -public function mxchat_get_role_options() {
7828 - return array(
7829 - 'public' => __('Public (Everyone)', 'mxchat'),
7830 - 'logged_in' => __('Logged In Users', 'mxchat'),
7831 - 'subscriber' => __('Subscribers & Above', 'mxchat'),
7832 - 'contributor' => __('Contributors & Above', 'mxchat'),
7833 - 'author' => __('Authors & Above', 'mxchat'),
7834 - 'editor' => __('Editors & Above', 'mxchat'),
7835 - 'administrator' => __('Administrators Only', 'mxchat')
7836 - );
7837 -}
7838 -
7839 -/**
7840 - * Check if user has access to content based on role restriction
7841 - */
7842 -public function mxchat_user_has_content_access($role_restriction) {
7843 - // Public content is always accessible
7844 - if ($role_restriction === 'public' || empty($role_restriction)) {
7845 - return true;
7846 - }
7847 -
7848 - // Check if user is logged in for logged_in restriction
7849 - if ($role_restriction === 'logged_in') {
7850 - return is_user_logged_in();
7851 - }
7852 -
7853 - // If not logged in, no access to role-restricted content
7854 - if (!is_user_logged_in()) {
7855 - return false;
7856 - }
7857 -
7858 - $user = wp_get_current_user();
7859 - $user_roles = $user->roles;
7860 -
7861 - if (empty($user_roles)) {
7862 - return false;
7863 - }
7864 -
7865 - // Define role hierarchy (higher number = higher access)
7866 - $hierarchy = array(
7867 - 'subscriber' => 1,
7868 - 'contributor' => 2,
7869 - 'author' => 3,
7870 - 'editor' => 4,
7871 - 'administrator' => 5
7872 - );
7873 -
7874 - // Get required level
7875 - $required_level = isset($hierarchy[$role_restriction]) ? $hierarchy[$role_restriction] : 0;
7876 -
7877 - // Check if user has required level or higher
7878 - foreach ($user_roles as $user_role) {
7879 - $user_level = isset($hierarchy[$user_role]) ? $hierarchy[$user_role] : 0;
7880 - if ($user_level >= $required_level) {
7881 - return true;
7882 - }
7883 - }
7884 -
7885 - return false;
7886 -}
7887 -
7888 -/**
7889 - * Handle role restriction updates via AJAX
7890 - * Removed cache clearing call since we removed caching
7891 - */
7892 -public function ajax_mxchat_update_role_restriction() {
7893 - // Verify nonce and permissions
7894 - if (!check_ajax_referer('mxchat_update_role_nonce', 'nonce', false)) {
7895 - wp_send_json_error('Invalid nonce');
7896 - exit;
7897 - }
7898 -
7899 - if (!current_user_can('manage_options')) {
7900 - wp_send_json_error('Unauthorized access');
7901 - exit;
7902 - }
7903 -
7904 - $entry_id = isset($_POST['entry_id']) ? sanitize_text_field($_POST['entry_id']) : '';
7905 - $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
7906 - $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
7907 -
7908 - if (empty($entry_id)) {
7909 - wp_send_json_error('Invalid entry ID');
7910 - exit;
7911 - }
7912 -
7913 - // Get knowledge manager instance to validate role restriction
7914 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
7915 - $valid_roles = array_keys($knowledge_manager->mxchat_get_role_options());
7916 - if (!in_array($role_restriction, $valid_roles)) {
7917 - wp_send_json_error('Invalid role restriction');
7918 - exit;
7919 - }
7920 -
7921 - global $wpdb;
7922 -
7923 - if ($data_source === 'pinecone') {
7924 - // Handle Pinecone role restriction (stored separately in WordPress table)
7925 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
7926 -
7927 - // Use REPLACE to insert or update the role restriction
7928 - $result = $wpdb->replace(
7929 - $roles_table,
7930 - array(
7931 - 'vector_id' => $entry_id,
7932 - 'role_restriction' => $role_restriction,
7933 - 'updated_at' => current_time('mysql')
7934 - ),
7935 - array('%s', '%s', '%s')
7936 - );
7937 -
7938 - // No cache clearing needed since we removed caching
7939 -
7940 - } else {
7941 - // Handle WordPress database role restriction (existing functionality)
7942 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
7943 -
7944 - $result = $wpdb->update(
7945 - $table_name,
7946 - array('role_restriction' => $role_restriction),
7947 - array('id' => absint($entry_id)),
7948 - array('%s'),
7949 - array('%d')
7950 - );
7951 - }
7952 -
7953 - if ($result === false) {
7954 - wp_send_json_error('Database update failed: ' . $wpdb->last_error);
7955 - exit;
7956 - }
7957 -
7958 - // Keep the OpenAI Vector Store mirror consistent with the new restriction
7959 - // (plan 15b5c6): non-public pulls the mirrored file, public re-mirrors.
7960 - if (class_exists('MxChat_Vectorstore_Manager')) {
7961 - if ($data_source === 'pinecone') {
7962 - if ($role_restriction !== 'public') {
7963 - MxChat_Vectorstore_Manager::sync_delete_by_key($entry_id, 'default');
7964 - }
7965 - // Public again: Pinecone-mode content isn't held locally, so the
7966 - // entry re-mirrors on its next save/import rather than here.
7967 - } else {
7968 - $row_url = $wpdb->get_var($wpdb->prepare(
7969 - "SELECT source_url FROM {$wpdb->prefix}mxchat_system_prompt_content WHERE id = %d",
7970 - absint($entry_id)
7971 - ));
7972 - if (!empty($row_url)) {
7973 - MxChat_Vectorstore_Manager::handle_role_change($row_url, 'default', $role_restriction);
7974 - }
7975 - }
7976 - }
7977 -
7978 - wp_send_json_success(array(
7979 - 'message' => 'Role restriction updated successfully',
7980 - 'role_restriction' => $role_restriction,
7981 - 'data_source' => $data_source,
7982 - 'entry_id' => $entry_id
7983 - ));
7984 - exit;
7985 -}
7986 -
7987 -// ========================================
7988 -// ROLE-BASED CONTENT RESTRICTIONS - NEW FUNCTIONS
7989 -// Add these to your MxChat_Knowledge_Manager class
7990 -// ========================================
7991 -
7992 -/**
7993 - * Initialize role-based content hooks
7994 - * Add this call to your __construct() or mxchat_init_hooks() method
7995 - */
7996 -private function mxchat_init_role_hooks() {
7997 - // AJAX handlers for tag-role mappings
7998 - add_action('wp_ajax_mxchat_add_tag_role_mapping', array($this, 'ajax_add_tag_role_mapping'));
7999 - add_action('wp_ajax_mxchat_delete_tag_role_mapping', array($this, 'ajax_delete_tag_role_mapping'));
8000 - add_action('wp_ajax_mxchat_get_tag_role_mappings', array($this, 'ajax_get_tag_role_mappings'));
8001 - add_action('wp_ajax_mxchat_bulk_update_tag_roles', array($this, 'ajax_bulk_update_tag_roles'));
8002 -
8003 - // Hook to automatically update role restrictions when tags are added/removed
8004 - add_action('set_object_terms', array($this, 'handle_tag_change'), 10, 6);
8005 -
8006 - // Hook to apply role restrictions on auto-sync
8007 - add_action('mxchat_content_stored', array($this, 'apply_role_restriction_after_storage'), 10, 2);
8008 -}
8009 -
8010 -/**
8011 - * Add tag-role mapping via AJAX
8012 - */
8013 -public function ajax_add_tag_role_mapping() {
8014 - // Verify nonce and permissions
8015 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
8016 -
8017 - if (!current_user_can('manage_options')) {
8018 - wp_send_json_error('Unauthorized access');
8019 - exit;
8020 - }
8021 -
8022 - $tag_input = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
8023 - $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
8024 -
8025 - if (empty($tag_input)) {
8026 - wp_send_json_error('Please enter a tag name or slug');
8027 - exit;
8028 - }
8029 -
8030 - // Validate role restriction
8031 - $valid_roles = array_keys($this->mxchat_get_role_options());
8032 - if (!in_array($role_restriction, $valid_roles)) {
8033 - wp_send_json_error('Invalid role restriction');
8034 - exit;
8035 - }
8036 -
8037 - // Resolve the tag by slug first, then fall back to its display name, so users can
8038 - // enter either "premium-content" or "Premium Content". (plan b8bcf5 — the field is
8039 - // labeled by name but previously validated by slug only, producing the confusing
8040 - // "Tag does not exist in WordPress" error when a real tag's name was typed.)
8041 - $term = get_term_by('slug', $tag_input, 'post_tag');
8042 - if (!$term) {
8043 - $term = get_term_by('name', $tag_input, 'post_tag');
8044 - }
8045 - if (!$term) {
8046 - 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.');
8047 - exit;
8048 - }
8049 -
8050 - // Always key the mapping by the RESOLVED slug — apply_role_restriction_to_post()
8051 - // compares against each post's tag slugs, so the stored key must be a slug,
8052 - // never the raw (possibly display-name) input.
8053 - $tag_slug = $term->slug;
8054 -
8055 - // Get existing mappings
8056 - $mappings = get_option('mxchat_tag_role_mappings', array());
8057 -
8058 - // Check if mapping already exists
8059 - if (isset($mappings[$tag_slug])) {
8060 - wp_send_json_error('Mapping for this tag already exists');
8061 - exit;
8062 - }
8063 -
8064 - // Add new mapping
8065 - $mappings[$tag_slug] = $role_restriction;
8066 - update_option('mxchat_tag_role_mappings', $mappings);
8067 -
8068 - wp_send_json_success(array(
8069 - 'message' => 'Tag-role mapping added successfully',
8070 - 'tag_slug' => $tag_slug,
8071 - 'role_restriction' => $role_restriction
8072 - ));
8073 - exit;
8074 -}
8075 -
8076 -/**
8077 - * Delete tag-role mapping via AJAX
8078 - */
8079 -public function ajax_delete_tag_role_mapping() {
8080 - // Verify nonce and permissions
8081 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
8082 -
8083 - if (!current_user_can('manage_options')) {
8084 - wp_send_json_error('Unauthorized access');
8085 - exit;
8086 - }
8087 -
8088 - $tag_slug = isset($_POST['tag_slug']) ? sanitize_text_field($_POST['tag_slug']) : '';
8089 -
8090 - if (empty($tag_slug)) {
8091 - wp_send_json_error('Tag slug is required');
8092 - exit;
8093 - }
8094 -
8095 - // Get existing mappings
8096 - $mappings = get_option('mxchat_tag_role_mappings', array());
8097 -
8098 - // Check if mapping exists
8099 - if (!isset($mappings[$tag_slug])) {
8100 - wp_send_json_error('Mapping does not exist');
8101 - exit;
8102 - }
8103 -
8104 - // Remove mapping
8105 - unset($mappings[$tag_slug]);
8106 - update_option('mxchat_tag_role_mappings', $mappings);
8107 -
8108 - wp_send_json_success(array(
8109 - 'message' => 'Tag-role mapping deleted successfully',
8110 - 'tag_slug' => $tag_slug
8111 - ));
8112 - exit;
8113 -}
8114 -
8115 -/**
8116 - * Get all tag-role mappings via AJAX
8117 - */
8118 -public function ajax_get_tag_role_mappings() {
8119 - // Verify nonce and permissions
8120 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
8121 -
8122 - if (!current_user_can('manage_options')) {
8123 - wp_send_json_error('Unauthorized access');
8124 - exit;
8125 - }
8126 -
8127 - // Get mappings
8128 - $mappings = get_option('mxchat_tag_role_mappings', array());
8129 - $role_options = $this->mxchat_get_role_options();
8130 -
8131 - $formatted_mappings = array();
8132 -
8133 - foreach ($mappings as $tag_slug => $role_restriction) {
8134 - // Get tag object
8135 - $term = get_term_by('slug', $tag_slug, 'post_tag');
8136 -
8137 - // Count posts with this tag
8138 - $post_count = 0;
8139 - if ($term) {
8140 - $post_count = $term->count;
8141 - }
8142 -
8143 - $formatted_mappings[] = array(
8144 - 'tag_slug' => $tag_slug,
8145 - 'role_restriction' => $role_restriction,
8146 - 'role_label' => $role_options[$role_restriction] ?? $role_restriction,
8147 - 'post_count' => $post_count
8148 - );
8149 - }
8150 -
8151 - wp_send_json_success(array(
8152 - 'mappings' => $formatted_mappings
8153 - ));
8154 - exit;
8155 -}
8156 -
8157 -/**
8158 - * Bulk update role restrictions for all existing content with mapped tags
8159 - */
8160 -public function ajax_bulk_update_tag_roles() {
8161 - // Verify nonce and permissions
8162 - check_ajax_referer('mxchat_prompts_setting_nonce', 'nonce');
8163 -
8164 - if (!current_user_can('manage_options')) {
8165 - wp_send_json_error('Unauthorized access');
8166 - exit;
8167 - }
8168 -
8169 - // Get mappings
8170 - $mappings = get_option('mxchat_tag_role_mappings', array());
8171 -
8172 - if (empty($mappings)) {
8173 - wp_send_json_error('No tag-role mappings found');
8174 - exit;
8175 - }
8176 -
8177 - global $wpdb;
8178 -
8179 - // Check if using Pinecone
8180 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
8181 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
8182 -
8183 - $updated_count = 0;
8184 - $details = array();
8185 -
8186 - foreach ($mappings as $tag_slug => $role_restriction) {
8187 - // Get all posts with this tag
8188 - $posts = get_posts(array(
8189 - 'tag' => $tag_slug,
8190 - 'post_type' => 'any',
8191 - 'posts_per_page' => -1,
8192 - 'fields' => 'ids',
8193 - 'post_status' => 'publish'
8194 - ));
8195 -
8196 - if (empty($posts)) {
8197 - continue;
8198 - }
8199 -
8200 - $tag_updated = 0;
8201 -
8202 - foreach ($posts as $post_id) {
8203 - $source_url = get_permalink($post_id);
8204 - if (!$source_url) {
8205 - continue;
8206 - }
8207 -
8208 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
8209 - // Update Pinecone role restriction
8210 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
8211 - $vector_id = md5($source_url);
8212 -
8213 - $result = $wpdb->replace(
8214 - $roles_table,
8215 - array(
8216 - 'vector_id' => $vector_id,
8217 - 'role_restriction' => $role_restriction,
8218 - 'updated_at' => current_time('mysql')
8219 - ),
8220 - array('%s', '%s', '%s')
8221 - );
8222 - } else {
8223 - // Update WordPress DB
8224 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
8225 -
8226 - $result = $wpdb->update(
8227 - $table_name,
8228 - array('role_restriction' => $role_restriction),
8229 - array('source_url' => $source_url),
8230 - array('%s'),
8231 - array('%s')
8232 - );
8233 - }
8234 -
8235 - if ($result !== false) {
8236 - $tag_updated++;
8237 - $updated_count++;
8238 - }
8239 - }
8240 -
8241 - if ($tag_updated > 0) {
8242 - $details[] = sprintf(
8243 - 'Tag "%s" (%s): %d posts updated',
8244 - $tag_slug,
8245 - $role_restriction,
8246 - $tag_updated
8247 - );
8248 - }
8249 - }
8250 -
8251 - wp_send_json_success(array(
8252 - 'message' => 'Bulk update completed',
8253 - 'updated_count' => $updated_count,
8254 - 'tags_processed' => count($mappings),
8255 - 'details' => $details
8256 - ));
8257 - exit;
8258 -}
8259 -
8260 -/**
8261 - * Handle tag changes on posts (when tags are added or removed)
8262 - */
8263 -public function handle_tag_change($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
8264 - // Only process post tags
8265 - if ($taxonomy !== 'post_tag') {
8266 - return;
8267 - }
8268 -
8269 - // Get tag-role mappings
8270 - $mappings = get_option('mxchat_tag_role_mappings', array());
8271 -
8272 - if (empty($mappings)) {
8273 - return;
8274 - }
8275 -
8276 - // Get the post's URL
8277 - $source_url = get_permalink($object_id);
8278 - if (!$source_url) {
8279 - return;
8280 - }
8281 -
8282 - // Determine the highest role restriction based on tags
8283 - $highest_role = 'public';
8284 - $role_hierarchy = array(
8285 - 'public' => 0,
8286 - 'logged_in' => 1,
8287 - 'subscriber' => 2,
8288 - 'contributor' => 3,
8289 - 'author' => 4,
8290 - 'editor' => 5,
8291 - 'administrator' => 6
8292 - );
8293 -
8294 - // Get all current tags for the post
8295 - $current_tags = wp_get_post_tags($object_id, array('fields' => 'slugs'));
8296 -
8297 - // Find the highest role restriction among the tags
8298 - foreach ($current_tags as $tag_slug) {
8299 - if (isset($mappings[$tag_slug])) {
8300 - $role = $mappings[$tag_slug];
8301 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
8302 - $highest_role = $role;
8303 - }
8304 - }
8305 - }
8306 -
8307 - // Update the role restriction in the database
8308 - global $wpdb;
8309 -
8310 - // Check if using Pinecone
8311 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
8312 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
8313 -
8314 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
8315 - // Update Pinecone role restriction
8316 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
8317 - $vector_id = md5($source_url);
8318 -
8319 - $wpdb->replace(
8320 - $roles_table,
8321 - array(
8322 - 'vector_id' => $vector_id,
8323 - 'role_restriction' => $highest_role,
8324 - 'updated_at' => current_time('mysql')
8325 - ),
8326 - array('%s', '%s', '%s')
8327 - );
8328 - } else {
8329 - // Update WordPress DB
8330 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
8331 -
8332 - $wpdb->update(
8333 - $table_name,
8334 - array('role_restriction' => $highest_role),
8335 - array('source_url' => $source_url),
8336 - array('%s'),
8337 - array('%s')
8338 - );
8339 - }
8340 -
8341 - // The entry's restriction just changed — keep the OpenAI Vector Store
8342 - // mirror consistent: non-public pulls the file (file_search has no
8343 - // per-role filtering), public re-mirrors it (plan 15b5c6).
8344 - if (class_exists('MxChat_Vectorstore_Manager')) {
8345 - MxChat_Vectorstore_Manager::handle_role_change($source_url, 'default', $highest_role);
8346 - }
8347 -}
8348 -
8349 -/**
8350 - * Apply role restriction after content is stored (for auto-sync)
8351 - */
8352 -public function apply_role_restriction_after_storage($post_id, $source_url) {
8353 - // Get tag-role mappings
8354 - $mappings = get_option('mxchat_tag_role_mappings', array());
8355 -
8356 - if (empty($mappings)) {
8357 - return;
8358 - }
8359 -
8360 - // Get all tags for the post
8361 - $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));
8362 -
8363 - if (empty($post_tags)) {
8364 - return;
8365 - }
8366 -
8367 - // Determine the highest role restriction based on tags
8368 - $highest_role = 'public';
8369 - $role_hierarchy = array(
8370 - 'public' => 0,
8371 - 'logged_in' => 1,
8372 - 'subscriber' => 2,
8373 - 'contributor' => 3,
8374 - 'author' => 4,
8375 - 'editor' => 5,
8376 - 'administrator' => 6
8377 - );
8378 -
8379 - foreach ($post_tags as $tag_slug) {
8380 - if (isset($mappings[$tag_slug])) {
8381 - $role = $mappings[$tag_slug];
8382 - if (isset($role_hierarchy[$role]) && $role_hierarchy[$role] > $role_hierarchy[$highest_role]) {
8383 - $highest_role = $role;
8384 - }
8385 - }
8386 - }
8387 -
8388 - // If no restricted tags found, return (leave as public)
8389 - if ($highest_role === 'public') {
8390 - return;
8391 - }
8392 -
8393 - // Update the role restriction
8394 - global $wpdb;
8395 -
8396 - // Check if using Pinecone
8397 - $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
8398 - $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
8399 -
8400 - if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
8401 - // Update Pinecone role restriction
8402 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
8403 - $vector_id = md5($source_url);
8404 -
8405 - $wpdb->replace(
8406 - $roles_table,
8407 - array(
8408 - 'vector_id' => $vector_id,
8409 - 'role_restriction' => $highest_role,
8410 - 'updated_at' => current_time('mysql')
8411 - ),
8412 - array('%s', '%s', '%s')
8413 - );
8414 - } else {
8415 - // Update WordPress DB
8416 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
8417 -
8418 - $wpdb->update(
8419 - $table_name,
8420 - array('role_restriction' => $highest_role),
8421 - array('source_url' => $source_url),
8422 - array('%s'),
8423 - array('%s')
8424 - );
8425 - }
8426 -
8427 - // The entry's restriction just changed — keep the OpenAI Vector Store
8428 - // mirror consistent: non-public pulls the file (file_search has no
8429 - // per-role filtering), public re-mirrors it (plan 15b5c6).
8430 - if (class_exists('MxChat_Vectorstore_Manager')) {
8431 - MxChat_Vectorstore_Manager::handle_role_change($source_url, 'default', $highest_role);
8432 - }
8433 -}
8434 -
8435 -
8436 - // ========================================
8437 - // HELPER METHODS
8438 - // ========================================
8439 -
8440 - /**
8441 - * Check if user has required permissions for content processing
8442 - */
8443 - private function mxchat_check_user_permissions() {
8444 - if (!current_user_can('manage_options')) {
8445 - wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
8446 - }
8447 - }
8448 -
8449 - /**
8450 - * Validate nonce for security
8451 - */
8452 - private function mxchat_validate_nonce($nonce_name, $nonce_action) {
8453 - if (!isset($_POST[$nonce_name]) || !wp_verify_nonce($_POST[$nonce_name], $nonce_action)) {
8454 - wp_die(esc_html__('Security check failed.', 'mxchat'));
8455 - }
8456 - }
8457 -
8458 - /**
8459 - * Get embedding API credentials
8460 - */
8461 - private function mxchat_get_embedding_credentials() {
8462 - $embedding_model = $this->options['embedding_model'] ?? 'text-embedding-ada-002';
8463 -
8464 - if (strpos($embedding_model, 'text-embedding-') !== false) {
8465 - return array(
8466 - 'type' => 'openai',
8467 - 'api_key' => $this->options['api_key'] ?? ''
8468 - );
8469 - } elseif (strpos($embedding_model, 'voyage-') !== false) {
8470 - return array(
8471 - 'type' => 'voyage',
8472 - 'api_key' => $this->options['voyage_api_key'] ?? ''
8473 - );
8474 - } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
8475 - return array(
8476 - 'type' => 'gemini',
8477 - 'api_key' => $this->options['gemini_api_key'] ?? ''
8478 - );
8479 - }
8480 -
8481 - return array('type' => 'unknown', 'api_key' => '');
8482 - }
8483 -
8484 - /**
8485 - * Log processing errors
8486 - */
8487 - private function mxchat_log_processing_error($operation, $error_message) {
8488 - //error_log("MxChat Knowledge Processing {$operation} Error: " . $error_message);
8489 - }
8490 -
8491 - /**
8492 - * Set admin notice transient
8493 - */
8494 - private function mxchat_set_admin_notice($type, $message) {
8495 - set_transient("mxchat_admin_notice_{$type}", $message, 30);
8496 - }
8497 -
8498 - /**
8499 - * Get Pinecone manager instance for vector operations
8500 - */
8501 - private function mxchat_get_pinecone_manager() {
8502 - return MxChat_Pinecone_Manager::get_instance();
8503 - }
8504 -
8505 -
8506 - // ========================================
8507 -// DATABASE QUEUE TABLE MANAGEMENT
8508 -// ========================================
8509 -
8510 -/**
8511 - * Create queue table on plugin activation
8512 - * Call this from your plugin activation hook
8513 - */
8514 -public function mxchat_create_queue_table() {
8515 - global $wpdb;
8516 -
8517 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8518 - $charset_collate = $wpdb->get_charset_collate();
8519 -
8520 - $sql = "CREATE TABLE IF NOT EXISTS $table_name (
8521 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
8522 - queue_id varchar(64) NOT NULL,
8523 - item_type varchar(20) NOT NULL,
8524 - item_data longtext NOT NULL,
8525 - status varchar(20) NOT NULL DEFAULT 'pending',
8526 - bot_id varchar(50) NOT NULL DEFAULT 'default',
8527 - priority int(11) NOT NULL DEFAULT 0,
8528 - attempts int(11) NOT NULL DEFAULT 0,
8529 - max_attempts int(11) NOT NULL DEFAULT 3,
8530 - error_message text DEFAULT NULL,
8531 - created_at datetime NOT NULL,
8532 - started_at datetime DEFAULT NULL,
8533 - completed_at datetime DEFAULT NULL,
8534 - PRIMARY KEY (id),
8535 - KEY queue_id (queue_id),
8536 - KEY status (status),
8537 - KEY item_type (item_type),
8538 - KEY priority (priority)
8539 - ) $charset_collate;";
8540 -
8541 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
8542 - dbDelta($sql);
8543 -
8544 - // Also create a meta table for queue metadata
8545 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
8546 -
8547 - $meta_sql = "CREATE TABLE IF NOT EXISTS $meta_table (
8548 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
8549 - queue_id varchar(64) NOT NULL,
8550 - meta_key varchar(255) NOT NULL,
8551 - meta_value longtext,
8552 - PRIMARY KEY (id),
8553 - KEY queue_id (queue_id),
8554 - KEY meta_key (meta_key)
8555 - ) $charset_collate;";
8556 -
8557 - dbDelta($meta_sql);
8558 -}
8559 -
8560 -/**
8561 - * Add items to the processing queue
8562 - *
8563 - * @param string $queue_id Unique identifier for this queue batch
8564 - * @param string $item_type Type of item (url, pdf_page)
8565 - * @param array $items Array of items to queue
8566 - * @param string $bot_id Bot ID for processing
8567 - * @return int Number of items queued
8568 - */
8569 -private function mxchat_add_to_queue($queue_id, $item_type, $items, $bot_id = 'default') {
8570 - global $wpdb;
8571 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8572 -
8573 - $queued_count = 0;
8574 - $priority = 0;
8575 -
8576 - foreach ($items as $item) {
8577 - $result = $wpdb->insert(
8578 - $table_name,
8579 - array(
8580 - 'queue_id' => $queue_id,
8581 - 'item_type' => $item_type,
8582 - 'item_data' => wp_json_encode($item),
8583 - 'status' => 'pending',
8584 - 'bot_id' => $bot_id,
8585 - 'priority' => $priority,
8586 - 'attempts' => 0,
8587 - 'max_attempts' => 3,
8588 - 'created_at' => current_time('mysql')
8589 - ),
8590 - array('%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%s')
8591 - );
8592 -
8593 - if ($result) {
8594 - $queued_count++;
8595 - }
8596 -
8597 - $priority++; // Process in order
8598 - }
8599 -
8600 - return $queued_count;
8601 -}
8602 -
8603 -/**
8604 - * Store queue metadata (total counts, source URL, etc.)
8605 - */
8606 -private function mxchat_set_queue_meta($queue_id, $meta_key, $meta_value) {
8607 - global $wpdb;
8608 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
8609 -
8610 - // Check if meta exists
8611 - $existing = $wpdb->get_var($wpdb->prepare(
8612 - "SELECT id FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
8613 - $queue_id,
8614 - $meta_key
8615 - ));
8616 -
8617 - if ($existing) {
8618 - // Update
8619 - $wpdb->update(
8620 - $meta_table,
8621 - array('meta_value' => maybe_serialize($meta_value)),
8622 - array('queue_id' => $queue_id, 'meta_key' => $meta_key),
8623 - array('%s'),
8624 - array('%s', '%s')
8625 - );
8626 - } else {
8627 - // Insert
8628 - $wpdb->insert(
8629 - $meta_table,
8630 - array(
8631 - 'queue_id' => $queue_id,
8632 - 'meta_key' => $meta_key,
8633 - 'meta_value' => maybe_serialize($meta_value)
8634 - ),
8635 - array('%s', '%s', '%s')
8636 - );
8637 - }
8638 -}
8639 -
8640 -/**
8641 - * Get queue metadata
8642 - */
8643 -private function mxchat_get_queue_meta($queue_id, $meta_key) {
8644 - global $wpdb;
8645 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
8646 -
8647 - $value = $wpdb->get_var($wpdb->prepare(
8648 - "SELECT meta_value FROM $meta_table WHERE queue_id = %s AND meta_key = %s",
8649 - $queue_id,
8650 - $meta_key
8651 - ));
8652 -
8653 - return maybe_unserialize($value);
8654 -}
8655 -
8656 -// ========================================
8657 -// AJAX QUEUE PROCESSING HANDLERS
8658 -// ========================================
8659 -
8660 -/**
8661 - * AJAX: Get next item from queue to process
8662 - */
8663 -public function ajax_mxchat_get_next_queue_item() {
8664 - // Verify nonce and permissions
8665 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8666 -
8667 - if (!current_user_can('manage_options')) {
8668 - wp_send_json_error('Unauthorized access');
8669 - }
8670 -
8671 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
8672 -
8673 - if (empty($queue_id)) {
8674 - wp_send_json_error('Missing queue ID');
8675 - }
8676 -
8677 - global $wpdb;
8678 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8679 -
8680 - // Get next pending item with retry logic for failed items
8681 - $next_item = $wpdb->get_row($wpdb->prepare(
8682 - "SELECT * FROM $table_name
8683 - WHERE queue_id = %s
8684 - AND status IN ('pending', 'failed')
8685 - AND attempts < max_attempts
8686 - ORDER BY priority ASC, id ASC
8687 - LIMIT 1",
8688 - $queue_id
8689 - ));
8690 -
8691 - if (!$next_item) {
8692 - // No more items - queue complete
8693 - wp_send_json_success(array(
8694 - 'complete' => true,
8695 - 'message' => 'Queue processing complete'
8696 - ));
8697 - }
8698 -
8699 - // Mark item as processing
8700 - $wpdb->update(
8701 - $table_name,
8702 - array(
8703 - 'status' => 'processing',
8704 - 'started_at' => current_time('mysql'),
8705 - 'attempts' => $next_item->attempts + 1
8706 - ),
8707 - array('id' => $next_item->id),
8708 - array('%s', '%s', '%d'),
8709 - array('%d')
8710 - );
8711 -
8712 - wp_send_json_success(array(
8713 - 'complete' => false,
8714 - 'item' => array(
8715 - 'id' => $next_item->id,
8716 - 'type' => $next_item->item_type,
8717 - 'data' => json_decode($next_item->item_data, true),
8718 - 'bot_id' => $next_item->bot_id,
8719 - 'attempt' => $next_item->attempts + 1
8720 - )
8721 - ));
8722 -}
8723 -
8724 -/**
8725 - * AJAX: Process a single queue item
8726 - */
8727 -public function ajax_mxchat_process_queue_item() {
8728 - // Verify nonce and permissions
8729 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
8730 -
8731 - if (!current_user_can('manage_options')) {
8732 - wp_send_json_error('Unauthorized access');
8733 - }
8734 -
8735 - $item_id = isset($_POST['item_id']) ? absint($_POST['item_id']) : 0;
8736 - $item_type = isset($_POST['item_type']) ? sanitize_text_field($_POST['item_type']) : '';
8737 - $item_data = isset($_POST['item_data']) ? $_POST['item_data'] : array();
8738 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
8739 -
8740 - if (empty($item_id) || empty($item_type)) {
8741 - wp_send_json_error('Missing item data');
8742 - }
8743 -
8744 - global $wpdb;
8745 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
8746 -
8747 - // Process based on item type
8748 - try {
8749 - set_time_limit(60); // Give processing 60 seconds
8750 -
8751 - $result = false;
8752 - $error_message = '';
8753 -
8754 - // Read item directly from DB to get queue_id and preserve special chars in item_data
8755 - // (POST round-trip through JS mangles characters like apostrophes in URLs)
8756 - $db_item = $wpdb->get_row($wpdb->prepare(
8757 - "SELECT queue_id, item_data FROM $table_name WHERE id = %d",
8758 - $item_id
8759 - ));
8760 - $item_queue_id = $db_item ? $db_item->queue_id : '';
8761 - if ($db_item && !empty($db_item->item_data)) {
8762 - $db_data = json_decode($db_item->item_data, true);
8763 - if (is_array($db_data)) {
8764 - $item_data = $db_data;
8765 - }
8766 - }
8767 -
8768 - switch ($item_type) {
8769 - case 'url':
8770 - $result = $this->mxchat_process_queue_url($item_data, $bot_id, $item_queue_id);
8771 - break;
8772 -
8773 - case 'pdf_page':
8774 - $result = $this->mxchat_process_queue_pdf_page($item_data, $bot_id);
8775 - break;
8776 -
8777 - default:
8778 - throw new Exception('Unknown item type: ' . $item_type);
8779 - }
8780 -
8781 - if (is_wp_error($result)) {
8782 - $error_code = $result->get_error_code();
8783 - // Content errors (empty page, sanitization) are permanent — retrying won't help
8784 - $permanent_codes = array('empty_page', 'empty_after_sanitization', 'no_api_key', 'page_not_found');
8785 - if (in_array($error_code, $permanent_codes)) {
8786 - // Mark as permanently failed — set attempts = max_attempts so it won't be retried
8787 - $current_item = $wpdb->get_row($wpdb->prepare(
8788 - "SELECT max_attempts FROM $table_name WHERE id = %d", $item_id
8789 - ));
8790 - $wpdb->update(
8791 - $table_name,
8792 - array(
8793 - 'status' => 'failed',
8794 - 'error_message' => $result->get_error_message(),
8795 - 'attempts' => $current_item ? $current_item->max_attempts : 3
8796 - ),
8797 - array('id' => $item_id),
8798 - array('%s', '%s', '%d'),
8799 - array('%d')
8800 - );
8801 - wp_send_json_error(array(
8802 - 'message' => $result->get_error_message(),
8803 - 'permanent_failure' => true,
8804 - 'item_id' => $item_id
8805 - ));
8806 - return;
8807 - }
8808 - throw new Exception($result->get_error_message());
8809 - }
8810 -
8811 - if ($result === false) {
8812 - throw new Exception('Processing returned false - item may be empty or invalid');
8813 - }
8814 -
8815 - // Mark as completed
8816 - $wpdb->update(
8817 - $table_name,
8818 - array(
8819 - 'status' => 'completed',
8820 - 'completed_at' => current_time('mysql'),
8821 - 'error_message' => null
8822 - ),
8823 - array('id' => $item_id),
8824 - array('%s', '%s', '%s'),
8825 - array('%d')
8826 - );
8827 -
8828 - wp_send_json_success(array(
8829 - 'processed' => true,
8830 - 'item_id' => $item_id,
8831 - 'message' => 'Item processed successfully'
8832 - ));
8833 -
8834 - } catch (Exception $e) {
8835 - $error_message = $e->getMessage();
8836 -
8837 - // Get current attempt count
8838 - $item = $wpdb->get_row($wpdb->prepare(
8839 - "SELECT attempts, max_attempts FROM $table_name WHERE id = %d",
8840 - $item_id
8841 - ));
8842 -
8843 - // Check if we've exhausted retries
8844 - if ($item && $item->attempts >= $item->max_attempts) {
8845 - // Permanently failed
8846 - $wpdb->update(
8847 - $table_name,
8848 - array(
8849 - 'status' => 'failed',
8850 - 'error_message' => $error_message
8851 - ),
8852 - array('id' => $item_id),
8853 - array('%s', '%s'),
8854 - array('%d')
8855 - );
8856 -
8857 - wp_send_json_error(array(
8858 - 'message' => 'Item failed after maximum attempts: ' . $error_message,
8859 - 'permanent_failure' => true,
8860 - 'item_id' => $item_id
8861 - ));
8862 - } else {
8863 - // Mark for retry
8864 - $wpdb->update(
8865 - $table_name,
8866 - array(
8867 - 'status' => 'failed',
8868 - 'error_message' => $error_message
8869 - ),
8870 - array('id' => $item_id),
8871 - array('%s', '%s'),
8872 - array('%d')
8873 - );
8874 -
8875 - wp_send_json_error(array(
8876 - 'message' => 'Item processing failed, will retry: ' . $error_message,
8877 - 'can_retry' => true,
8878 - 'item_id' => $item_id,
8879 - 'attempts' => $item ? $item->attempts : 0
8880 - ));
8881 - }
8882 - }
8883 -}
8884 -
8885 -/**
8886 - * Process a URL from the queue
8887 - */
8888 -private function mxchat_process_queue_url($item_data, $bot_id = 'default', $queue_id = '') {
8889 - $url = isset($item_data['url']) ? $item_data['url'] : '';
8890 -
8891 - if (empty($url)) {
8892 - return new WP_Error('invalid_url', 'URL is empty');
8893 - }
8894 -
8895 - // Get bot-specific embedding decision early (needed for both paths) —
8896 - // custom-provider-aware (plan cbd5fd). Error code preserved.
8897 - $bot_options = $this->get_bot_options($bot_id);
8898 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
8899 -
8900 - $preflight = MxChat_Utils::embedding_preflight($options);
8901 - if (!$preflight['ok']) {
8902 - return new WP_Error('no_api_key', $preflight['reason'] . ' (bot: ' . $bot_id . ')');
8903 - }
8904 - $api_key = $preflight['api_key'];
8905 -
8906 - // Check if this is a WooCommerce product URL and WooCommerce is active
8907 - $is_product_url = (strpos($url, '/product/') !== false || strpos($url, '/shop/') !== false);
8908 - $content_type = $is_product_url ? 'product' : 'url';
8909 -
8910 - // Try to get WooCommerce product data if it's a product URL
8911 - if ($is_product_url && class_exists('WooCommerce')) {
8912 - $product_content = $this->mxchat_extract_woocommerce_product_content($url);
8913 -
8914 - if (!empty($product_content)) {
8915 - // Successfully extracted WooCommerce product data with pricing
8916 - $result = MxChat_Utils::submit_content_to_db(
8917 - $product_content,
8918 - $url,
8919 - $api_key,
8920 - null,
8921 - $bot_id,
8922 - 'product'
8923 - );
8924 - return $result;
8925 - }
8926 - // If WooCommerce extraction failed, fall through to HTML extraction
8927 - }
8928 -
8929 - // Fetch URL content (fallback for non-products or when WooCommerce extraction fails)
8930 - $is_likely_pdf = (strtolower(pathinfo(parse_url($url, PHP_URL_PATH) ?: '', PATHINFO_EXTENSION)) === 'pdf');
8931 - $response = wp_remote_get($url, array(
8932 - 'timeout' => $is_likely_pdf ? 120 : 30,
8933 - 'redirection' => 5,
8934 - 'user-agent' => mxchat_ingest_user_agent(),
8935 - ));
8936 -
8937 - if (is_wp_error($response)) {
8938 - return $response;
8939 - }
8940 -
8941 - $response_code = wp_remote_retrieve_response_code($response);
8942 - if ($response_code !== 200) {
8943 - return new WP_Error('http_error', 'HTTP ' . $response_code . ' error for: ' . $url);
8944 - }
8945 -
8946 - // Check if URL is a PDF — expand into per-page queue items using the standard PDF pipeline
8947 - if ($this->mxchat_is_pdf_url($url, $response)) {
8948 - return $this->mxchat_expand_pdf_to_queue($url, $response, $bot_id, $queue_id);
8949 - }
8950 -
8951 - $html = wp_remote_retrieve_body($response);
8952 -
8953 - if (empty($html)) {
8954 - return new WP_Error('empty_response', 'Empty response body');
8955 - }
8956 -
8957 - // Extract and sanitize content
8958 - $content = $this->mxchat_extract_main_content($html);
8959 - $sanitized = $this->mxchat_sanitize_content_for_api($content);
8960 -
8961 - if (empty($sanitized)) {
8962 - // Not an error - just no content found (maybe a redirect or empty page)
8963 - return false;
8964 - }
8965 -
8966 - // Submit to database with content_type
8967 - $result = MxChat_Utils::submit_content_to_db(
8968 - $sanitized,
8969 - $url,
8970 - $api_key,
8971 - null,
8972 - $bot_id,
8973 - $content_type
8974 - );
8975 -
8976 - return $result;
8977 -}
8978 -
8979 -/**
8980 - * Expand a PDF URL into per-page queue items using the standard PDF pipeline.
8981 - * Called when a sitemap URL turns out to be a PDF — downloads, parses page count,
8982 - * and adds pdf_page items to the same queue so they process with full progress tracking.
8983 - */
8984 -private function mxchat_expand_pdf_to_queue($pdf_url, $response, $bot_id = 'default', $queue_id = '') {
8985 - set_time_limit(120); // PDFs need extra time for download + parsing
8986 -
8987 - $upload_dir = wp_upload_dir();
8988 - $pdf_filename = sanitize_file_name('mxchat_kb_' . md5($pdf_url) . '.pdf');
8989 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
8990 -
8991 - $response_body = wp_remote_retrieve_body($response);
8992 - if (empty($response_body)) {
8993 - return new WP_Error('empty_pdf', 'Empty PDF response for: ' . $pdf_url);
8994 - }
8995 -
8996 - if (!wp_mkdir_p(dirname($pdf_path))) {
8997 - return new WP_Error('dir_error', 'Failed to create upload directory');
8998 - }
8999 -
9000 - file_put_contents($pdf_path, $response_body);
9001 -
9002 - if (!file_exists($pdf_path)) {
9003 - return new WP_Error('save_error', 'Failed to save PDF file');
9004 - }
9005 -
9006 - try {
9007 - $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
9008 -
9009 - if ($total_pages === false || $total_pages < 1) {
9010 - wp_delete_file($pdf_path);
9011 - return new WP_Error('no_pages', 'PDF has no pages: ' . $pdf_url);
9012 - }
9013 -
9014 - // Build per-page items identical to mxchat_handle_pdf_for_knowledge_base
9015 - $pages = array();
9016 - for ($i = 1; $i <= $total_pages; $i++) {
9017 - $pages[] = array(
9018 - 'pdf_path' => $pdf_path,
9019 - 'pdf_url' => $pdf_url,
9020 - 'page_number' => $i,
9021 - 'total_pages' => $total_pages
9022 - );
9023 - }
9024 -
9025 - // Add pdf_page items to the SAME queue so the JS picks them up automatically
9026 - if (!empty($queue_id)) {
9027 - $queued_count = $this->mxchat_add_to_queue($queue_id, 'pdf_page', $pages, $bot_id);
9028 - } else {
9029 - // Fallback: create a new PDF queue (shouldn't happen in sitemap flow)
9030 - $new_queue_id = 'pdf_' . md5($pdf_url . time());
9031 - $queued_count = $this->mxchat_add_to_queue($new_queue_id, 'pdf_page', $pages, $bot_id);
9032 - $this->mxchat_set_queue_meta($new_queue_id, 'source_url', $pdf_url);
9033 - $this->mxchat_set_queue_meta($new_queue_id, 'queue_type', 'pdf');
9034 - $this->mxchat_set_queue_meta($new_queue_id, 'total_items', $total_pages);
9035 - $this->mxchat_set_queue_meta($new_queue_id, 'bot_id', $bot_id);
9036 - $this->mxchat_set_queue_meta($new_queue_id, 'pdf_path', $pdf_path);
9037 - $this->mxchat_set_queue_meta($new_queue_id, 'created_at', current_time('mysql'));
9038 - }
9039 -
9040 - if ($queued_count === 0) {
9041 - wp_delete_file($pdf_path);
9042 - return new WP_Error('queue_error', 'Failed to add PDF pages to queue');
9043 - }
9044 -
9045 - // Return true so the original URL item is marked complete
9046 - // The new pdf_page items will be processed in subsequent batches
9047 - return true;
9048 -
9049 - } catch (Exception $e) {
9050 - if (file_exists($pdf_path)) {
9051 - wp_delete_file($pdf_path);
9052 - }
9053 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
9054 - }
9055 -}
9056 -
9057 -/**
9058 - * Legacy: Process a PDF URL inline during sitemap queue processing.
9059 - * @deprecated Use mxchat_expand_pdf_to_queue instead — kept for reference only.
9060 - */
9061 -private function mxchat_process_pdf_url_inline($pdf_url, $response, $api_key, $bot_id = 'default') {
9062 - set_time_limit(120); // PDFs need more time — downloading + parsing all pages
9063 -
9064 - $upload_dir = wp_upload_dir();
9065 - $pdf_filename = sanitize_file_name('mxchat_kb_' . md5($pdf_url) . '.pdf');
9066 - $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
9067 -
9068 - $response_body = wp_remote_retrieve_body($response);
9069 - if (empty($response_body)) {
9070 - return new WP_Error('empty_pdf', 'Empty PDF response');
9071 - }
9072 -
9073 - if (!wp_mkdir_p(dirname($pdf_path))) {
9074 - return new WP_Error('dir_error', 'Failed to create upload directory');
9075 - }
9076 -
9077 - file_put_contents($pdf_path, $response_body);
9078 -
9079 - if (!file_exists($pdf_path)) {
9080 - return new WP_Error('save_error', 'Failed to save PDF file');
9081 - }
9082 -
9083 - try {
9084 - mxchat_load_pdf_parser();
9085 - $parser = new \Smalot\PdfParser\Parser();
9086 - $pdf = $parser->parseFile($pdf_path);
9087 - $pages = $pdf->getPages();
9088 - $total_pages = count($pages);
9089 -
9090 - if ($total_pages < 1) {
9091 - wp_delete_file($pdf_path);
9092 - return new WP_Error('no_pages', 'PDF has no pages');
9093 - }
9094 -
9095 - $processed = 0;
9096 - $skipped_pages = array();
9097 -
9098 - for ($i = 0; $i < $total_pages; $i++) {
9099 - $page_num = $i + 1;
9100 - $text = $pages[$i]->getText();
9101 - $text = MxChat_Utils::normalize_pdf_rtl($text, 'kb_pdf_import page ' . $page_num);
9102 - if (empty($text)) {
9103 - $skipped_pages[] = 'Page ' . $page_num . ': No text could be extracted — page may contain only images, links, or non-standard encoding';
9104 - continue;
9105 - }
9106 -
9107 - $sanitized = $this->mxchat_sanitize_content_for_api($text);
9108 - if (empty($sanitized)) {
9109 - $skipped_pages[] = 'Page ' . $page_num . ': Text was extracted but contained only special characters, control codes, or unsupported content';
9110 - continue;
9111 - }
9112 -
9113 - $metadata = array(
9114 - 'document_type' => 'pdf',
9115 - 'total_pages' => $total_pages,
9116 - 'current_page' => $page_num,
9117 - 'source_url' => $pdf_url,
9118 - );
9119 -
9120 - $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized;
9121 - $page_url = esc_url($pdf_url . '#page=' . $page_num);
9122 -
9123 - MxChat_Utils::submit_content_to_db(
9124 - $content_with_metadata,
9125 - $page_url,
9126 - $api_key,
9127 - null,
9128 - $bot_id,
9129 - 'pdf'
9130 - );
9131 -
9132 - $processed++;
9133 - }
9134 -
9135 - // Clean up the temp PDF file
9136 - wp_delete_file($pdf_path);
9137 -
9138 - if (!empty($skipped_pages)) {
9139 - error_log('MxChat PDF: Skipped ' . count($skipped_pages) . ' of ' . $total_pages . ' pages: ' . implode('; ', $skipped_pages));
9140 - }
9141 -
9142 - return $processed > 0 ? true : false;
9143 -
9144 - } catch (Exception $e) {
9145 - if (file_exists($pdf_path)) {
9146 - wp_delete_file($pdf_path);
9147 - }
9148 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
9149 - }
9150 -}
9151 -
9152 -/**
9153 - * Extract WooCommerce product content including pricing
9154 - *
9155 - * @param string $url The product URL
9156 - * @return string|false Product content with pricing, or false if not found
9157 - */
9158 -private function mxchat_extract_woocommerce_product_content($url) {
9159 - // Try to get product ID from URL
9160 - $product_id = url_to_postid($url);
9161 -
9162 - // If url_to_postid fails, try to extract from URL pattern
9163 - if (!$product_id) {
9164 - $product_slug = '';
9165 -
9166 - // Handle pretty permalinks: /product/product-name/
9167 - if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
9168 - $product_slug = $matches[1];
9169 - }
9170 -
9171 - if (!empty($product_slug)) {
9172 - $product_post = get_page_by_path($product_slug, OBJECT, 'product');
9173 - if ($product_post) {
9174 - $product_id = $product_post->ID;
9175 - }
9176 - }
9177 - }
9178 -
9179 - if (!$product_id) {
9180 - return false;
9181 - }
9182 -
9183 - // Get WooCommerce product object
9184 - $product = wc_get_product($product_id);
9185 -
9186 - if (!$product) {
9187 - return false;
9188 - }
9189 -
9190 - // Build product content via the shared WC-object assembler (a3d60c) — same
9191 - // body as the auto-sync product writer, so the two paths can never drift.
9192 - $content = $this->mxchat_prepare_product_content_for_indexing($product);
9193 -
9194 - return $this->mxchat_sanitize_content_for_api($content);
9195 -}
9196 -
9197 -/**
9198 - * Process a PDF page from the queue
9199 - */
9200 -private function mxchat_process_queue_pdf_page($item_data, $bot_id = 'default') {
9201 - $pdf_path = isset($item_data['pdf_path']) ? $item_data['pdf_path'] : '';
9202 - $pdf_url = isset($item_data['pdf_url']) ? $item_data['pdf_url'] : '';
9203 - $page_number = isset($item_data['page_number']) ? absint($item_data['page_number']) : 0;
9204 - $total_pages = isset($item_data['total_pages']) ? absint($item_data['total_pages']) : 0;
9205 -
9206 - if (empty($pdf_path) || !file_exists($pdf_path)) {
9207 - return new WP_Error('pdf_not_found', 'PDF file not found: ' . $pdf_path);
9208 - }
9209 -
9210 - if ($page_number < 1) {
9211 - return new WP_Error('invalid_page', 'Invalid page number');
9212 - }
9213 -
9214 - try {
9215 - mxchat_load_pdf_parser();
9216 - $parser = new \Smalot\PdfParser\Parser();
9217 - $pdf = $parser->parseFile($pdf_path);
9218 - $pages = $pdf->getPages();
9219 -
9220 - if (!isset($pages[$page_number - 1])) {
9221 - return new WP_Error('page_not_found', 'Page ' . $page_number . ' not found in PDF');
9222 - }
9223 -
9224 - $text = $pages[$page_number - 1]->getText();
9225 - $text = MxChat_Utils::normalize_pdf_rtl($text, 'kb_pdf_page page ' . $page_number);
9226 -
9227 - if (empty($text)) {
9228 - return new WP_Error('empty_page', 'Page ' . $page_number . ': No text could be extracted — page may contain only images, links, or non-standard encoding');
9229 - }
9230 -
9231 - $sanitized = $this->mxchat_sanitize_content_for_api($text);
9232 -
9233 - if (empty($sanitized)) {
9234 - 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');
9235 - }
9236 -
9237 - // Create metadata
9238 - $metadata = array(
9239 - 'document_type' => 'pdf',
9240 - 'total_pages' => $total_pages,
9241 - 'current_page' => $page_number,
9242 - 'source_url' => $pdf_url
9243 - );
9244 -
9245 - $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized;
9246 - $page_url = esc_url($pdf_url . "#page=" . $page_number);
9247 -
9248 - // Get bot-specific embedding decision — custom-provider-aware
9249 - // (plan cbd5fd). Error code preserved.
9250 - $bot_options = $this->get_bot_options($bot_id);
9251 - $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
9252 -
9253 - $preflight = MxChat_Utils::embedding_preflight($options);
9254 - if (!$preflight['ok']) {
9255 - return new WP_Error('no_api_key', $preflight['reason'] . ' (bot: ' . $bot_id . ')');
9256 - }
9257 - $api_key = $preflight['api_key'];
9258 -
9259 - // Submit to database - UPDATED 2.5.6: Added content_type 'pdf'
9260 - $result = MxChat_Utils::submit_content_to_db(
9261 - $content_with_metadata,
9262 - $page_url,
9263 - $api_key,
9264 - null,
9265 - $bot_id,
9266 - 'pdf'
9267 - );
9268 -
9269 - return $result;
9270 -
9271 - } catch (Exception $e) {
9272 - return new WP_Error('pdf_parse_error', 'Error parsing PDF: ' . $e->getMessage());
9273 - }
9274 -}
9275 -
9276 -/**
9277 - * AJAX: Get queue processing status
9278 - */
9279 -public function ajax_mxchat_get_queue_status() {
9280 - // Verify nonce and permissions
9281 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
9282 -
9283 - if (!current_user_can('manage_options')) {
9284 - wp_send_json_error('Unauthorized access');
9285 - }
9286 -
9287 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
9288 -
9289 - if (empty($queue_id)) {
9290 - wp_send_json_error('Missing queue ID');
9291 - }
9292 -
9293 - global $wpdb;
9294 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
9295 -
9296 - // Get counts by status
9297 - $counts = $wpdb->get_results($wpdb->prepare(
9298 - "SELECT status, COUNT(*) as count
9299 - FROM $table_name
9300 - WHERE queue_id = %s
9301 - GROUP BY status",
9302 - $queue_id
9303 - ), OBJECT_K);
9304 -
9305 - $total = 0;
9306 - $completed = 0;
9307 - $failed = 0;
9308 - $processing = 0;
9309 - $pending = 0;
9310 -
9311 - foreach ($counts as $status => $data) {
9312 - $count = absint($data->count);
9313 - $total += $count;
9314 -
9315 - switch ($status) {
9316 - case 'completed':
9317 - $completed = $count;
9318 - break;
9319 - case 'failed':
9320 - $failed = $count;
9321 - break;
9322 - case 'processing':
9323 - $processing = $count;
9324 - break;
9325 - case 'pending':
9326 - $pending = $count;
9327 - break;
9328 - }
9329 - }
9330 -
9331 - // Calculate percentage
9332 - $percentage = $total > 0 ? round((($completed + $failed) / $total) * 100) : 0;
9333 -
9334 - // Get failed items details (include all failed items, not just those that exhausted retries)
9335 - $failed_items = array();
9336 - if ($failed > 0) {
9337 - $failed_items = $wpdb->get_results($wpdb->prepare(
9338 - "SELECT item_type, item_data, error_message, attempts
9339 - FROM $table_name
9340 - WHERE queue_id = %s
9341 - AND status = 'failed'
9342 - ORDER BY id DESC
9343 - LIMIT 50",
9344 - $queue_id
9345 - ));
9346 - }
9347 -
9348 - // Get queue metadata
9349 - $source_url = $this->mxchat_get_queue_meta($queue_id, 'source_url');
9350 - $queue_type = $this->mxchat_get_queue_meta($queue_id, 'queue_type');
9351 -
9352 - // Determine if queue is complete
9353 - $is_complete = ($pending === 0 && $processing === 0);
9354 -
9355 - wp_send_json_success(array(
9356 - 'queue_id' => $queue_id,
9357 - 'queue_type' => $queue_type,
9358 - 'source_url' => $source_url,
9359 - 'total' => $total,
9360 - 'completed' => $completed,
9361 - 'failed' => $failed,
9362 - 'processing' => $processing,
9363 - 'pending' => $pending,
9364 - 'percentage' => $percentage,
9365 - 'is_complete' => $is_complete,
9366 - 'failed_items' => $failed_items,
9367 - 'status' => $is_complete ? 'complete' : 'processing'
9368 - ));
9369 -}
9370 -
9371 -/**
9372 - * AJAX: Clear completed queue
9373 - */
9374 -public function ajax_mxchat_clear_queue() {
9375 - // Verify nonce and permissions
9376 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
9377 -
9378 - if (!current_user_can('manage_options')) {
9379 - wp_send_json_error('Unauthorized access');
9380 - }
9381 -
9382 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
9383 -
9384 - if (empty($queue_id)) {
9385 - wp_send_json_error('Missing queue ID');
9386 - }
9387 -
9388 - global $wpdb;
9389 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
9390 - $meta_table = $wpdb->prefix . 'mxchat_queue_meta';
9391 -
9392 - // Delete queue items
9393 - $wpdb->delete(
9394 - $table_name,
9395 - array('queue_id' => $queue_id),
9396 - array('%s')
9397 - );
9398 -
9399 - // Delete queue metadata
9400 - $wpdb->delete(
9401 - $meta_table,
9402 - array('queue_id' => $queue_id),
9403 - array('%s')
9404 - );
9405 -
9406 - wp_send_json_success(array(
9407 - 'message' => 'Queue cleared successfully'
9408 - ));
9409 -}
9410 -
9411 -/**
9412 - * AJAX: Retry failed items in queue
9413 - */
9414 -public function ajax_mxchat_retry_failed() {
9415 - // Verify nonce and permissions
9416 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
9417 -
9418 - if (!current_user_can('manage_options')) {
9419 - wp_send_json_error('Unauthorized access');
9420 - }
9421 -
9422 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
9423 -
9424 - if (empty($queue_id)) {
9425 - wp_send_json_error('Missing queue ID');
9426 - }
9427 -
9428 - global $wpdb;
9429 - $table_name = $wpdb->prefix . 'mxchat_processing_queue';
9430 -
9431 - // Reset failed items to pending and reset attempt count
9432 - $updated = $wpdb->update(
9433 - $table_name,
9434 - array(
9435 - 'status' => 'pending',
9436 - 'attempts' => 0,
9437 - 'error_message' => null
9438 - ),
9439 - array(
9440 - 'queue_id' => $queue_id,
9441 - 'status' => 'failed'
9442 - ),
9443 - array('%s', '%d', '%s'),
9444 - array('%s', '%s')
9445 - );
9446 -
9447 - wp_send_json_success(array(
9448 - 'message' => 'Reset ' . $updated . ' failed items for retry',
9449 - 'reset_count' => $updated
9450 - ));
9451 -}
9452 -
9453 -
9454 -public function ajax_mxchat_mark_queue_complete() {
9455 - check_ajax_referer('mxchat_queue_nonce', 'nonce');
9456 -
9457 - if (!current_user_can('manage_options')) {
9458 - wp_send_json_error('Unauthorized access');
9459 - }
9460 -
9461 - $queue_id = isset($_POST['queue_id']) ? sanitize_text_field($_POST['queue_id']) : '';
9462 -
9463 - if (empty($queue_id)) {
9464 - wp_send_json_error('Missing queue ID');
9465 - }
9466 -
9467 - // Clear active queue transients
9468 - if (strpos($queue_id, 'sitemap_') === 0) {
9469 - delete_transient('mxchat_active_queue_sitemap');
9470 - } else if (strpos($queue_id, 'pdf_') === 0) {
9471 - delete_transient('mxchat_active_queue_pdf');
9472 - }
9473 -
9474 - wp_send_json_success(array('message' => 'Queue marked as complete'));
9475 -}
9476 -
9477 -
9478 - // ========================================
9479 - // STATIC ACCESS METHODS
9480 - // ========================================
9481 -
9482 - /**
9483 - * Get singleton instance
9484 - */
9485 - public static function get_instance() {
9486 - static $instance = null;
9487 - if ($instance === null) {
9488 - $instance = new self();
9489 - }
9490 - return $instance;
9491 - }
9492 -}
9493 -
9494 -// 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 +
24 + /**
25 + * Initialize WordPress hooks for content processing
26 + */
27 + private function mxchat_init_hooks() {
28 + // Admin post handlers for form submissions
29 + add_action('admin_post_mxchat_submit_content', array($this, 'mxchat_handle_content_submission'));
30 + add_action('admin_post_mxchat_submit_sitemap', array($this, 'mxchat_handle_sitemap_submission'));
31 + add_action('admin_post_mxchat_stop_processing', array($this, 'mxchat_stop_processing'));
32 +
33 + // AJAX handlers for real-time processing and status updates
34 + add_action('wp_ajax_mxchat_get_status_updates', array($this, 'mxchat_ajax_get_status_updates'));
35 + add_action('wp_ajax_mxchat_dismiss_completed_status', array($this, 'mxchat_ajax_dismiss_completed_status'));
36 + add_action('wp_ajax_mxchat_get_content_list', array($this, 'ajax_mxchat_get_content_list'));
37 + add_action('wp_ajax_mxchat_process_selected_content', array($this, 'ajax_mxchat_process_selected_content'));
38 + add_action('wp_ajax_mxchat_manual_batch_process', array($this, 'ajax_manual_batch_process'));
39 + add_action('mxchat_delete_content', array($this, 'mxchat_delete_from_pinecone_by_url'), 10, 1);
40 +
41 +
42 + // Cron handlers for background processing
43 + add_action('mxchat_process_sitemap_urls', array($this, 'mxchat_process_sitemap_urls_cron'), 10, 5);
44 + add_action('mxchat_process_pdf_pages', array($this, 'mxchat_process_pdf_pages_cron'), 10, 5);
45 +
46 + // WordPress post management hooks - UPDATED FOR BETTER STATUS TRACKING
47 + add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2);
48 + add_action('post_updated', array($this, 'mxchat_handle_post_update'), 10, 3);
49 + add_action('before_delete_post', array($this, 'mxchat_handle_post_delete'));
50 + add_action('wp_trash_post', array($this, 'mxchat_handle_post_delete'));
51 + add_action('wp_ajax_mxchat_save_inline_prompt', array($this, 'mxchat_save_inline_prompt'));
52 + add_action('admin_post_mxchat_delete_pinecone_prompt', array($this, 'mxchat_handle_pinecone_prompt_delete'));
53 + add_action('wp_ajax_mxchat_delete_pinecone_prompt', array($this, 'ajax_mxchat_delete_pinecone_prompt'));
54 + add_action('wp_ajax_mxchat_update_role_restriction', array($this, 'ajax_mxchat_update_role_restriction'));
55 +
56 + // WooCommerce product hooks (if WooCommerce is active)
57 + if (class_exists('WooCommerce')) {
58 + add_action('pre_post_update', array($this, 'mxchat_store_pre_update_status'), 10, 2); // Same hook for products
59 + add_action('save_post_product', array($this, 'mxchat_handle_product_change'), 10, 3);
60 + add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
61 + add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
62 + }
63 +
64 + }
65 +
66 + /**
67 + * Get current options (refreshed)
68 + */
69 + private function mxchat_get_options() {
70 + if (empty($this->options)) {
71 + $this->options = get_option('mxchat_options', array());
72 + }
73 + return $this->options;
74 + }
75 +
76 +public function ajax_manual_batch_process() {
77 + try {
78 + // Verify nonce and permissions
79 + check_ajax_referer('mxchat_status_nonce', 'nonce');
80 +
81 + if (!current_user_can('manage_options')) {
82 + wp_send_json_error('Unauthorized access');
83 + }
84 +
85 + $process_type = sanitize_text_field($_POST['process_type'] ?? '');
86 + $url = sanitize_text_field($_POST['url'] ?? '');
87 +
88 + if (empty($process_type) || empty($url)) {
89 + wp_send_json_error('Missing required parameters');
90 + }
91 +
92 + // Debug logging
93 + //error_log('MANUAL BATCH DEBUG: Process type: ' . $process_type);
94 + //error_log('MANUAL BATCH DEBUG: URL: ' . $url);
95 +
96 + // FIXED: Extract bot_id from stored status instead of POST data
97 + $bot_id = 'default';
98 +
99 + if ($process_type === 'pdf') {
100 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($url));
101 + $status = get_transient($status_key);
102 + //error_log('MANUAL BATCH DEBUG: Status key: ' . $status_key);
103 + //error_log('MANUAL BATCH DEBUG: Status data: ' . print_r($status, true));
104 +
105 + if ($status && isset($status['bot_id'])) {
106 + $bot_id = $status['bot_id'];
107 + //error_log('MANUAL BATCH DEBUG: Bot ID from status: ' . $bot_id);
108 + } else {
109 + //error_log('MANUAL BATCH DEBUG: No bot_id in status, using default');
110 + }
111 + } elseif ($process_type === 'sitemap') {
112 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($url));
113 + $status = get_transient($status_key);
114 + if ($status && isset($status['bot_id'])) {
115 + $bot_id = $status['bot_id'];
116 + }
117 + }
118 +
119 + //error_log('MANUAL BATCH DEBUG: Final bot_id: ' . $bot_id);
120 +
121 + $processed = 0;
122 +
123 + if ($process_type === 'pdf') {
124 + $processed = $this->mxchat_manual_process_pdf_batch($url);
125 + //error_log('MANUAL BATCH DEBUG: PDF processing returned: ' . $processed);
126 + } elseif ($process_type === 'sitemap') {
127 + $processed = $this->mxchat_manual_process_sitemap_batch($url);
128 + }
129 +
130 + if ($processed > 0) {
131 + wp_send_json_success(array(
132 + 'message' => "Processed {$processed} items successfully",
133 + 'processed' => $processed,
134 + 'bot_id' => $bot_id
135 + ));
136 + } else {
137 + // Enhanced error response with debugging info
138 + wp_send_json_error(array(
139 + 'message' => 'No items were processed',
140 + 'debug_info' => array(
141 + 'process_type' => $process_type,
142 + 'url' => $url,
143 + 'bot_id' => $bot_id,
144 + 'status_exists' => !empty($status),
145 + 'status_data' => $status
146 + )
147 + ));
148 + }
149 +
150 + } catch (Exception $e) {
151 + //error_log('MANUAL BATCH DEBUG: Exception: ' . $e->getMessage());
152 + wp_send_json_error('Processing failed: ' . $e->getMessage());
153 + }
154 +}
155 +
156 +private function mxchat_manual_process_pdf_batch($pdf_url) {
157 + try {
158 + //error_log('MANUAL PDF DEBUG: Starting batch processing for: ' . $pdf_url);
159 +
160 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
161 + $status = get_transient($status_key);
162 +
163 + //error_log('MANUAL PDF DEBUG: Status key: ' . $status_key);
164 + //error_log('MANUAL PDF DEBUG: Status data: ' . print_r($status, true));
165 +
166 + if (!$status || $status['status'] !== 'processing') {
167 + //error_log('MANUAL PDF DEBUG: No processing status found or status is not processing');
168 + //error_log('MANUAL PDF DEBUG: Status: ' . ($status ? $status['status'] : 'NULL'));
169 + return 0;
170 + }
171 +
172 + // FIXED: Extract bot_id from status
173 + $bot_id = $status['bot_id'] ?? 'default';
174 + //error_log('MANUAL PDF DEBUG: Bot ID from status: ' . $bot_id);
175 +
176 + // Get current progress
177 + $current_page = $status['processed_pages'] ?? 0;
178 + $total_pages = $status['total_pages'] ?? 0;
179 +
180 + //error_log('MANUAL PDF DEBUG: Current page: ' . $current_page . ', Total pages: ' . $total_pages);
181 +
182 + if ($current_page >= $total_pages) {
183 + //error_log('MANUAL PDF DEBUG: Already completed');
184 + return 0;
185 + }
186 +
187 + // Try to download the PDF again for processing
188 + //error_log('MANUAL PDF DEBUG: Attempting to download PDF');
189 + $response = wp_remote_get($pdf_url, array('timeout' => 30));
190 +
191 + if (is_wp_error($response)) {
192 + //error_log('MANUAL PDF DEBUG: Failed to download PDF: ' . $response->get_error_message());
193 + return 0;
194 + }
195 +
196 + $pdf_content = wp_remote_retrieve_body($response);
197 + if (empty($pdf_content)) {
198 + //error_log('MANUAL PDF DEBUG: Empty PDF content');
199 + return 0;
200 + }
201 +
202 + //error_log('MANUAL PDF DEBUG: PDF content size: ' . strlen($pdf_content) . ' bytes');
203 +
204 + // Save PDF temporarily
205 + $upload_dir = wp_upload_dir();
206 + $temp_pdf_path = trailingslashit($upload_dir['path']) . 'temp_manual_' . time() . '.pdf';
207 + file_put_contents($temp_pdf_path, $pdf_content);
208 +
209 + //error_log('MANUAL PDF DEBUG: Temp PDF saved to: ' . $temp_pdf_path);
210 +
211 + // Process 5 pages directly with bot_id
212 + $processed = $this->mxchat_process_pdf_pages_direct($temp_pdf_path, $pdf_url, $current_page, 5, $bot_id);
213 +
214 + //error_log('MANUAL PDF DEBUG: Direct processing returned: ' . $processed);
215 +
216 + // Clean up temp file
217 + if (file_exists($temp_pdf_path)) {
218 + wp_delete_file($temp_pdf_path);
219 + //error_log('MANUAL PDF DEBUG: Cleaned up temp file');
220 + }
221 +
222 + return $processed;
223 +
224 + } catch (Exception $e) {
225 + //error_log('MANUAL PDF DEBUG: Exception in manual batch: ' . $e->getMessage());
226 + return 0;
227 + }
228 +}
229 +
230 +/**
231 + * Process PDF pages directly without cron
232 + */
233 +private function mxchat_process_pdf_pages_direct($pdf_path, $pdf_url, $start_page, $batch_size, $bot_id = 'default') {
234 + try {
235 + if (!file_exists($pdf_path)) {
236 + //error_log('Direct PDF: File not found at ' . $pdf_path);
237 + return 0;
238 + }
239 +
240 + $parser = new \Smalot\PdfParser\Parser();
241 + $pdf = $parser->parseFile($pdf_path);
242 + $pages = $pdf->getPages();
243 +
244 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
245 + $status = get_transient($status_key);
246 +
247 + if (!$status) {
248 + return 0;
249 + }
250 +
251 + // UPDATED: Get bot-specific options
252 + $bot_options = $this->get_bot_options($bot_id);
253 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
254 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
255 +
256 + if (strpos($selected_model, 'voyage') === 0) {
257 + $api_key = $options['voyage_api_key'] ?? '';
258 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
259 + $api_key = $options['gemini_api_key'] ?? '';
260 + } else {
261 + $api_key = $options['api_key'] ?? '';
262 + }
263 +
264 + if (empty($api_key)) {
265 + //error_log('Direct PDF: No API key for bot: ' . $bot_id);
266 + return 0;
267 + }
268 +
269 + $processed = 0;
270 + $end_page = min($start_page + $batch_size, count($pages));
271 +
272 + for ($i = $start_page; $i < $end_page; $i++) {
273 + try {
274 + $page_number = $i + 1;
275 + $text = $pages[$i]->getText();
276 +
277 + if (empty($text)) {
278 + //error_log('Direct PDF: Empty text on page ' . $page_number);
279 + continue;
280 + }
281 +
282 + $sanitized_content = $this->mxchat_sanitize_content_for_api($text);
283 + if (empty($sanitized_content)) {
284 + //error_log('Direct PDF: No content after sanitization on page ' . $page_number);
285 + continue;
286 + }
287 +
288 + // UPDATED: Use bot-specific embedding generation
289 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content, $bot_id);
290 + if (is_string($embedding_vector)) {
291 + //error_log('Direct PDF: Embedding failed on page ' . $page_number . ': ' . $embedding_vector);
292 + continue;
293 + }
294 +
295 + // Create metadata
296 + $metadata = array(
297 + 'document_type' => 'pdf',
298 + 'total_pages' => count($pages),
299 + 'current_page' => $page_number,
300 + 'source_url' => $pdf_url
301 + );
302 +
303 + $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized_content;
304 + $page_url = esc_url($pdf_url . "#page=" . $page_number);
305 +
306 + // UPDATED: Pass bot_id to database submission
307 + $db_result = MxChat_Utils::submit_content_to_db($content_with_metadata, $page_url, $api_key, null, $bot_id);
308 +
309 + if (is_wp_error($db_result)) {
310 + //error_log('Direct PDF: DB error on page ' . $page_number . ': ' . $db_result->get_error_message());
311 + continue;
312 + }
313 +
314 + $processed++;
315 + //error_log('Direct PDF: Successfully processed page ' . $page_number . ' for bot: ' . $bot_id);
316 +
317 + // Update status
318 + $status['processed_pages'] = $i + 1;
319 + $status['last_update'] = time();
320 + $status['percentage'] = round(($status['processed_pages'] / $status['total_pages']) * 100);
321 + set_transient($status_key, $status, DAY_IN_SECONDS);
322 +
323 + } catch (Exception $e) {
324 + //error_log('Direct PDF: Error processing page ' . ($i + 1) . ': ' . $e->getMessage());
325 + continue;
326 + }
327 + }
328 +
329 + // Check if completed
330 + if ($status['processed_pages'] >= $status['total_pages']) {
331 + $status['status'] = 'complete';
332 + set_transient($status_key, $status, DAY_IN_SECONDS);
333 + //error_log('Direct PDF: Processing completed for bot: ' . $bot_id);
334 + }
335 +
336 + return $processed;
337 +
338 + } catch (Exception $e) {
339 + //error_log('Direct PDF processing error: ' . $e->getMessage());
340 + return 0;
341 + }
342 +}
343 +
344 +
345 +/**
346 + * Process a small sitemap batch manually - DIRECT PROCESSING
347 + */
348 +private function mxchat_manual_process_sitemap_batch($sitemap_url) {
349 + try {
350 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
351 + $status = get_transient($status_key);
352 +
353 + if (!$status || $status['status'] !== 'processing') {
354 + return 0;
355 + }
356 +
357 + // FIXED: Extract bot_id from status
358 + $bot_id = $status['bot_id'] ?? 'default';
359 +
360 + //error_log('Manual Sitemap: Starting direct processing for ' . $sitemap_url . ' with bot: ' . $bot_id);
361 +
362 + // Re-fetch the sitemap to get URLs
363 + $response = wp_remote_get($sitemap_url, array('timeout' => 30));
364 + if (is_wp_error($response)) {
365 + //error_log('Manual Sitemap: Failed to fetch sitemap');
366 + return 0;
367 + }
368 +
369 + $sitemap_content = wp_remote_retrieve_body($response);
370 + $xml = simplexml_load_string($sitemap_content);
371 +
372 + if (!$xml) {
373 + //error_log('Manual Sitemap: Invalid XML');
374 + return 0;
375 + }
376 +
377 + $urls = array();
378 + foreach ($xml->url as $url_element) {
379 + $urls[] = (string)$url_element->loc;
380 + }
381 +
382 + $current_processed = $status['processed_urls'] ?? 0;
383 + $batch_size = 50;
384 + $processed = 0;
385 +
386 + // Process next batch of URLs
387 + for ($i = $current_processed; $i < min($current_processed + $batch_size, count($urls)); $i++) {
388 + $url = $urls[$i];
389 +
390 + // UPDATED: Pass bot_id to single URL processing
391 + if ($this->mxchat_process_single_url_direct($url, $bot_id)) {
392 + $processed++;
393 + }
394 +
395 + // Update status
396 + $status['processed_urls'] = $i + 1;
397 + $status['last_update'] = time();
398 + $status['percentage'] = round(($status['processed_urls'] / $status['total_urls']) * 100);
399 + set_transient($status_key, $status, DAY_IN_SECONDS);
400 + }
401 +
402 + // Check if completed
403 + if ($status['processed_urls'] >= $status['total_urls']) {
404 + $status['status'] = 'complete';
405 + set_transient($status_key, $status, DAY_IN_SECONDS);
406 + }
407 +
408 + //error_log('Manual Sitemap: Processed ' . $processed . ' URLs for bot: ' . $bot_id);
409 + return $processed;
410 +
411 + } catch (Exception $e) {
412 + //error_log('Manual sitemap batch error: ' . $e->getMessage());
413 + return 0;
414 + }
415 +}
416 +
417 +
418 +/**
419 + * Process a single URL directly
420 + */
421 +private function mxchat_process_single_url_direct($url, $bot_id = 'default') {
422 + try {
423 + $response = wp_remote_get($url, array('timeout' => 30));
424 + if (is_wp_error($response)) {
425 + //error_log('Single URL processing failed for ' . $url . ': ' . $response->get_error_message());
426 + return false;
427 + }
428 +
429 + $html = wp_remote_retrieve_body($response);
430 + $content = $this->mxchat_extract_main_content($html);
431 + $sanitized = $this->mxchat_sanitize_content_for_api($content);
432 +
433 + if (empty($sanitized)) {
434 + //error_log('Single URL processing: No content found for ' . $url);
435 + return false;
436 + }
437 +
438 + // UPDATED: Get bot-specific options and API key
439 + $bot_options = $this->get_bot_options($bot_id);
440 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
441 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
442 +
443 + if (strpos($selected_model, 'voyage') === 0) {
444 + $api_key = $options['voyage_api_key'] ?? '';
445 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
446 + $api_key = $options['gemini_api_key'] ?? '';
447 + } else {
448 + $api_key = $options['api_key'] ?? '';
449 + }
450 +
451 + if (empty($api_key)) {
452 + //error_log('Single URL processing: No API key configured for bot: ' . $bot_id);
453 + return false;
454 + }
455 +
456 + // UPDATED: Pass bot_id to database submission
457 + $result = MxChat_Utils::submit_content_to_db($sanitized, $url, $api_key, null, $bot_id);
458 +
459 + $success = !is_wp_error($result);
460 +
461 + if ($success) {
462 + //error_log('Single URL processing: Successfully processed ' . $url . ' for bot: ' . $bot_id);
463 + } else {
464 + //error_log('Single URL processing: Failed to store ' . $url . ' for bot: ' . $bot_id . ': ' . $result->get_error_message());
465 + }
466 +
467 + return $success;
468 +
469 + } catch (Exception $e) {
470 + //error_log('Single URL processing error: ' . $e->getMessage());
471 + return false;
472 + }
473 +}
474 +
475 + // ========================================
476 + // MAIN CONTENT SUBMISSION HANDLERS
477 + // ========================================
478 +
479 +public function mxchat_handle_content_submission() {
480 + // Check if the form was submitted and the user has permission.
481 + if (!isset($_POST['submit_content']) || !current_user_can('manage_options')) {
482 + return;
483 + }
484 +
485 + // Verify the nonce.
486 + $nonce = isset($_POST['mxchat_submit_content_nonce']) ? sanitize_text_field(wp_unslash($_POST['mxchat_submit_content_nonce'])) : '';
487 + if (!wp_verify_nonce($nonce, 'mxchat_submit_content_action')) {
488 + wp_die(esc_html__('Nonce verification failed.', 'mxchat'));
489 + }
490 +
491 + // Sanitize the inputs.
492 + $article_content = sanitize_textarea_field($_POST['article_content']);
493 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
494 +
495 + // UPDATED: Get bot_id from form submission
496 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
497 +
498 + // UPDATED: Get bot-specific options and API key
499 + $bot_options = $this->get_bot_options($bot_id);
500 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
501 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
502 +
503 + if (strpos($selected_model, 'voyage') === 0) {
504 + $api_key = $options['voyage_api_key'] ?? '';
505 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
506 + $api_key = $options['gemini_api_key'] ?? '';
507 + } else {
508 + $api_key = $options['api_key'] ?? '';
509 + }
510 +
511 + if (empty($api_key)) {
512 + set_transient('mxchat_admin_notice_error',
513 + esc_html__('API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
514 + 30
515 + );
516 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
517 + exit;
518 + }
519 +
520 + // UPDATED: Use centralized utility function with bot_id
521 + $result = MxChat_Utils::submit_content_to_db($article_content, $article_url, $api_key, null, $bot_id);
522 +
523 + if (is_wp_error($result)) {
524 + set_transient('mxchat_admin_notice_error',
525 + esc_html__('Error storing content: ', 'mxchat') . $result->get_error_message(),
526 + 30
527 + );
528 + } else {
529 + set_transient('mxchat_admin_notice_success',
530 + esc_html__('Content successfully submitted!', 'mxchat'),
531 + 30
532 + );
533 + }
534 +
535 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
536 + exit;
537 +}
538 +
539 +public function mxchat_is_pdf_url($url, $response) {
540 + $content_type = wp_remote_retrieve_header($response, 'content-type');
541 + $file_extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
542 +
543 + return strpos($content_type, 'pdf') !== false || $file_extension === 'pdf';
544 +}
545 +
546 +
547 +public function mxchat_handle_pdf_for_knowledge_base($pdf_url, $response, $bot_id = 'default') {
548 + if (!current_user_can('manage_options')) {
549 + //error_log('[PDF DEBUG] Unauthorized PDF processing attempt');
550 + return false;
551 + }
552 +
553 + //error_log('[PDF DEBUG] Starting PDF processing for bot: ' . $bot_id);
554 + //error_log('[PDF DEBUG] PDF URL: ' . $pdf_url);
555 +
556 + $pdf_url = esc_url_raw($pdf_url);
557 + $upload_dir = wp_upload_dir();
558 +
559 + if (isset($upload_dir['error']) && $upload_dir['error'] !== false) {
560 + //error_log('[PDF DEBUG] Upload directory error: ' . $upload_dir['error']);
561 + return false;
562 + }
563 +
564 + $pdf_filename = sanitize_file_name('mxchat_kb_' . time() . '.pdf');
565 + $pdf_path = trailingslashit($upload_dir['path']) . $pdf_filename;
566 +
567 + $response_body = wp_remote_retrieve_body($response);
568 + if (empty($response_body)) {
569 + //error_log('[PDF DEBUG] Empty PDF response body');
570 + return false;
571 + }
572 +
573 + if (!wp_mkdir_p(dirname($pdf_path))) {
574 + //error_log('[PDF DEBUG] Failed to create directory for PDF: ' . $pdf_path);
575 + return false;
576 + }
577 +
578 + try {
579 + file_put_contents($pdf_path, $response_body);
580 +
581 + if (!file_exists($pdf_path)) {
582 + throw new Exception(__('Failed to save PDF file', 'mxchat'));
583 + }
584 +
585 + $total_pages = $this->mxchat_validate_and_count_pdf_pages($pdf_path);
586 +
587 + if ($total_pages === false || $total_pages < 1) {
588 + throw new Exception(__('Invalid PDF: Unable to parse or no pages found', 'mxchat'));
589 + }
590 +
591 + //error_log('[PDF DEBUG] PDF validated successfully with ' . $total_pages . ' pages');
592 +
593 + // UPDATED: Pass bot_id to PDF processing cron
594 + wp_schedule_single_event(time(), 'mxchat_process_pdf_pages', array(
595 + $pdf_path, // Position 0
596 + $pdf_url, // Position 1
597 + $total_pages, // Position 2
598 + absint(15), // Position 3 (batch_size)
599 + absint(10), // Position 4 (batch_pause)
600 + $bot_id // Position 5 (bot_id)
601 + ));
602 +
603 + //error_log('[PDF DEBUG] Cron job scheduled with bot_id: ' . $bot_id);
604 +
605 + // UPDATED: Store bot_id in status data
606 + $status_data = array(
607 + 'total_pages' => $total_pages,
608 + 'processed_pages' => 0,
609 + 'status' => 'processing',
610 + 'last_update' => time(),
611 + 'bot_id' => $bot_id // CRITICAL: Store the bot_id
612 + );
613 +
614 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
615 + set_transient($status_key, $status_data, DAY_IN_SECONDS);
616 +
617 + //error_log('[PDF DEBUG] Status stored with bot_id: ' . $bot_id . ' using key: ' . $status_key);
618 +
619 + return __('scheduled', 'mxchat');
620 +
621 + } catch (Exception $e) {
622 + //error_log('[PDF DEBUG] Error preparing PDF for processing: ' . $e->getMessage());
623 + if (file_exists($pdf_path)) {
624 + wp_delete_file($pdf_path);
625 + }
626 + return false;
627 + }
628 +}
629 +
630 +/**
631 + * NEW: Validate PDF and count pages with multiple parser attempts
632 + */
633 +private function mxchat_validate_and_count_pdf_pages($pdf_path) {
634 + // Method 1: Try with Smalot PDF Parser (your current method)
635 + try {
636 + $parser = new \Smalot\PdfParser\Parser();
637 + $pdf = $parser->parseFile($pdf_path);
638 + $pages = $pdf->getPages();
639 + $page_count = count($pages);
640 +
641 + if ($page_count > 0) {
642 + //error_log('PDF parsed successfully with Smalot parser: ' . $page_count . ' pages');
643 + return $page_count;
644 + }
645 + } catch (Exception $e) {
646 + //error_log('Smalot PDF parser failed: ' . $e->getMessage());
647 + }
648 +
649 + // Method 2: Try with pdfinfo command (if available)
650 + if (function_exists('shell_exec') && !$this->mxchat_is_shell_disabled()) {
651 + try {
652 + $command = 'pdfinfo ' . escapeshellarg($pdf_path) . ' 2>&1';
653 + $output = shell_exec($command);
654 +
655 + if ($output && preg_match('/Pages:\s*(\d+)/', $output, $matches)) {
656 + $page_count = intval($matches[1]);
657 + if ($page_count > 0) {
658 + //error_log('PDF parsed successfully with pdfinfo: ' . $page_count . ' pages');
659 + return $page_count;
660 + }
661 + }
662 + } catch (Exception $e) {
663 + //error_log('pdfinfo command failed: ' . $e->getMessage());
664 + }
665 + }
666 +
667 + // Method 3: Try to repair PDF and parse again
668 + try {
669 + $repaired_path = $this->mxchat_attempt_pdf_repair($pdf_path);
670 + if ($repaired_path && $repaired_path !== $pdf_path) {
671 + $parser = new \Smalot\PdfParser\Parser();
672 + $pdf = $parser->parseFile($repaired_path);
673 + $pages = $pdf->getPages();
674 + $page_count = count($pages);
675 +
676 + if ($page_count > 0) {
677 + // Replace original with repaired version
678 + copy($repaired_path, $pdf_path);
679 + unlink($repaired_path);
680 + //error_log('PDF repaired and parsed successfully: ' . $page_count . ' pages');
681 + return $page_count;
682 + }
683 +
684 + // Clean up repaired file if it didn't work
685 + unlink($repaired_path);
686 + }
687 + } catch (Exception $e) {
688 + //error_log('PDF repair attempt failed: ' . $e->getMessage());
689 + }
690 +
691 + // Method 4: Manual PDF structure analysis (basic page count)
692 + try {
693 + $page_count = $this->mxchat_manual_pdf_page_count($pdf_path);
694 + if ($page_count > 0) {
695 + //error_log('PDF page count determined manually: ' . $page_count . ' pages');
696 + return $page_count;
697 + }
698 + } catch (Exception $e) {
699 + //error_log('Manual PDF analysis failed: ' . $e->getMessage());
700 + }
701 +
702 + //error_log('All PDF parsing methods failed for: ' . $pdf_path);
703 + return false;
704 +}
705 +
706 +/**
707 + * NEW: Check if shell_exec is disabled
708 + */
709 +private function mxchat_is_shell_disabled() {
710 + $disabled = explode(',', ini_get('disable_functions'));
711 + return in_array('shell_exec', $disabled);
712 +}
713 +
714 +/**
715 + * NEW: Attempt to repair PDF using basic methods
716 + */
717 +private function mxchat_attempt_pdf_repair($pdf_path) {
718 + try {
719 + $content = file_get_contents($pdf_path);
720 + if (!$content) {
721 + return false;
722 + }
723 +
724 + // Check if PDF starts with proper header
725 + if (substr($content, 0, 4) !== '%PDF') {
726 + // Try to find PDF header in the content
727 + $header_pos = strpos($content, '%PDF');
728 + if ($header_pos !== false && $header_pos < 1024) {
729 + // Remove junk before PDF header
730 + $content = substr($content, $header_pos);
731 + $repaired_path = $pdf_path . '.repaired';
732 + file_put_contents($repaired_path, $content);
733 + return $repaired_path;
734 + }
735 + }
736 +
737 + // Check for EOF marker
738 + $content = rtrim($content);
739 + if (!preg_match('/%%EOF\s*$/', $content)) {
740 + // Add EOF marker if missing
741 + $content .= "\n%%EOF";
742 + $repaired_path = $pdf_path . '.repaired';
743 + file_put_contents($repaired_path, $content);
744 + return $repaired_path;
745 + }
746 +
747 + } catch (Exception $e) {
748 + //error_log('PDF repair error: ' . $e->getMessage());
749 + }
750 +
751 + return false;
752 +}
753 +
754 +/**
755 + * NEW: Manual PDF page counting by analyzing PDF structure
756 + */
757 +private function mxchat_manual_pdf_page_count($pdf_path) {
758 + try {
759 + $content = file_get_contents($pdf_path);
760 + if (!$content) {
761 + return 0;
762 + }
763 +
764 + // Method 1: Count /Type /Page objects
765 + $page_count = preg_match_all('/\/Type\s*\/Page[^s]/', $content);
766 + if ($page_count > 0) {
767 + return $page_count;
768 + }
769 +
770 + // Method 2: Look for /Count in pages object
771 + if (preg_match('/\/Type\s*\/Pages.*?\/Count\s+(\d+)/', $content, $matches)) {
772 + return intval($matches[1]);
773 + }
774 +
775 + // Method 3: Count page references
776 + $page_count = preg_match_all('/\d+\s+0\s+obj\s*<<[^>]*\/Type\s*\/Page/', $content);
777 + if ($page_count > 0) {
778 + return $page_count;
779 + }
780 +
781 + } catch (Exception $e) {
782 + //error_log('Manual PDF analysis error: ' . $e->getMessage());
783 + }
784 +
785 + return 0;
786 +}
787 +
788 +public function mxchat_process_pdf_pages_cron($pdf_path, $pdf_url, $total_pages, $batch_size, $batch_pause, $bot_id = 'default') {
789 + // ADD THIS DEBUG SECTION AT THE VERY BEGINNING
790 + //error_log('[PDF CRON DEBUG] ===== PDF Cron Job Started =====');
791 + //error_log('[PDF CRON DEBUG] Initial bot_id parameter: ' . $bot_id);
792 + //error_log('[PDF CRON DEBUG] Received parameters:');
793 + //error_log('[PDF CRON DEBUG] - pdf_path: ' . $pdf_path);
794 + //error_log('[PDF CRON DEBUG] - pdf_url: ' . $pdf_url);
795 + //error_log('[PDF CRON DEBUG] - total_pages: ' . $total_pages);
796 + //error_log('[PDF CRON DEBUG] - batch_size: ' . $batch_size);
797 + //error_log('[PDF CRON DEBUG] - batch_pause: ' . $batch_pause);
798 + //error_log('[PDF CRON DEBUG] - Total args received: ' . func_num_args());
799 + //error_log('[PDF CRON DEBUG] - All args: ' . print_r(func_get_args(), true));
800 +
801 + // FIXED: Get the correct bot_id from stored status instead of relying on cron parameters
802 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
803 + $status = get_transient($status_key);
804 +
805 + if ($status && isset($status['bot_id'])) {
806 + $bot_id = $status['bot_id'];
807 + //error_log('[PDF CRON DEBUG] Using bot_id from status: ' . $bot_id);
808 + } else {
809 + //error_log('[PDF CRON DEBUG] No bot_id in status, using default: ' . $bot_id);
810 + }
811 +
812 + // Validate inputs
813 + $pdf_path = sanitize_text_field($pdf_path);
814 + $pdf_url = esc_url_raw($pdf_url);
815 + $total_pages = absint($total_pages);
816 + $batch_size = absint($batch_size);
817 + $batch_pause = absint($batch_pause);
818 + $bot_id = sanitize_key($bot_id);
819 +
820 + try {
821 + if (!file_exists($pdf_path)) {
822 + throw new Exception(sprintf('PDF file not found at path: %s', esc_html($pdf_path)));
823 + }
824 +
825 + // Try to parse PDF with error recovery
826 + $pdf = null;
827 + $pages = null;
828 +
829 + try {
830 + $parser = new \Smalot\PdfParser\Parser();
831 + $pdf = $parser->parseFile($pdf_path);
832 + $pages = $pdf->getPages();
833 + } catch (Exception $e) {
834 + //error_log('Primary PDF parsing failed, attempting recovery: ' . $e->getMessage());
835 +
836 + $repaired_path = $this->mxchat_attempt_pdf_repair($pdf_path);
837 + if ($repaired_path) {
838 + try {
839 + $parser = new \Smalot\PdfParser\Parser();
840 + $pdf = $parser->parseFile($repaired_path);
841 + $pages = $pdf->getPages();
842 +
843 + copy($repaired_path, $pdf_path);
844 + unlink($repaired_path);
845 + //error_log('PDF successfully repaired and parsed');
846 + } catch (Exception $e2) {
847 + if (file_exists($repaired_path)) {
848 + unlink($repaired_path);
849 + }
850 + throw new Exception('PDF parsing failed even after repair attempt: ' . $e2->getMessage());
851 + }
852 + } else {
853 + throw new Exception('PDF parsing failed and repair was unsuccessful: ' . $e->getMessage());
854 + }
855 + }
856 +
857 + if (!$pages || count($pages) === 0) {
858 + throw new Exception('No pages found in PDF after parsing');
859 + }
860 +
861 + // Get current progress (already fetched above for bot_id)
862 + if (!$status || !is_array($status)) {
863 + throw new Exception('Invalid status data retrieved from transient');
864 + }
865 +
866 + // Initialize failed pages list if it doesn't exist
867 + if (!isset($status['failed_pages_list']) || !is_array($status['failed_pages_list'])) {
868 + $status['failed_pages_list'] = [];
869 + }
870 +
871 + $start_page = absint($status['processed_pages']);
872 + $end_page = min($start_page + $batch_size, $total_pages);
873 +
874 + // UPDATED: Get bot-specific options using the correct bot_id
875 + $bot_options = $this->get_bot_options($bot_id);
876 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
877 +
878 + //error_log('[PDF CRON DEBUG] Using bot options for bot: ' . $bot_id);
879 +
880 + if (empty($options['api_key'])) {
881 + throw new Exception('API key is missing or invalid for bot: ' . $bot_id);
882 + }
883 +
884 + $successful_pages = 0;
885 + $failed_pages = 0;
886 +
887 + for ($i = $start_page; $i < $end_page; $i++) {
888 + $page_number = $i + 1;
889 + $max_retries = 3;
890 + $retry_count = 0;
891 + $page_processed = false;
892 + $last_error = '';
893 +
894 + while (!$page_processed && $retry_count < $max_retries) {
895 + try {
896 + $text = $pages[$i]->getText();
897 +
898 + if (empty($text)) {
899 + throw new Exception("Empty text on page {$page_number}");
900 + }
901 +
902 + $sanitized_content = $this->mxchat_sanitize_content_for_api($text);
903 +
904 + if (empty($sanitized_content)) {
905 + throw new Exception("No valid content after sanitization on page {$page_number}");
906 + }
907 +
908 + // UPDATED: Use bot-specific embedding generation with correct bot_id
909 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content, $bot_id);
910 +
911 + if (is_string($embedding_vector)) {
912 + throw new Exception("Embedding generation failed: " . $embedding_vector);
913 + }
914 +
915 + if (!is_array($embedding_vector)) {
916 + throw new Exception("Embedding generation returned unexpected result type: " . gettype($embedding_vector));
917 + }
918 +
919 + $metadata = array(
920 + 'document_type' => 'pdf',
921 + 'total_pages' => $total_pages,
922 + 'current_page' => $page_number,
923 + 'prev_page' => $i > 0 ? $i : null,
924 + 'next_page' => $i < ($total_pages - 1) ? $i + 2 : null,
925 + 'source_url' => $pdf_url
926 + );
927 +
928 + $content_with_metadata = wp_json_encode($metadata) . "\n---\n" . $sanitized_content;
929 + $page_url = esc_url($pdf_url . "#page=" . $page_number);
930 +
931 + // UPDATED: Pass correct bot_id to database submission
932 + $db_result = MxChat_Utils::submit_content_to_db($content_with_metadata, $page_url, $options['api_key'], null, $bot_id);
933 +
934 + if (is_wp_error($db_result)) {
935 + throw new Exception("Database submission failed: " . $db_result->get_error_message());
936 + }
937 +
938 + // Success!
939 + $page_processed = true;
940 + $successful_pages++;
941 + //error_log('[PDF CRON DEBUG] Successfully processed page ' . $page_number . ' for bot: ' . $bot_id);
942 +
943 + } catch (Exception $e) {
944 + $retry_count++;
945 + $last_error = $e->getMessage();
946 +
947 + //error_log("PDF page {$page_number} failed (attempt {$retry_count}/{$max_retries}): " . $last_error);
948 +
949 + if ($retry_count < $max_retries) {
950 + sleep(pow(2, $retry_count - 1));
951 + }
952 + }
953 + }
954 +
955 + // If page still not processed after all retries, mark as failed
956 + if (!$page_processed) {
957 + $failed_pages++;
958 + $status['failed_pages_list'][] = [
959 + 'page' => $page_number,
960 + 'error' => $last_error,
961 + 'time' => time(),
962 + 'retries' => $max_retries
963 + ];
964 +
965 + if (count($status['failed_pages_list']) > 50) {
966 + $status['failed_pages_list'] = array_slice($status['failed_pages_list'], -50);
967 + }
968 +
969 + //error_log("PDF page {$page_number} permanently failed after {$max_retries} attempts: " . $last_error);
970 + }
971 +
972 + // Update progress
973 + $status['processed_pages'] = absint($page_number);
974 + $status['last_update'] = time();
975 + $status['failed_pages'] = absint($status['failed_pages'] ?? 0) + ($page_processed ? 0 : 1);
976 +
977 + set_transient($status_key, $status, DAY_IN_SECONDS);
978 + }
979 +
980 + // Schedule next batch if needed
981 + if ($end_page < $total_pages) {
982 + // Use the same indexed array format (though bot_id still won't pass correctly)
983 + wp_schedule_single_event(time() + $batch_pause, 'mxchat_process_pdf_pages', array(
984 + $pdf_path,
985 + $pdf_url,
986 + $total_pages,
987 + $batch_size,
988 + $batch_pause,
989 + $bot_id // This still won't work, but we're now getting bot_id from status
990 + ));
991 + } else {
992 + // Processing complete
993 + $status['status'] = 'complete';
994 + $status['processed_pages'] = $total_pages;
995 +
996 + $status['completion_summary'] = [
997 + 'total_pages' => $total_pages,
998 + 'successful_pages' => $total_pages - absint($status['failed_pages'] ?? 0),
999 + 'failed_pages' => absint($status['failed_pages'] ?? 0),
1000 + 'completion_time' => current_time('mysql')
1001 + ];
1002 +
1003 + set_transient($status_key, $status, DAY_IN_SECONDS);
1004 +
1005 + if (file_exists($pdf_path)) {
1006 + wp_delete_file($pdf_path);
1007 + }
1008 +
1009 + //error_log('[PDF CRON DEBUG] PDF processing completed for bot: ' . $bot_id);
1010 + }
1011 +
1012 + } catch (\Exception $e) {
1013 + //error_log(sprintf('[MXCHAT-PDF] Error processing PDF for bot %s: %s', $bot_id, $e->getMessage()));
1014 +
1015 + $status_key = sanitize_key('mxchat_pdf_status_' . md5($pdf_url));
1016 + $status = get_transient($status_key);
1017 +
1018 + if (!$status || !is_array($status)) {
1019 + $status = array(
1020 + 'total_pages' => $total_pages,
1021 + 'processed_pages' => 0,
1022 + 'status' => 'error',
1023 + 'error' => sanitize_text_field($e->getMessage()),
1024 + 'last_update' => time(),
1025 + 'bot_id' => $bot_id
1026 + );
1027 + } else {
1028 + $status['status'] = 'error';
1029 + $status['error'] = sanitize_text_field($e->getMessage());
1030 + $status['last_update'] = time();
1031 + }
1032 +
1033 + set_transient($status_key, $status, DAY_IN_SECONDS);
1034 +
1035 + if (file_exists($pdf_path)) {
1036 + wp_delete_file($pdf_path);
1037 + }
1038 + }
1039 +}
1040 +
1041 +public function mxchat_save_inline_prompt() {
1042 + // DEBUG: Log what we're receiving
1043 + //error_log('=== MXCHAT DEBUG ===');
1044 + //error_log('POST data: ' . print_r($_POST, true));
1045 + //error_log('Nonce from POST: ' . ($_POST['_ajax_nonce'] ?? 'NOT FOUND'));
1046 +
1047 + // Check for nonce security
1048 + check_ajax_referer('mxchat_save_inline_nonce', '_ajax_nonce');
1049 +
1050 + // If we get here, nonce passed
1051 + //error_log('Nonce verification PASSED');
1052 +
1053 + // Verify permissions
1054 + if (!current_user_can('manage_options')) {
1055 + wp_send_json_error(esc_html__('Permission denied.', 'mxchat'));
1056 + return;
1057 + }
1058 +
1059 + global $wpdb;
1060 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
1061 +
1062 + // Validate and sanitize input data - FIXED LINE BELOW
1063 + $prompt_id = isset($_POST['id']) ? absint($_POST['id']) : 0;
1064 + $article_content = isset($_POST['article_content']) ? sanitize_textarea_field(wp_unslash($_POST['article_content'])) : '';
1065 + $article_url = isset($_POST['article_url']) ? esc_url_raw($_POST['article_url']) : '';
1066 +
1067 + if ($prompt_id > 0 && !empty($article_content)) {
1068 + // Re-generate the embedding vector for the updated content
1069 + $embedding_vector = $this->mxchat_generate_embedding($article_content);
1070 + if (is_array($embedding_vector)) {
1071 + // Serialize the embedding vector before storing it
1072 + $embedding_vector_serialized = serialize($embedding_vector);
1073 + // Update the prompt in the database
1074 + $updated = $wpdb->update(
1075 + $table_name,
1076 + array(
1077 + 'article_content' => $article_content,
1078 + 'embedding_vector' => $embedding_vector_serialized,
1079 + 'source_url' => $article_url,
1080 + ),
1081 + array('id' => $prompt_id),
1082 + array('%s', '%s', '%s'),
1083 + array('%d')
1084 + );
1085 + if ($updated !== false) {
1086 + wp_send_json_success();
1087 + } else {
1088 + wp_send_json_error(esc_html__('Database update failed.', 'mxchat'));
1089 + }
1090 + } else {
1091 + wp_send_json_error(esc_html__('Embedding generation failed.', 'mxchat'));
1092 + }
1093 + } else {
1094 + wp_send_json_error(esc_html__('Invalid data.', 'mxchat'));
1095 + }
1096 +}
1097 +
1098 +
1099 +public function mxchat_get_pdf_processing_status($pdf_url) {
1100 + $pdf_url = esc_url_raw($pdf_url);
1101 + $status = get_transient(sanitize_key('mxchat_pdf_status_' . md5($pdf_url)));
1102 +
1103 + if (!$status || !is_array($status)) {
1104 + return false;
1105 + }
1106 +
1107 + // Check for stalled processing (no updates for 5 minutes)
1108 + if ($status['status'] === 'processing' && (time() - absint($status['last_update'])) > 300) {
1109 + $status['status'] = 'error';
1110 + $status['error'] = __('PDF processing appears to be stalled. No updates for over 5 minutes.', 'mxchat');
1111 +
1112 + // Save the updated status
1113 + set_transient(
1114 + sanitize_key('mxchat_pdf_status_' . md5($pdf_url)),
1115 + array_map('sanitize_text_field', $status),
1116 + DAY_IN_SECONDS
1117 + );
1118 + }
1119 +
1120 + $result = array(
1121 + 'total_pages' => absint($status['total_pages']),
1122 + 'processed_pages' => absint($status['processed_pages']),
1123 + 'failed_pages' => absint($status['failed_pages'] ?? 0),
1124 + 'percentage' => ($status['total_pages'] > 0)
1125 + ? round((absint($status['processed_pages']) / absint($status['total_pages'])) * 100)
1126 + : 0,
1127 + 'status' => sanitize_text_field($status['status']),
1128 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
1129 + 'failed_pages_list' => isset($status['failed_pages_list']) ? $status['failed_pages_list'] : array(),
1130 + 'completion_summary' => isset($status['completion_summary']) ? $status['completion_summary'] : null
1131 + );
1132 +
1133 + // Add error message if present
1134 + if (isset($status['error']) && !empty($status['error'])) {
1135 + $result['error'] = sanitize_text_field($status['error']);
1136 + }
1137 +
1138 + return $result;
1139 +}
1140 +
1141 +
1142 +public function mxchat_handle_sitemap_submission() {
1143 + // START DEBUG
1144 + //error_log('[SITEMAP DEBUG] ===== Starting URL submission process =====');
1145 + //error_log('[SITEMAP DEBUG] POST data: ' . print_r($_POST, true));
1146 +
1147 + // Get bot_id from form submission EARLY for debugging
1148 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1149 + //error_log('[SITEMAP DEBUG] Extracted bot_id: ' . $bot_id);
1150 + //error_log('[SITEMAP DEBUG] Class exists MxChat_Multi_Bot_Manager: ' . (class_exists('MxChat_Multi_Bot_Manager') ? 'YES' : 'NO'));
1151 + // END DEBUG
1152 +
1153 + // Check if the form was submitted and verify permissions
1154 + if (!isset($_POST['submit_sitemap']) || !current_user_can('manage_options')) {
1155 + //error_log('[SITEMAP DEBUG] Error: Unauthorized access or form not submitted properly');
1156 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
1157 + }
1158 +
1159 + // Verify nonce
1160 + //error_log('[SITEMAP DEBUG] Verifying 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 + //error_log('[SITEMAP DEBUG] Error: Empty or missing URL');
1166 + set_transient('mxchat_admin_notice_error',
1167 + esc_html__('Please provide a valid URL.', 'mxchat'),
1168 + 30
1169 + );
1170 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1171 + exit;
1172 + }
1173 +
1174 + $submitted_url = esc_url_raw($_POST['sitemap_url']);
1175 +
1176 + // Continue processing with already extracted bot_id
1177 + //error_log('[SITEMAP DEBUG] Processing URL: ' . $submitted_url . ' for bot: ' . $bot_id);
1178 +
1179 + // UPDATED: Get bot-specific options and validate API key
1180 + $bot_options = $this->get_bot_options($bot_id);
1181 + //error_log('[SITEMAP DEBUG] Bot options retrieved: ' . print_r($bot_options, true));
1182 +
1183 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1184 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
1185 +
1186 + //error_log('[SITEMAP DEBUG] Selected embedding model: ' . $selected_model);
1187 +
1188 + if (strpos($selected_model, 'voyage') === 0) {
1189 + $api_key = $options['voyage_api_key'] ?? '';
1190 + $provider_name = 'Voyage AI';
1191 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
1192 + $api_key = $options['gemini_api_key'] ?? '';
1193 + $provider_name = 'Google Gemini';
1194 + } else {
1195 + $api_key = $options['api_key'] ?? '';
1196 + $provider_name = 'OpenAI';
1197 + }
1198 +
1199 + //error_log('[SITEMAP DEBUG] Provider: ' . $provider_name . ', Has API key: ' . (!empty($api_key) ? 'YES' : 'NO'));
1200 +
1201 + if (empty($api_key)) {
1202 + $error_message = sprintf(
1203 + esc_html__('%s API key is not configured. Please add your API key in the settings before submitting content.', 'mxchat'),
1204 + $provider_name
1205 + );
1206 + //error_log('[SITEMAP DEBUG] Error: ' . $error_message);
1207 + set_transient('mxchat_admin_notice_error', $error_message, 30);
1208 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1209 + exit;
1210 + }
1211 +
1212 + //error_log('[SITEMAP DEBUG] Fetching URL content');
1213 + $response = wp_remote_get($submitted_url, array('timeout' => 30));
1214 +
1215 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1216 + $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP Status: ' . wp_remote_retrieve_response_code($response);
1217 + //error_log('[SITEMAP DEBUG] Error fetching URL: ' . $error_message);
1218 + set_transient('mxchat_admin_notice_error',
1219 + sprintf(
1220 + esc_html__('Failed to fetch the URL: %s', 'mxchat'),
1221 + esc_html($error_message)
1222 + ),
1223 + 30
1224 + );
1225 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1226 + exit;
1227 + }
1228 +
1229 + $content_type = wp_remote_retrieve_header($response, 'content-type');
1230 + //error_log('[SITEMAP DEBUG] Content type: ' . $content_type);
1231 + $body_content = wp_remote_retrieve_body($response);
1232 +
1233 + if (empty($body_content)) {
1234 + //error_log('[SITEMAP DEBUG] Error: Empty response body');
1235 + set_transient('mxchat_admin_notice_error',
1236 + esc_html__('Empty response received from URL.', 'mxchat'),
1237 + 30
1238 + );
1239 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1240 + exit;
1241 + }
1242 + //error_log('[SITEMAP DEBUG] Retrieved body content length: ' . strlen($body_content) . ' bytes');
1243 +
1244 + // Handle PDF URL
1245 + if ($this->mxchat_is_pdf_url($submitted_url, $response)) {
1246 + //error_log('[SITEMAP DEBUG] Detected PDF URL, handling PDF for knowledge base');
1247 + //error_log('[SITEMAP DEBUG] About to call PDF handler with bot_id: ' . $bot_id);
1248 +
1249 + // UPDATED: Pass bot_id to PDF handler
1250 + $result = $this->mxchat_handle_pdf_for_knowledge_base($submitted_url, $response, $bot_id);
1251 + //error_log('[SITEMAP DEBUG] PDF handling result: ' . $result);
1252 +
1253 + if ($result === 'scheduled') {
1254 + set_transient(
1255 + 'mxchat_last_pdf_url',
1256 + sanitize_text_field($submitted_url),
1257 + DAY_IN_SECONDS
1258 + );
1259 + // UPDATED: Store bot_id for PDF processing
1260 + set_transient(
1261 + 'mxchat_last_pdf_bot_id',
1262 + $bot_id,
1263 + DAY_IN_SECONDS
1264 + );
1265 + //error_log('[SITEMAP DEBUG] PDF processing scheduled successfully for bot: ' . $bot_id);
1266 + set_transient('mxchat_admin_notice_info',
1267 + esc_html__('PDF processing has started in the background. You can check the progress in the Knowledge Base section.', 'mxchat'),
1268 + 30
1269 + );
1270 + } else {
1271 + //error_log('[SITEMAP DEBUG] PDF processing failed: ' . $result);
1272 + set_transient('mxchat_admin_notice_error',
1273 + esc_html__('Failed to start PDF processing: ', 'mxchat') . esc_html($result),
1274 + 30
1275 + );
1276 + }
1277 +
1278 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1279 + exit;
1280 + }
1281 +
1282 + // Handle Sitemap XML
1283 + if (strpos($content_type, 'xml') !== false || strpos($body_content, '<urlset') !== false) {
1284 + //error_log('[SITEMAP DEBUG] Detected XML content, processing as sitemap');
1285 + libxml_use_internal_errors(true);
1286 + $xml = simplexml_load_string($body_content);
1287 + $xml_errors = libxml_get_errors();
1288 + libxml_clear_errors();
1289 +
1290 + if ($xml === false || !empty($xml_errors)) {
1291 + //error_log('[SITEMAP DEBUG] Error: Invalid XML format');
1292 + if (!empty($xml_errors)) {
1293 + foreach ($xml_errors as $error) {
1294 + //error_log('[SITEMAP DEBUG] XML Error: ' . $error->message);
1295 + }
1296 + }
1297 +
1298 + set_transient('mxchat_admin_notice_error',
1299 + esc_html__('Invalid sitemap XML. Please provide a valid sitemap.', 'mxchat'),
1300 + 30
1301 + );
1302 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1303 + exit;
1304 + }
1305 +
1306 + //error_log('[SITEMAP DEBUG] Valid XML found, handling sitemap for knowledge base');
1307 + // UPDATED: Pass bot_id to sitemap handler
1308 + $result = $this->mxchat_handle_sitemap_for_knowledge_base($xml, $submitted_url, $bot_id);
1309 + //error_log('[SITEMAP DEBUG] Sitemap handling result: ' . $result);
1310 +
1311 + if ($result === 'scheduled') {
1312 + set_transient(
1313 + 'mxchat_last_sitemap_url',
1314 + sanitize_text_field($submitted_url),
1315 + DAY_IN_SECONDS
1316 + );
1317 + // UPDATED: Store bot_id for sitemap processing
1318 + set_transient(
1319 + 'mxchat_last_sitemap_bot_id',
1320 + $bot_id,
1321 + DAY_IN_SECONDS
1322 + );
1323 + set_transient('mxchat_admin_notice_info',
1324 + esc_html__('Sitemap processing has started in the background. You can check the progress in the Knowledge Base section.', 'mxchat'),
1325 + 30
1326 + );
1327 + } else {
1328 + set_transient('mxchat_admin_notice_error',
1329 + esc_html__('Failed to start sitemap processing. Please check the status below for details.', 'mxchat'),
1330 + 30
1331 + );
1332 + }
1333 +
1334 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1335 + exit;
1336 + }
1337 +
1338 + // Handle Regular URL
1339 + //error_log('[SITEMAP DEBUG] Processing as regular webpage');
1340 + $page_content = $this->mxchat_extract_main_content($body_content);
1341 + //error_log('[SITEMAP DEBUG] Extracted content length: ' . strlen($page_content) . ' bytes');
1342 +
1343 + $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
1344 + //error_log('[SITEMAP DEBUG] Sanitized content length: ' . strlen($sanitized_content) . ' bytes');
1345 +
1346 + if (empty($sanitized_content)) {
1347 + //error_log('[SITEMAP DEBUG] Error: No valid content after sanitization');
1348 +
1349 + set_transient('mxchat_admin_notice_error',
1350 + esc_html__('No valid content found on the provided URL.', 'mxchat'),
1351 + 30
1352 + );
1353 +
1354 + set_transient('mxchat_single_url_status', [
1355 + 'url' => $submitted_url,
1356 + 'timestamp' => current_time('mysql'),
1357 + 'status' => 'failed',
1358 + 'error' => esc_html__('No valid content found on the provided URL.', 'mxchat')
1359 + ], DAY_IN_SECONDS);
1360 +
1361 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1362 + exit;
1363 + }
1364 +
1365 + //error_log('[SITEMAP DEBUG] Generating embedding for content');
1366 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content, $bot_id);
1367 +
1368 + if (is_string($embedding_vector)) {
1369 + //error_log('[SITEMAP DEBUG] Error generating embedding: ' . $embedding_vector);
1370 + $error_message = esc_html__('Failed to generate embedding: ', 'mxchat') . esc_html($embedding_vector);
1371 +
1372 + set_transient('mxchat_admin_notice_error', $error_message, 30);
1373 +
1374 + set_transient('mxchat_single_url_status', [
1375 + 'url' => $submitted_url,
1376 + 'timestamp' => current_time('mysql'),
1377 + 'status' => 'failed',
1378 + 'error' => $error_message
1379 + ], DAY_IN_SECONDS);
1380 +
1381 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1382 + exit;
1383 + }
1384 +
1385 + if (is_array($embedding_vector)) {
1386 + //error_log('[SITEMAP DEBUG] Successfully generated embedding with ' . count($embedding_vector) . ' dimensions');
1387 +
1388 + // UPDATED: Pass bot_id to database submission
1389 + $db_result = MxChat_Utils::submit_content_to_db(
1390 + $sanitized_content,
1391 + $submitted_url,
1392 + $api_key,
1393 + null,
1394 + $bot_id
1395 + );
1396 +
1397 + if (is_wp_error($db_result)) {
1398 + //error_log('[SITEMAP DEBUG] Error: Failed to store content in database: ' . $db_result->get_error_message());
1399 + $error_message = esc_html__('Failed to store content in database: ', 'mxchat') . esc_html($db_result->get_error_message());
1400 +
1401 + set_transient('mxchat_admin_notice_error', $error_message, 30);
1402 +
1403 + set_transient('mxchat_single_url_status', [
1404 + 'url' => $submitted_url,
1405 + 'timestamp' => current_time('mysql'),
1406 + 'status' => 'failed',
1407 + 'error' => $error_message
1408 + ], DAY_IN_SECONDS);
1409 +
1410 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1411 + exit;
1412 + }
1413 +
1414 + //error_log('[SITEMAP DEBUG] Successfully stored content in database');
1415 + $success_message = esc_html__('URL content successfully submitted!', 'mxchat');
1416 +
1417 + set_transient('mxchat_admin_notice_success', $success_message, 30);
1418 +
1419 + set_transient('mxchat_single_url_status', [
1420 + 'url' => $submitted_url,
1421 + 'timestamp' => current_time('mysql'),
1422 + 'status' => 'complete',
1423 + 'content_length' => strlen($sanitized_content),
1424 + 'embedding_dimensions' => count($embedding_vector)
1425 + ], DAY_IN_SECONDS);
1426 +
1427 + } else {
1428 + //error_log('[SITEMAP DEBUG] Error: Failed to generate embedding. Unexpected result type: ' . gettype($embedding_vector));
1429 + $error_message = esc_html__('Failed to generate embedding: Unexpected result type. Please check your API key and try again.', 'mxchat');
1430 +
1431 + set_transient('mxchat_admin_notice_error', $error_message, 30);
1432 +
1433 + set_transient('mxchat_single_url_status', [
1434 + 'url' => $submitted_url,
1435 + 'timestamp' => current_time('mysql'),
1436 + 'status' => 'failed',
1437 + 'error' => $error_message
1438 + ], DAY_IN_SECONDS);
1439 + }
1440 +
1441 + //error_log('[SITEMAP DEBUG] ===== Completed URL submission process =====');
1442 + wp_safe_redirect(esc_url(admin_url('admin.php?page=mxchat-prompts')));
1443 + exit;
1444 +}
1445 +
1446 +public function mxchat_get_single_url_status() {
1447 + $status = get_transient('mxchat_single_url_status');
1448 + if (!$status) {
1449 + return null;
1450 + }
1451 +
1452 + // Add human-readable time
1453 + if (isset($status['timestamp'])) {
1454 + $status['human_time'] = human_time_diff(strtotime($status['timestamp']), current_time('timestamp')) . ' ' . __('ago', 'mxchat');
1455 + }
1456 +
1457 + return $status;
1458 +}
1459 +
1460 +public function mxchat_handle_sitemap_for_knowledge_base($xml, $sitemap_url, $bot_id = 'default') {
1461 + delete_transient('mxchat_single_url_status');
1462 + if (!current_user_can('manage_options')) {
1463 + //error_log(esc_html__('Unauthorized sitemap processing attempt', 'mxchat'));
1464 + return false;
1465 + }
1466 +
1467 + try {
1468 + $sitemap_url = esc_url_raw($sitemap_url);
1469 +
1470 + if (!$xml || !is_object($xml)) {
1471 + throw new Exception(__('Invalid XML object provided', 'mxchat'));
1472 + }
1473 +
1474 + // UPDATED: Get bot-specific embedding API for validation
1475 + $bot_options = $this->get_bot_options($bot_id);
1476 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1477 +
1478 + // ADD THIS: Test the embedding API before processing
1479 + $test_phrase = "Test embedding generation for MxChat";
1480 + $test_result = $this->mxchat_generate_embedding($test_phrase, $bot_id);
1481 +
1482 + if (is_string($test_result)) {
1483 + //error_log('[MXCHAT-URL] Embedding API validation failed: ' . $test_result);
1484 +
1485 + $status_data = array(
1486 + 'total_urls' => 0,
1487 + 'processed_urls' => 0,
1488 + 'status' => 'error',
1489 + 'error' => __('Embedding API validation failed: ', 'mxchat') . $test_result,
1490 + 'last_update' => time(),
1491 + 'bot_id' => $bot_id // ADDED
1492 + );
1493 +
1494 + set_transient(
1495 + sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url)),
1496 + array_map('sanitize_text_field', $status_data),
1497 + DAY_IN_SECONDS
1498 + );
1499 +
1500 + throw new Exception(__('Embedding API validation failed: ', 'mxchat') . $test_result);
1501 + }
1502 +
1503 + if (!is_array($test_result)) {
1504 + //error_log('[MXCHAT-URL] Embedding API returned unexpected result type: ' . gettype($test_result));
1505 + throw new Exception(__('Embedding API returned unexpected result type. Please check your configuration.', 'mxchat'));
1506 + }
1507 +
1508 + $urls = [];
1509 + foreach ($xml->url as $url_element) {
1510 + $url = esc_url_raw((string)$url_element->loc);
1511 + if ($url) {
1512 + $urls[] = $url;
1513 + }
1514 + }
1515 +
1516 + $total_urls = absint(count($urls));
1517 +
1518 + if ($total_urls < 1) {
1519 + throw new Exception(__('No valid URLs found in sitemap', 'mxchat'));
1520 + }
1521 +
1522 + // UPDATED: Pass bot_id to sitemap processing cron
1523 + wp_schedule_single_event(time(), 'mxchat_process_sitemap_urls', array(
1524 + 'urls' => $urls,
1525 + 'sitemap_url' => $sitemap_url,
1526 + 'total_urls' => $total_urls,
1527 + 'batch_size' => absint(10),
1528 + 'batch_pause' => absint(5),
1529 + 'bot_id' => $bot_id // ADDED
1530 + ));
1531 +
1532 + $status_data = array(
1533 + 'total_urls' => $total_urls,
1534 + 'processed_urls' => 0,
1535 + 'status' => 'processing',
1536 + 'last_update' => time(),
1537 + 'bot_id' => $bot_id // ADDED
1538 + );
1539 +
1540 + set_transient(
1541 + sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url)),
1542 + array_map('sanitize_text_field', $status_data),
1543 + DAY_IN_SECONDS
1544 + );
1545 +
1546 + return __('scheduled', 'mxchat');
1547 +
1548 + } catch (\Exception $e) {
1549 + $error_message = $e->getMessage();
1550 + //error_log(sprintf(esc_html__('Error preparing sitemap for processing: %s', 'mxchat'), esc_html($error_message)));
1551 +
1552 + set_transient(
1553 + 'mxchat_last_sitemap_url',
1554 + sanitize_text_field($sitemap_url),
1555 + DAY_IN_SECONDS
1556 + );
1557 +
1558 + $status_data = array(
1559 + 'total_urls' => 0,
1560 + 'processed_urls' => 0,
1561 + 'status' => 'error',
1562 + 'error' => $error_message,
1563 + 'last_update' => time(),
1564 + 'bot_id' => $bot_id // ADDED
1565 + );
1566 +
1567 + set_transient(
1568 + sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url)),
1569 + array_map('sanitize_text_field', $status_data),
1570 + DAY_IN_SECONDS
1571 + );
1572 +
1573 + return $error_message;
1574 + }
1575 +}
1576 +public function mxchat_process_sitemap_urls_cron($urls, $sitemap_url, $total_urls, $batch_size, $batch_pause, $bot_id = 'default') {
1577 + // Validate inputs
1578 + $sitemap_url = esc_url_raw($sitemap_url);
1579 + $total_urls = absint($total_urls);
1580 + $batch_size = absint($batch_size);
1581 + $batch_pause = absint($batch_pause);
1582 + $bot_id = sanitize_key($bot_id);
1583 +
1584 + if (!is_array($urls) || empty($urls)) {
1585 + return;
1586 + }
1587 +
1588 + try {
1589 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
1590 + $status = get_transient($status_key);
1591 +
1592 + if (!$status || !is_array($status)) {
1593 + throw new Exception('Invalid status data retrieved from transient');
1594 + }
1595 +
1596 + // Initialize failed_urls array if it doesn't exist
1597 + if (!isset($status['failed_urls_list']) || !is_array($status['failed_urls_list'])) {
1598 + $status['failed_urls_list'] = [];
1599 + }
1600 +
1601 + $start_url = absint($status['processed_urls']);
1602 + $end_url = min($start_url + $batch_size, $total_urls);
1603 +
1604 + // UPDATED: Get bot-specific options
1605 + $bot_options = $this->get_bot_options($bot_id);
1606 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
1607 +
1608 + // Track batch statistics
1609 + $batch_stats = [
1610 + 'processed' => 0,
1611 + 'failed' => 0,
1612 + 'last_error' => '',
1613 + 'embedding_errors' => 0,
1614 + 'network_errors' => 0,
1615 + 'timeout_errors' => 0
1616 + ];
1617 +
1618 + @set_time_limit(300);
1619 +
1620 + for ($i = $start_url; $i < $end_url; $i++) {
1621 + $page_url = esc_url_raw($urls[$i]);
1622 + $max_retries = 5;
1623 + $retry_count = 0;
1624 + $url_processed = false;
1625 + $last_error = '';
1626 +
1627 + if (memory_get_usage(true) > (1024 * 1024 * 100)) {
1628 + //error_log('MxChat: Memory usage high, taking break');
1629 + sleep(2);
1630 + }
1631 +
1632 + while (!$url_processed && $retry_count < $max_retries) {
1633 + try {
1634 + $timeout = 30 + ($retry_count * 10);
1635 +
1636 + $page_response = wp_remote_get($page_url, array(
1637 + 'timeout' => $timeout,
1638 + 'redirection' => 5,
1639 + 'user-agent' => 'MxChat/1.0'
1640 + ));
1641 +
1642 + if (is_wp_error($page_response)) {
1643 + $error_msg = $page_response->get_error_message();
1644 +
1645 + if (strpos($error_msg, 'timeout') !== false) {
1646 + $batch_stats['timeout_errors']++;
1647 + } else {
1648 + $batch_stats['network_errors']++;
1649 + }
1650 +
1651 + throw new Exception('HTTP request failed: ' . $error_msg);
1652 + }
1653 +
1654 + $response_code = wp_remote_retrieve_response_code($page_response);
1655 +
1656 + if (!in_array($response_code, [200, 201, 202])) {
1657 + if ($response_code >= 400 && $response_code < 500) {
1658 + throw new Exception('HTTP Status: ' . $response_code . ' (permanent failure)');
1659 + }
1660 + throw new Exception('HTTP Status: ' . $response_code);
1661 + }
1662 +
1663 + $page_html = wp_remote_retrieve_body($page_response);
1664 +
1665 + if (empty($page_html)) {
1666 + throw new Exception('Empty response body');
1667 + }
1668 +
1669 + $page_content = $this->mxchat_extract_main_content($page_html);
1670 + $sanitized_content = $this->mxchat_sanitize_content_for_api($page_content);
1671 +
1672 + if (empty($sanitized_content)) {
1673 + //error_log("MxChat: No content found for URL: {$page_url}");
1674 + $url_processed = true;
1675 + $batch_stats['processed']++;
1676 + break;
1677 + }
1678 +
1679 + // UPDATED: Use bot-specific embedding generation
1680 + $embedding_vector = $this->mxchat_generate_embedding($sanitized_content, $bot_id);
1681 +
1682 + if (is_string($embedding_vector)) {
1683 + $batch_stats['embedding_errors']++;
1684 +
1685 + if (strpos($embedding_vector, 'rate limit') !== false ||
1686 + strpos($embedding_vector, 'quota') !== false) {
1687 + sleep(30 + ($retry_count * 10));
1688 + }
1689 +
1690 + throw new Exception('Embedding generation failed: ' . $embedding_vector);
1691 + }
1692 +
1693 + if (!is_array($embedding_vector)) {
1694 + throw new Exception('Embedding generation returned unexpected result type: ' . gettype($embedding_vector));
1695 + }
1696 +
1697 + // UPDATED: Pass bot_id to database submission
1698 + $submission_result = MxChat_Utils::submit_content_to_db($sanitized_content, $page_url, $options['api_key'], null, $bot_id);
1699 +
1700 + if (is_wp_error($submission_result)) {
1701 + throw new Exception('Database submission failed: ' . $submission_result->get_error_message());
1702 + }
1703 +
1704 + // Success!
1705 + $url_processed = true;
1706 + $batch_stats['processed']++;
1707 +
1708 + } catch (Exception $e) {
1709 + $retry_count++;
1710 + $last_error = $e->getMessage();
1711 +
1712 + if (strpos($last_error, 'rate limit') !== false) {
1713 + sleep(60);
1714 + } elseif (strpos($last_error, 'timeout') !== false) {
1715 + sleep(10);
1716 + } elseif (strpos($last_error, 'permanent failure') !== false) {
1717 + break;
1718 + } else {
1719 + sleep(pow(2, $retry_count - 1));
1720 + }
1721 + }
1722 + }
1723 +
1724 + // If URL still not processed after all retries, mark as failed
1725 + if (!$url_processed) {
1726 + $batch_stats['failed']++;
1727 + $batch_stats['last_error'] = $last_error;
1728 +
1729 + $status['failed_urls_list'][] = [
1730 + 'url' => $page_url,
1731 + 'error' => $last_error,
1732 + 'time' => time(),
1733 + 'retries' => $max_retries
1734 + ];
1735 +
1736 + if (count($status['failed_urls_list']) > 100) {
1737 + $status['failed_urls_list'] = array_slice($status['failed_urls_list'], -100);
1738 + }
1739 + }
1740 +
1741 + // Update progress after each URL
1742 + $status['processed_urls'] = absint($i + 1);
1743 + $status['last_update'] = time();
1744 + $status['failed_urls'] = absint($status['failed_urls'] ?? 0) + ($url_processed ? 0 : 1);
1745 + $status['last_error'] = $batch_stats['last_error'];
1746 +
1747 + set_transient($status_key, $status, DAY_IN_SECONDS);
1748 + }
1749 +
1750 + $failure_rate = $batch_stats['failed'] / max(1, $batch_stats['processed'] + $batch_stats['failed']);
1751 +
1752 + if ($batch_stats['processed'] === 0 && $batch_stats['failed'] >= 5) {
1753 + $status['status'] = 'error';
1754 + $status['error'] = sprintf(
1755 + 'Processing stopped after %d consecutive failures. Last error: %s',
1756 + $batch_stats['failed'],
1757 + $batch_stats['last_error']
1758 + );
1759 + set_transient($status_key, $status, DAY_IN_SECONDS);
1760 + return;
1761 + }
1762 +
1763 + // Update final progress
1764 + $status['processed_urls'] = min($end_url, $total_urls);
1765 + $status['last_update'] = time();
1766 + $status['batch_stats'] = $batch_stats;
1767 + set_transient($status_key, $status, DAY_IN_SECONDS);
1768 +
1769 + // Check if we've processed all URLs
1770 + if ($end_url >= $total_urls) {
1771 + // All URLs have been processed - mark as complete
1772 + $status['status'] = 'complete';
1773 + $status['processed_urls'] = $total_urls;
1774 +
1775 + $status['completion_summary'] = [
1776 + 'total_urls' => $total_urls,
1777 + 'successful_urls' => $total_urls - absint($status['failed_urls'] ?? 0),
1778 + 'failed_urls' => absint($status['failed_urls'] ?? 0),
1779 + 'completion_time' => current_time('mysql'),
1780 + 'final_batch_stats' => $batch_stats
1781 + ];
1782 +
1783 + set_transient($status_key, $status, DAY_IN_SECONDS);
1784 + } else {
1785 + $dynamic_pause = $batch_pause;
1786 +
1787 + if ($failure_rate > 0.5) {
1788 + $dynamic_pause *= 3;
1789 + } elseif ($batch_stats['embedding_errors'] > 3) {
1790 + $dynamic_pause *= 2;
1791 + }
1792 +
1793 + // UPDATED: Pass bot_id to next batch
1794 + wp_schedule_single_event(time() + $dynamic_pause, 'mxchat_process_sitemap_urls', array(
1795 + 'urls' => $urls,
1796 + 'sitemap_url' => $sitemap_url,
1797 + 'total_urls' => $total_urls,
1798 + 'batch_size' => $batch_size,
1799 + 'batch_pause' => $batch_pause,
1800 + 'bot_id' => $bot_id // ADDED
1801 + ));
1802 + }
1803 + } catch (\Exception $e) {
1804 + $status['last_error'] = $e->getMessage();
1805 + $status['error_count'] = ($status['error_count'] ?? 0) + 1;
1806 +
1807 + if ($status['error_count'] >= 5) {
1808 + $status['status'] = 'error';
1809 + $status['error'] = 'Too many batch failures: ' . $e->getMessage();
1810 + } else {
1811 + // UPDATED: Pass bot_id to retry batch
1812 + wp_schedule_single_event(time() + 300, 'mxchat_process_sitemap_urls', array(
1813 + 'urls' => $urls,
1814 + 'sitemap_url' => $sitemap_url,
1815 + 'total_urls' => $total_urls,
1816 + 'batch_size' => max(5, $batch_size / 2),
1817 + 'batch_pause' => $batch_pause * 2,
1818 + 'bot_id' => $bot_id // ADDED
1819 + ));
1820 + }
1821 +
1822 + set_transient($status_key, $status, DAY_IN_SECONDS);
1823 + }
1824 +}
1825 +
1826 +
1827 +public function mxchat_sanitize_content_for_api($content) {
1828 + //error_log('[MXCHAT-SANITIZE] Original content preview: ' . substr($content, 0, 500) . '...');
1829 +
1830 + // Remove script, style tags, and HTML comments
1831 + $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
1832 + $content = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $content);
1833 + $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
1834 +
1835 + // Remove all HTML tags and decode HTML entities
1836 + $content = wp_strip_all_tags($content);
1837 + $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
1838 +
1839 + // Normalize whitespace but preserve paragraph breaks
1840 + // First, normalize line endings to \n
1841 + $content = str_replace(["\r\n", "\r"], "\n", $content);
1842 + // Replace multiple spaces/tabs with single space, but preserve newlines
1843 + $content = preg_replace('/[ \t]+/', ' ', $content);
1844 + // Replace 3+ newlines with 2 newlines (max 2 blank lines)
1845 + $content = preg_replace('/\n{3,}/', "\n\n", $content);
1846 + // Trim each line
1847 + $lines = explode("\n", $content);
1848 + $lines = array_map('trim', $lines);
1849 + $content = implode("\n", $lines);
1850 + // Final trim
1851 + $content = trim($content);
1852 +
1853 + // Remove control characters (which can cause database issues) but preserve \n (0x0A) and \r (0x0D)
1854 + $content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $content);
1855 +
1856 + // Remove NULL bytes which can cause database errors
1857 + $content = str_replace("\0", "", $content);
1858 +
1859 + // Ensure valid UTF-8 encoding
1860 + $content = wp_check_invalid_utf8($content);
1861 +
1862 + // Remove any extremely long strings without spaces (often garbage)
1863 + $content = preg_replace('/\S{300,}/', ' ', $content);
1864 +
1865 + // Replace problematic characters that often cause database issues
1866 + $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content); // Remove emoji and other high Unicode characters
1867 +
1868 + // Replace any remaining potentially problematic characters with spaces
1869 + // BUT preserve newlines by temporarily replacing them
1870 + $content = str_replace("\n", "NEWLINE_PLACEHOLDER", $content);
1871 + $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Z}\p{Sm}]/u', ' ', $content);
1872 + $content = str_replace("NEWLINE_PLACEHOLDER", "\n", $content);
1873 +
1874 + // Limit to reasonable length if needed
1875 + $max_length = 65000; // Just under MySQL TEXT field limit
1876 + if (strlen($content) > $max_length) {
1877 + $content = substr($content, 0, $max_length);
1878 + }
1879 +
1880 + //error_log('[MXCHAT-SANITIZE] Sanitized content preview: ' . substr($content, 0, 500) . '...');
1881 + return $content;
1882 +}
1883 +public function mxchat_extract_main_content($html) {
1884 + if (empty($html)) {
1885 + return '';
1886 + }
1887 + try {
1888 + $dom = new DOMDocument;
1889 + libxml_use_internal_errors(true); // Suppress HTML parsing errors
1890 + @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
1891 + $xpath = new DOMXPath($dom);
1892 +
1893 + // For debugging purposes
1894 + $debugEnabled = false; // Set to true to enable debugging output
1895 + $debug = function($message) use ($debugEnabled) {
1896 + if ($debugEnabled) {
1897 + //error_log('[MXCHAT-DEBUG] ' . $message);
1898 + }
1899 + };
1900 +
1901 + // Direct targeting for Gerow theme posts
1902 + $post_text = $xpath->query('//div[contains(@class, "post-text")]');
1903 + if ($post_text && $post_text->length > 0) {
1904 + $debug("Found post-text directly");
1905 + $content = '';
1906 + foreach ($post_text as $node) {
1907 + $content .= $dom->saveHTML($node);
1908 + }
1909 + if (!empty($content)) {
1910 + $debug("Returning post-text content");
1911 + return $content;
1912 + }
1913 + }
1914 +
1915 + // Try to get the blog details content which contains the post-text
1916 + $blog_details = $xpath->query('//div[contains(@class, "blog-details-content")]');
1917 + if ($blog_details && $blog_details->length > 0) {
1918 + $debug("Found blog-details-content");
1919 + $content = '';
1920 + foreach ($blog_details as $node) {
1921 + $content .= $dom->saveHTML($node);
1922 + }
1923 + if (!empty($content)) {
1924 + $debug("Returning blog-details-content");
1925 + return $content;
1926 + }
1927 + }
1928 +
1929 + // Try to get the article which contains the blog details
1930 + $article = $xpath->query('//article[contains(@class, "blog-details-wrap")]');
1931 + if ($article && $article->length > 0) {
1932 + $debug("Found article with blog-details-wrap");
1933 + $content = '';
1934 + foreach ($article as $node) {
1935 + $content .= $dom->saveHTML($node);
1936 + }
1937 + if (!empty($content)) {
1938 + $debug("Returning article content");
1939 + return $content;
1940 + }
1941 + }
1942 +
1943 + // Try even broader with the blog-item-wrap
1944 + $blog_item = $xpath->query('//div[contains(@class, "blog-item-wrap")]');
1945 + if ($blog_item && $blog_item->length > 0) {
1946 + $debug("Found blog-item-wrap");
1947 + $content = '';
1948 + foreach ($blog_item as $node) {
1949 + $content .= $dom->saveHTML($node);
1950 + }
1951 + if (!empty($content)) {
1952 + $debug("Returning blog-item-wrap content");
1953 + return $content;
1954 + }
1955 + }
1956 +
1957 + // Specific Gerow theme path
1958 + $gerow_path = $xpath->query('//section[contains(@class, "blog-area")]//div[contains(@class, "post-text")]');
1959 + if ($gerow_path && $gerow_path->length > 0) {
1960 + $debug("Found Gerow theme path to post-text");
1961 + $content = '';
1962 + foreach ($gerow_path as $node) {
1963 + $content .= $dom->saveHTML($node);
1964 + }
1965 + if (!empty($content)) {
1966 + $debug("Returning Gerow post-text content");
1967 + return $content;
1968 + }
1969 + }
1970 +
1971 + // Generic blog post selectors
1972 + $selectors = [
1973 + // Blog post specific selectors
1974 + '//div[contains(@class, "post-text")]',
1975 + '//article[contains(@class, "blog-post-item")]//div[contains(@class, "post-text")]',
1976 + '//div[contains(@class, "blog-details-content")]',
1977 + '//article[contains(@class, "blog-details-wrap")]',
1978 + '//div[contains(@class, "entry-content")]',
1979 + '//div[contains(@class, "blog-content")]',
1980 + '//div[contains(@class, "blog-item-wrap")]',
1981 +
1982 + // More general content selectors
1983 + '//div[contains(@class, "page__content")]',
1984 + '//div[contains(@class, "elementor-widget-container")]',
1985 + '//div[contains(@class, "elementor-text-editor")]',
1986 + '//div[contains(@class, "elementor-widget-text-editor")]',
1987 + '//*[contains(@class, "entry-content")]',
1988 + '//*[contains(@class, "post-content")]',
1989 + '//*[contains(@class, "article-content")]',
1990 + '//*[@id="content"]',
1991 + '//*[@id="main-content"]',
1992 + '//section[contains(@class, "blog-area")]',
1993 + '//article',
1994 + '//main',
1995 + '//div[contains(@class, "content")]'
1996 + ];
1997 +
1998 + // First handle Elementor content
1999 + $debug("Checking for Elementor content");
2000 + $elementor_widgets = $xpath->query('//div[contains(@class, "elementor-element")]//div[contains(@class, "elementor-widget-container")]');
2001 + if ($elementor_widgets && $elementor_widgets->length > 0) {
2002 + $debug("Found Elementor widgets");
2003 + $combined_content = '';
2004 + foreach ($elementor_widgets as $widget) {
2005 + $widget_content = $dom->saveHTML($widget);
2006 + if (!empty($widget_content)) {
2007 + $combined_content .= $widget_content;
2008 + }
2009 + }
2010 + if (!empty($combined_content)) {
2011 + $debug("Returning Elementor content");
2012 + return $combined_content;
2013 + }
2014 + }
2015 +
2016 + // Try standard selectors one by one
2017 + foreach ($selectors as $selector) {
2018 + $debug("Trying selector: " . $selector);
2019 + $nodes = $xpath->query($selector);
2020 + if ($nodes && $nodes->length > 0) {
2021 + $debug("Found matches for selector: " . $selector);
2022 + $content = '';
2023 + foreach ($nodes as $node) {
2024 + $content .= $dom->saveHTML($node);
2025 + }
2026 + if (!empty($content)) {
2027 + $debug("Returning content from selector: " . $selector);
2028 + return $content;
2029 + }
2030 + }
2031 + }
2032 +
2033 + // Manual regex fallback for post-text if DOM methods fail
2034 + $debug("Trying regex fallback");
2035 + if (preg_match('/<div class="post-text">(.*?)<\/div>\s*<\/div>\s*<\/div>/s', $html, $matches)) {
2036 + $debug("Found post-text via regex");
2037 + return '<div class="post-text">' . $matches[1] . '</div>';
2038 + }
2039 +
2040 + // Try to extract the blog section as a whole
2041 + $blog_section = $xpath->query('//section[contains(@class, "blog-area")]');
2042 + if ($blog_section && $blog_section->length > 0) {
2043 + $debug("Found blog-area section");
2044 + $content = '';
2045 + foreach ($blog_section as $node) {
2046 + $content .= $dom->saveHTML($node);
2047 + }
2048 + if (!empty($content)) {
2049 + $debug("Returning blog-area section content");
2050 + return $content;
2051 + }
2052 + }
2053 +
2054 + // Fallback: Return the body content if no specific selector matches
2055 + $debug("Using body fallback");
2056 + $body = $dom->getElementsByTagName('body');
2057 + if ($body->length > 0) {
2058 + return $dom->saveHTML($body->item(0));
2059 + }
2060 +
2061 + // Last resort: return the original HTML
2062 + $debug("Returning original HTML");
2063 + return $html;
2064 + } catch (Exception $e) {
2065 + //error_log('[MXCHAT-ERROR] Content extraction failed: ' . $e->getMessage());
2066 + return $html; // Return original HTML if parsing fails
2067 + } finally {
2068 + libxml_clear_errors();
2069 + }
2070 +}
2071 +public function mxchat_get_sitemap_processing_status($sitemap_url) {
2072 + $sitemap_url = esc_url_raw($sitemap_url);
2073 + $status_key = sanitize_key('mxchat_sitemap_status_' . md5($sitemap_url));
2074 + $status = get_transient($status_key);
2075 +
2076 + if (!$status || !is_array($status)) {
2077 + return false;
2078 + }
2079 +
2080 + // Auto-complete check: if all URLs are processed but status isn't complete
2081 + if (isset($status['processed_urls']) && isset($status['total_urls']) &&
2082 + $status['processed_urls'] >= $status['total_urls'] &&
2083 + isset($status['status']) && $status['status'] !== 'complete' &&
2084 + $status['status'] !== 'error') {
2085 +
2086 + // Mark as complete
2087 + $status['status'] = 'complete';
2088 + $status['processed_urls'] = $status['total_urls']; // Ensure exact match
2089 +
2090 + // Update the transient with the corrected status
2091 + set_transient($status_key, $status, DAY_IN_SECONDS);
2092 + }
2093 +
2094 + return array(
2095 + 'total_urls' => absint($status['total_urls']),
2096 + 'processed_urls' => absint($status['processed_urls']),
2097 + 'failed_urls' => absint($status['failed_urls'] ?? 0),
2098 + 'percentage' => ($status['total_urls'] > 0)
2099 + ? round((absint($status['processed_urls']) / absint($status['total_urls'])) * 100)
2100 + : 0,
2101 + 'status' => sanitize_text_field($status['status']),
2102 + 'last_update' => human_time_diff(absint($status['last_update']), time()) . ' ' . esc_html__('ago', 'mxchat'),
2103 + 'error' => isset($status['error']) ? sanitize_text_field($status['error']) : '',
2104 + 'last_error' => isset($status['last_error']) ? sanitize_text_field($status['last_error']) : '',
2105 + 'failed_urls_list' => isset($status['failed_urls_list']) ? $status['failed_urls_list'] : array()
2106 + );
2107 +}
2108 +
2109 +public function mxchat_ajax_get_status_updates() {
2110 + try {
2111 + // Verify the request
2112 + check_ajax_referer('mxchat_status_nonce', 'nonce');
2113 +
2114 + // Get the status just like in your admin page
2115 + $pdf_url = get_transient('mxchat_last_pdf_url');
2116 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
2117 +
2118 + $pdf_status = $pdf_url ? $this->mxchat_get_pdf_processing_status($pdf_url) : false;
2119 + $sitemap_status = $sitemap_url ? $this->mxchat_get_sitemap_processing_status($sitemap_url) : false;
2120 +
2121 + // Add the PDF URL to the status object
2122 + if ($pdf_status && $pdf_url) {
2123 + $pdf_status['pdf_url'] = $pdf_url;
2124 + }
2125 +
2126 + // Set the current PDF URL for the manual batch processing button
2127 + $current_pdf_url = $pdf_url;
2128 +
2129 + // Check for true processing status, not just presence of status
2130 + $is_active_processing =
2131 + ($sitemap_status && isset($sitemap_status['status']) && $sitemap_status['status'] === 'processing') ||
2132 + ($pdf_status && isset($pdf_status['status']) && $pdf_status['status'] === 'processing');
2133 +
2134 + // Get single URL status, but only if no processing is active
2135 + $single_url_status = !$is_active_processing ? $this->mxchat_get_single_url_status() : false;
2136 +
2137 + // REMOVED: Auto-clearing of completed status - now only done via dismiss button
2138 +
2139 + // Return JSON response with the status data
2140 + wp_send_json(array(
2141 + 'pdf_status' => $pdf_status,
2142 + 'sitemap_status' => $sitemap_status,
2143 + 'single_url_status' => $single_url_status,
2144 + 'is_processing' => $is_active_processing,
2145 + 'current_pdf_url' => $current_pdf_url
2146 + ));
2147 +
2148 + } catch (Exception $e) {
2149 + // Log the error
2150 + //error_log('MxChat Status Update Error: ' . $e->getMessage());
2151 +
2152 + // Return a friendly error response
2153 + wp_send_json_error(array(
2154 + 'message' => 'Error getting status updates: ' . $e->getMessage(),
2155 + 'status' => 'error'
2156 + ));
2157 + }
2158 +}
2159 +public function mxchat_stop_processing() {
2160 + // Verify permissions
2161 + if (!current_user_can('manage_options')) {
2162 + wp_die(esc_html__('Unauthorized access', 'mxchat'));
2163 + }
2164 +
2165 + // Verify nonce
2166 + check_admin_referer('mxchat_stop_processing_action', 'mxchat_stop_processing_nonce');
2167 +
2168 + // Get the last sitemap URL and clear its transient
2169 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
2170 + if ($sitemap_url) {
2171 + delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
2172 + delete_transient('mxchat_last_sitemap_url');
2173 + }
2174 +
2175 + // Get the last PDF URL and clear its transient
2176 + $pdf_url = get_transient('mxchat_last_pdf_url');
2177 + if ($pdf_url) {
2178 + delete_transient('mxchat_pdf_status_' . md5($pdf_url));
2179 + delete_transient('mxchat_last_pdf_url');
2180 + }
2181 +
2182 + // Unschedule any pending sitemap events
2183 + $timestamp = wp_next_scheduled('mxchat_process_sitemap_urls');
2184 + if ($timestamp) {
2185 + wp_unschedule_event($timestamp, 'mxchat_process_sitemap_urls');
2186 + }
2187 +
2188 + // Redirect back with a success message
2189 + set_transient('mxchat_admin_notice_success',
2190 + esc_html__('Processing has been stopped successfully.', 'mxchat'),
2191 + 30
2192 + );
2193 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
2194 + exit;
2195 +}
2196 +
2197 +
2198 +/**
2199 + * Get content list for processing
2200 + */
2201 +public function ajax_mxchat_get_content_list() {
2202 + // Verify the nonce
2203 + check_ajax_referer('mxchat_content_selector_nonce', 'nonce');
2204 +
2205 + if (!current_user_can('manage_options')) {
2206 + wp_send_json_error(__('Unauthorized access', 'mxchat'));
2207 + }
2208 +
2209 + $page = isset($_GET['page']) ? absint($_GET['page']) : 1;
2210 + $per_page = isset($_GET['per_page']) ? absint($_GET['per_page']) : 50;
2211 + $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
2212 + $post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'all';
2213 + $post_status = isset($_GET['post_status']) ? sanitize_text_field($_GET['post_status']) : 'publish';
2214 + $processed_filter = isset($_GET['processed_filter']) ? sanitize_text_field($_GET['processed_filter']) : 'all';
2215 +
2216 + // Build query args
2217 + $args = array(
2218 + 'posts_per_page' => $per_page,
2219 + 'paged' => $page,
2220 + 'post_status' => $post_status !== 'all' ? $post_status : array('publish', 'draft', 'pending'),
2221 + 'orderby' => 'date',
2222 + 'order' => 'DESC',
2223 + );
2224 +
2225 + // Handle post types - IMPROVED VERSION
2226 + if ($post_type !== 'all') {
2227 + $args['post_type'] = $post_type;
2228 + } else {
2229 + // Get all available post types that might contain content
2230 + $all_post_types = array();
2231 +
2232 + // First get all public post types
2233 + $public_types = get_post_types(array('public' => true), 'names');
2234 + $all_post_types = array_merge($all_post_types, $public_types);
2235 +
2236 + // Add common forum/community post types
2237 + $forum_types = array('topic', 'reply', 'forum', 'wpforo_topic', 'wpforo_post');
2238 + foreach ($forum_types as $forum_type) {
2239 + if (post_type_exists($forum_type)) {
2240 + $all_post_types[] = $forum_type;
2241 + }
2242 + }
2243 +
2244 + // Add other commonly used post types
2245 + $common_types = array('product', 'job_listing', 'event', 'portfolio');
2246 + foreach ($common_types as $common_type) {
2247 + if (post_type_exists($common_type)) {
2248 + $all_post_types[] = $common_type;
2249 + }
2250 + }
2251 +
2252 + // Remove duplicates and ensure we have at least some post types
2253 + $all_post_types = array_unique($all_post_types);
2254 +
2255 + if (empty($all_post_types)) {
2256 + // Fallback to basic post types
2257 + $all_post_types = array('post', 'page');
2258 + }
2259 +
2260 + $args['post_type'] = $all_post_types;
2261 +
2262 + // Debug logging to see what post types are being queried
2263 + //error_log('MxChat Debug: Querying post types: ' . implode(', ', $all_post_types));
2264 + }
2265 +
2266 + if (!empty($search)) {
2267 + $args['s'] = $search;
2268 + }
2269 +
2270 + // Get processed data from storage
2271 + $processed_data = array();
2272 +
2273 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
2274 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
2275 +
2276 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
2277 + // Get fresh data from Pinecone - no caching
2278 + $processed_data = $this->mxchat_get_pinecone_processed_content($pinecone_options);
2279 + } else {
2280 + // WordPress DB checking with better URL matching for all post types
2281 + global $wpdb;
2282 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2283 + $processed_items = $wpdb->get_results("SELECT id, source_url, timestamp FROM {$table_name}");
2284 +
2285 + if (!empty($processed_items)) {
2286 + foreach ($processed_items as $item) {
2287 + // Use improved URL matching that works for all post types
2288 + $post_id = $this->mxchat_url_to_post_id_improved($item->source_url);
2289 +
2290 + if ($post_id) {
2291 + $processed_data[$post_id] = array(
2292 + 'db_id' => $item->id,
2293 + 'timestamp' => $item->timestamp,
2294 + 'url' => $item->source_url,
2295 + 'source' => 'wordpress'
2296 + );
2297 + }
2298 + }
2299 + }
2300 + }
2301 +
2302 + // Get processed IDs as a simple array for in_array checks
2303 + $processed_ids = array_keys($processed_data);
2304 +
2305 + // Handle processed/unprocessed filter
2306 + if ($processed_filter === 'processed' && !empty($processed_ids)) {
2307 + $args['post__in'] = $processed_ids;
2308 + } elseif ($processed_filter === 'unprocessed' && !empty($processed_ids)) {
2309 + $args['post__not_in'] = $processed_ids;
2310 + }
2311 +
2312 + // Run the query
2313 + $query = new WP_Query($args);
2314 + $content_items = array();
2315 +
2316 + if ($query->have_posts()) {
2317 + while ($query->have_posts()) {
2318 + $query->the_post();
2319 + $id = get_the_ID();
2320 + $post_date = get_the_date();
2321 + $excerpt = wp_trim_words(get_the_excerpt(), 20, '...');
2322 + $word_count = str_word_count(strip_tags(get_the_content()));
2323 +
2324 + $is_processed = in_array($id, $processed_ids);
2325 + $processed_date = '';
2326 + $db_record_id = 0;
2327 + $data_source = 'none';
2328 +
2329 + if ($is_processed && isset($processed_data[$id])) {
2330 + $item_data = $processed_data[$id];
2331 + $data_source = $item_data['source'];
2332 +
2333 + if ($data_source === 'wordpress' && isset($item_data['timestamp'])) {
2334 + // WordPress DB format
2335 + $timestamp = strtotime($item_data['timestamp']);
2336 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
2337 + $db_record_id = $item_data['db_id'];
2338 + } elseif ($data_source === 'pinecone') {
2339 + // Pinecone format
2340 + $processed_date = $item_data['processed_date'];
2341 + $db_record_id = $item_data['db_id'];
2342 + }
2343 + }
2344 +
2345 + $content_items[] = array(
2346 + 'id' => $id,
2347 + 'title' => get_the_title(),
2348 + 'permalink' => get_permalink(),
2349 + 'date' => $post_date,
2350 + 'type' => get_post_type(),
2351 + 'status' => get_post_status(),
2352 + 'excerpt' => $excerpt,
2353 + 'word_count' => $word_count,
2354 + 'already_processed' => $is_processed,
2355 + 'processed_date' => $processed_date,
2356 + 'db_record_id' => $db_record_id,
2357 + 'data_source' => $data_source
2358 + );
2359 + }
2360 + wp_reset_postdata();
2361 + }
2362 +
2363 + $response = array(
2364 + 'items' => $content_items,
2365 + 'total' => $query->found_posts,
2366 + 'total_pages' => $query->max_num_pages,
2367 + 'current_page' => $page,
2368 + 'processed_count' => count($processed_ids)
2369 + );
2370 +
2371 + wp_send_json_success($response);
2372 + exit;
2373 +}
2374 +
2375 +
2376 +/**
2377 + * This function handles various WooCommerce URL formats and permalink structures
2378 + */
2379 +private function mxchat_url_to_post_id_improved($url) {
2380 + // First try the standard WordPress function
2381 + $post_id = url_to_postid($url);
2382 +
2383 + if ($post_id > 0) {
2384 + return $post_id;
2385 + }
2386 +
2387 + // If that fails, try more aggressive URL matching
2388 + // Remove trailing slashes and query parameters for better matching
2389 + $clean_url = rtrim($url, '/');
2390 + $clean_url = strtok($clean_url, '?'); // Remove query parameters
2391 +
2392 + // Try again with cleaned URL
2393 + $post_id = url_to_postid($clean_url);
2394 + if ($post_id > 0) {
2395 + return $post_id;
2396 + }
2397 +
2398 + // For bbPress forum topics, try extracting slug from URL
2399 + if (strpos($url, '/topic/') !== false || strpos($url, '/forums/') !== false) {
2400 + // Handle bbPress URLs: /forums/topic/topic-name/
2401 + if (preg_match('/\/forums\/topic\/([^\/\?]+)/', $url, $matches)) {
2402 + $topic_slug = $matches[1];
2403 +
2404 + // Look up topic by slug
2405 + $topic = get_page_by_path($topic_slug, OBJECT, 'topic');
2406 + if ($topic) {
2407 + return $topic->ID;
2408 + }
2409 +
2410 + // Alternative method: query by post_name
2411 + global $wpdb;
2412 + $post_id = $wpdb->get_var($wpdb->prepare(
2413 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
2414 + $topic_slug
2415 + ));
2416 +
2417 + if ($post_id) {
2418 + return intval($post_id);
2419 + }
2420 + }
2421 +
2422 + // Handle simpler topic URLs: /topic/topic-name/
2423 + if (preg_match('/\/topic\/([^\/\?]+)/', $url, $matches)) {
2424 + $topic_slug = $matches[1];
2425 +
2426 + global $wpdb;
2427 + $post_id = $wpdb->get_var($wpdb->prepare(
2428 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'topic' AND post_status IN ('publish', 'closed')",
2429 + $topic_slug
2430 + ));
2431 +
2432 + if ($post_id) {
2433 + return intval($post_id);
2434 + }
2435 + }
2436 + }
2437 +
2438 + // For WooCommerce products
2439 + if (strpos($url, '/product/') !== false || strpos($url, 'product=') !== false) {
2440 + // Extract product slug from various URL formats
2441 + $product_slug = '';
2442 +
2443 + // Handle pretty permalinks: /product/product-name/
2444 + if (preg_match('/\/product\/([^\/\?]+)/', $url, $matches)) {
2445 + $product_slug = $matches[1];
2446 + }
2447 + // Handle query parameters: ?product=product-name
2448 + elseif (preg_match('/[\?&]product=([^&]+)/', $url, $matches)) {
2449 + $product_slug = $matches[1];
2450 + }
2451 +
2452 + if (!empty($product_slug)) {
2453 + // Look up product by slug
2454 + $product = get_page_by_path($product_slug, OBJECT, 'product');
2455 + if ($product) {
2456 + return $product->ID;
2457 + }
2458 +
2459 + // Alternative method: query by post_name
2460 + global $wpdb;
2461 + $post_id = $wpdb->get_var($wpdb->prepare(
2462 + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'product' AND post_status = 'publish'",
2463 + $product_slug
2464 + ));
2465 +
2466 + if ($post_id) {
2467 + return intval($post_id);
2468 + }
2469 + }
2470 + }
2471 +
2472 + // Generic approach: try to extract slug and match against all post types
2473 + $parsed_url = wp_parse_url($clean_url);
2474 + $path = $parsed_url['path'] ?? '';
2475 +
2476 + if (!empty($path)) {
2477 + // Get the last part of the path as potential slug
2478 + $path_parts = array_filter(explode('/', trim($path, '/')));
2479 + $potential_slug = end($path_parts);
2480 +
2481 + if (!empty($potential_slug)) {
2482 + global $wpdb;
2483 +
2484 + // Try to find any post with this slug
2485 + $post_id = $wpdb->get_var($wpdb->prepare(
2486 + "SELECT ID FROM {$wpdb->posts}
2487 + WHERE post_name = %s
2488 + AND post_status IN ('publish', 'closed', 'private')
2489 + AND post_type NOT IN ('revision', 'attachment', 'nav_menu_item')
2490 + ORDER BY CASE
2491 + WHEN post_type = 'post' THEN 1
2492 + WHEN post_type = 'page' THEN 2
2493 + WHEN post_type = 'topic' THEN 3
2494 + WHEN post_type = 'product' THEN 4
2495 + ELSE 5
2496 + END
2497 + LIMIT 1",
2498 + $potential_slug
2499 + ));
2500 +
2501 + if ($post_id) {
2502 + return intval($post_id);
2503 + }
2504 + }
2505 + }
2506 +
2507 + // ADDITIONAL: Try direct database lookup by URL variations
2508 + global $wpdb;
2509 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2510 +
2511 + // Try variations of the URL (with/without trailing slash, http/https)
2512 + $url_variations = array(
2513 + $url,
2514 + rtrim($url, '/'),
2515 + $url . '/',
2516 + str_replace('http://', 'https://', $url),
2517 + str_replace('https://', 'http://', $url),
2518 + str_replace('http://', 'https://', rtrim($url, '/')),
2519 + str_replace('https://', 'http://', rtrim($url, '/'))
2520 + );
2521 +
2522 + // Remove duplicates
2523 + $url_variations = array_unique($url_variations);
2524 +
2525 + foreach ($url_variations as $variation) {
2526 + $existing_record = $wpdb->get_row($wpdb->prepare(
2527 + "SELECT id, source_url FROM $table_name WHERE source_url = %s",
2528 + $variation
2529 + ));
2530 +
2531 + if ($existing_record) {
2532 + // Try to get post ID from this stored URL
2533 + $stored_post_id = url_to_postid($existing_record->source_url);
2534 + if ($stored_post_id > 0) {
2535 + return $stored_post_id;
2536 + }
2537 + }
2538 + }
2539 +
2540 + return 0; // No match found
2541 +}
2542 +/**
2543 + * Process selected content via AJAX
2544 + */
2545 +public function ajax_mxchat_process_selected_content() {
2546 + // Basic request validation
2547 + if (!check_ajax_referer('mxchat_content_selector_nonce', 'nonce', false)) {
2548 + wp_send_json_error('Invalid nonce');
2549 + exit;
2550 + }
2551 +
2552 + if (!current_user_can('manage_options')) {
2553 + wp_send_json_error('Unauthorized access');
2554 + exit;
2555 + }
2556 +
2557 + // Get post IDs - safely parse the array
2558 + $post_ids = array();
2559 + if (isset($_POST['post_ids']) && is_array($_POST['post_ids'])) {
2560 + foreach ($_POST['post_ids'] as $id) {
2561 + $post_ids[] = absint($id);
2562 + }
2563 + }
2564 +
2565 + if (empty($post_ids)) {
2566 + wp_send_json_error('No content selected');
2567 + exit;
2568 + }
2569 +
2570 + // UPDATED: Get bot_id from request
2571 + $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
2572 +
2573 + // Process only ONE post at a time to avoid request size issues
2574 + $post_id = reset($post_ids);
2575 + $post = get_post($post_id);
2576 +
2577 + if (!$post) {
2578 + wp_send_json_error('Post not found');
2579 + exit;
2580 + }
2581 +
2582 + // Get content including ACF fields
2583 + $content = $post->post_title . "\n\n" . wp_strip_all_tags($post->post_content);
2584 +
2585 + // ADD ACF FIELDS SUPPORT
2586 + $acf_fields = $this->mxchat_get_acf_fields_for_post($post_id);
2587 + if (!empty($acf_fields)) {
2588 + $acf_content_parts = array();
2589 +
2590 + foreach ($acf_fields as $field_name => $field_value) {
2591 + $formatted_value = $this->mxchat_format_acf_field_value($field_value, $field_name, $post_id);
2592 +
2593 + if (!empty($formatted_value)) {
2594 + $field_label = ucwords(str_replace('_', ' ', $field_name));
2595 + $acf_content_parts[] = $field_label . ": " . $formatted_value;
2596 + }
2597 + }
2598 +
2599 + if (!empty($acf_content_parts)) {
2600 + $content .= "\n\n" . implode("\n", $acf_content_parts);
2601 + }
2602 + }
2603 +
2604 + $content = substr($content, 0, 10000); // Limit content size
2605 +
2606 + // UPDATED: Get bot-specific API key
2607 + $bot_options = $this->get_bot_options($bot_id);
2608 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
2609 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2610 +
2611 + if (strpos($selected_model, 'voyage') === 0) {
2612 + $api_key = $options['voyage_api_key'] ?? '';
2613 + $provider_name = 'Voyage AI';
2614 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
2615 + $api_key = $options['gemini_api_key'] ?? '';
2616 + $provider_name = 'Google Gemini';
2617 + } else {
2618 + $api_key = $options['api_key'] ?? '';
2619 + $provider_name = 'OpenAI';
2620 + }
2621 +
2622 + if (empty($api_key)) {
2623 + wp_send_json_error($provider_name . ' API key not configured');
2624 + exit;
2625 + }
2626 +
2627 + $source_url = get_permalink($post_id);
2628 + $vector_id = md5($source_url); // Vector ID for Pinecone
2629 +
2630 + // UPDATED: Check for existing content in bot-specific storage
2631 + $is_update = false;
2632 +
2633 + // Get bot-specific Pinecone configuration
2634 + $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
2635 + $use_pinecone = !empty($bot_pinecone_config) && ($bot_pinecone_config['use_pinecone'] ?? false);
2636 +
2637 + if ($use_pinecone && !empty($bot_pinecone_config['api_key'])) {
2638 + // Check Pinecone for this bot
2639 + $pinecone_data = $this->mxchat_get_pinecone_processed_content($bot_pinecone_config);
2640 + if (isset($pinecone_data[$post_id])) {
2641 + $is_update = true;
2642 + }
2643 + } else {
2644 + // Check WordPress DB (same as before since it's shared)
2645 + global $wpdb;
2646 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
2647 + $existing_record = $wpdb->get_row($wpdb->prepare(
2648 + "SELECT id FROM $table_name WHERE source_url = %s",
2649 + $source_url
2650 + ));
2651 +
2652 + if ($existing_record) {
2653 + $is_update = true;
2654 + }
2655 + }
2656 +
2657 + // UPDATED: Use the centralized utility function with bot_id
2658 + $result = MxChat_Utils::submit_content_to_db(
2659 + $content,
2660 + $source_url,
2661 + $api_key,
2662 + $vector_id,
2663 + $bot_id
2664 + );
2665 +
2666 + if (is_wp_error($result)) {
2667 + wp_send_json_error('Storage failed: ' . $result->get_error_message());
2668 + exit;
2669 + }
2670 +
2671 + $operation_type = $is_update ? 'update' : 'new';
2672 +
2673 + // Count ACF fields for debugging
2674 + $acf_field_count = count($acf_fields);
2675 +
2676 + // Success response with minimal data
2677 + wp_send_json_success(array(
2678 + 'message' => $operation_type === 'update' ? 'Content updated successfully' : 'Content processed successfully',
2679 + 'post_id' => $post_id,
2680 + 'title' => $post->post_title,
2681 + 'operation_type' => $operation_type,
2682 + 'vector_id' => $vector_id,
2683 + 'acf_fields_found' => $acf_field_count,
2684 + 'content_preview' => substr($content, 0, 100) . '...',
2685 + 'bot_id' => $bot_id
2686 + ));
2687 + exit;
2688 +}
2689 +
2690 +public function mxchat_get_public_post_types() {
2691 + // Get all public post types
2692 + $post_types = get_post_types(array('public' => true), 'objects');
2693 + $post_type_options = array();
2694 +
2695 + foreach ($post_types as $post_type) {
2696 + $post_type_options[$post_type->name] = $post_type->label;
2697 + }
2698 +
2699 + // Also include common forum/community post types that might not be marked as public
2700 + $additional_types = array(
2701 + 'topic' => 'Forum Topics (bbPress)',
2702 + 'reply' => 'Forum Replies (bbPress)',
2703 + 'forum' => 'Forums (bbPress)',
2704 + 'wpforo_topic' => 'wpForo Topics',
2705 + 'wpforo_post' => 'wpForo Posts'
2706 + );
2707 +
2708 + foreach ($additional_types as $type_name => $type_label) {
2709 + if (post_type_exists($type_name) && !isset($post_type_options[$type_name])) {
2710 + $post_type_options[$type_name] = $type_label;
2711 + }
2712 + }
2713 +
2714 + return $post_type_options;
2715 +}
2716 +
2717 +/**
2718 + * Retrieves processed content from Pinecone API
2719 + */
2720 +public function mxchat_get_pinecone_processed_content($pinecone_options) {
2721 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2722 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2723 +
2724 + if (empty($api_key) || empty($host)) {
2725 + return array();
2726 + }
2727 +
2728 + $pinecone_data = array();
2729 +
2730 + try {
2731 + // Always get fresh data from Pinecone
2732 + $pinecone_data = $this->mxchat_scan_pinecone_for_processed_content($pinecone_options);
2733 +
2734 + // Method 2: Final fallback - try stats endpoint (if available)
2735 + if (empty($pinecone_data)) {
2736 + $stats_url = "https://{$host}/describe_index_stats";
2737 +
2738 + $response = wp_remote_post($stats_url, array(
2739 + 'headers' => array(
2740 + 'Api-Key' => $api_key,
2741 + 'Content-Type' => 'application/json'
2742 + ),
2743 + 'body' => json_encode(array()),
2744 + 'timeout' => 30
2745 + ));
2746 +
2747 + if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
2748 + $body = wp_remote_retrieve_body($response);
2749 + $stats_data = json_decode($body, true);
2750 + }
2751 + }
2752 +
2753 + } catch (Exception $e) {
2754 + // Log error but return fresh data only
2755 + }
2756 +
2757 + return $pinecone_data;
2758 +}
2759 +public function mxchat_fetch_pinecone_vectors_by_ids($pinecone_options, $vector_ids) {
2760 + //error_log('=== DEBUG: Starting mxchat_fetch_pinecone_vectors_by_ids ===');
2761 +
2762 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2763 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2764 +
2765 + if (empty($api_key) || empty($host) || empty($vector_ids)) {
2766 + //error_log('DEBUG: Missing parameters for fetch by IDs');
2767 + return array();
2768 + }
2769 +
2770 + try {
2771 + $fetch_url = "https://{$host}/vectors/fetch";
2772 + //error_log('DEBUG: Fetch URL: ' . $fetch_url);
2773 + //error_log('DEBUG: Fetching ' . count($vector_ids) . ' vector IDs');
2774 +
2775 + // Pinecone fetch API allows fetching specific vectors by ID
2776 + $fetch_data = array(
2777 + 'ids' => array_values($vector_ids)
2778 + );
2779 +
2780 + $response = wp_remote_post($fetch_url, array(
2781 + 'headers' => array(
2782 + 'Api-Key' => $api_key,
2783 + 'Content-Type' => 'application/json'
2784 + ),
2785 + 'body' => json_encode($fetch_data),
2786 + 'timeout' => 30
2787 + ));
2788 +
2789 + if (is_wp_error($response)) {
2790 + //error_log('DEBUG: Fetch by IDs WP error: ' . $response->get_error_message());
2791 + return array();
2792 + }
2793 +
2794 + $response_code = wp_remote_retrieve_response_code($response);
2795 + //error_log('DEBUG: Fetch response code: ' . $response_code);
2796 +
2797 + if ($response_code !== 200) {
2798 + $error_body = wp_remote_retrieve_body($response);
2799 + //error_log('DEBUG: Fetch failed with body: ' . $error_body);
2800 + return array();
2801 + }
2802 +
2803 + $body = wp_remote_retrieve_body($response);
2804 + $data = json_decode($body, true);
2805 +
2806 + //error_log('DEBUG: Fetch response structure: ' . print_r(array_keys($data), true));
2807 +
2808 + if (!isset($data['vectors'])) {
2809 + //error_log('DEBUG: No vectors key in response');
2810 + return array();
2811 + }
2812 +
2813 + $processed_data = array();
2814 +
2815 + foreach ($data['vectors'] as $vector_id => $vector_data) {
2816 + $metadata = $vector_data['metadata'] ?? array();
2817 + $source_url = $metadata['source_url'] ?? '';
2818 +
2819 + if (!empty($source_url)) {
2820 + $post_id = url_to_postid($source_url);
2821 + if ($post_id) {
2822 + $created_at = $metadata['created_at'] ?? '';
2823 + $processed_date = 'Recently';
2824 +
2825 + if (!empty($created_at)) {
2826 + $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
2827 + if ($timestamp) {
2828 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
2829 + }
2830 + }
2831 +
2832 + $processed_data[$post_id] = array(
2833 + 'db_id' => $vector_id,
2834 + 'processed_date' => $processed_date,
2835 + 'url' => $source_url,
2836 + 'source' => 'pinecone',
2837 + 'timestamp' => $timestamp ?? current_time('timestamp')
2838 + );
2839 + }
2840 + }
2841 + }
2842 +
2843 + //error_log('DEBUG: Processed ' . count($processed_data) . ' vectors from fetch');
2844 + return $processed_data;
2845 +
2846 + } catch (Exception $e) {
2847 + //error_log('DEBUG: Exception in fetch_pinecone_vectors_by_ids: ' . $e->getMessage());
2848 + return array();
2849 + }
2850 +}
2851 +
2852 +/**
2853 + * Scan Pinecone for processed content
2854 + */
2855 +public function mxchat_scan_pinecone_for_processed_content($pinecone_options) {
2856 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
2857 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
2858 +
2859 + if (empty($api_key) || empty($host)) {
2860 + return array();
2861 + }
2862 +
2863 + try {
2864 + // Use multiple random vectors to get better coverage
2865 + $all_matches = array();
2866 + $seen_ids = array();
2867 +
2868 + // Try 3 different random vectors to get better coverage
2869 + for ($i = 0; $i < 3; $i++) {
2870 + $query_url = "https://{$host}/query";
2871 +
2872 + // Generate a random unit vector instead of zeros
2873 + $random_vector = array();
2874 + for ($j = 0; $j < 1536; $j++) {
2875 + $random_vector[] = (rand(-1000, 1000) / 1000.0);
2876 + }
2877 +
2878 + // Normalize the vector to unit length
2879 + $magnitude = sqrt(array_sum(array_map(function($x) { return $x * $x; }, $random_vector)));
2880 + if ($magnitude > 0) {
2881 + $random_vector = array_map(function($x) use ($magnitude) { return $x / $magnitude; }, $random_vector);
2882 + }
2883 +
2884 + $query_data = array(
2885 + 'includeMetadata' => true,
2886 + 'includeValues' => false,
2887 + 'topK' => 10000,
2888 + 'vector' => $random_vector
2889 + );
2890 +
2891 + $response = wp_remote_post($query_url, array(
2892 + 'headers' => array(
2893 + 'Api-Key' => $api_key,
2894 + 'Content-Type' => 'application/json'
2895 + ),
2896 + 'body' => json_encode($query_data),
2897 + 'timeout' => 30
2898 + ));
2899 +
2900 + if (is_wp_error($response)) {
2901 + continue;
2902 + }
2903 +
2904 + $response_code = wp_remote_retrieve_response_code($response);
2905 +
2906 + if ($response_code !== 200) {
2907 + continue;
2908 + }
2909 +
2910 + $body = wp_remote_retrieve_body($response);
2911 + $data = json_decode($body, true);
2912 +
2913 + if (isset($data['matches'])) {
2914 + foreach ($data['matches'] as $match) {
2915 + $match_id = $match['id'] ?? '';
2916 + if (!empty($match_id) && !isset($seen_ids[$match_id])) {
2917 + $all_matches[] = $match;
2918 + $seen_ids[$match_id] = true;
2919 + }
2920 + }
2921 + }
2922 + }
2923 +
2924 + // Convert matches to processed data format
2925 + $processed_data = array();
2926 +
2927 + foreach ($all_matches as $match) {
2928 + $metadata = $match['metadata'] ?? array();
2929 + $source_url = $metadata['source_url'] ?? '';
2930 + $match_id = $match['id'] ?? '';
2931 +
2932 + if (!empty($source_url) && !empty($match_id)) {
2933 + $post_id = url_to_postid($source_url);
2934 + if ($post_id) {
2935 + $created_at = $metadata['created_at'] ?? '';
2936 + $processed_date = 'Recently';
2937 +
2938 + if (!empty($created_at)) {
2939 + $timestamp = is_numeric($created_at) ? $created_at : strtotime($created_at);
2940 + if ($timestamp) {
2941 + $processed_date = human_time_diff($timestamp, current_time('timestamp')) . ' ago';
2942 + }
2943 + }
2944 +
2945 + $processed_data[$post_id] = array(
2946 + 'db_id' => $match_id,
2947 + 'processed_date' => $processed_date,
2948 + 'url' => $source_url,
2949 + 'source' => 'pinecone',
2950 + 'timestamp' => $timestamp ?? current_time('timestamp')
2951 + );
2952 + }
2953 + }
2954 + }
2955 +
2956 + return $processed_data;
2957 +
2958 + } catch (Exception $e) {
2959 + return array();
2960 + }
2961 +}
2962 +/**
2963 + * UPDATED: Generate embeddings from input text for MXChat with bot support
2964 + */
2965 +private function mxchat_generate_embedding($text, $bot_id = 'default') {
2966 + // Enable detailed logging for debugging
2967 + //error_log('[MXCHAT-EMBED] Starting embedding generation for bot: ' . $bot_id . '. Text length: ' . strlen($text) . ' bytes');
2968 + //error_log('[MXCHAT-EMBED] Text preview: ' . substr($text, 0, 100) . '...');
2969 +
2970 + // UPDATED: Get bot-specific options
2971 + $bot_options = $this->get_bot_options($bot_id);
2972 + $options = !empty($bot_options) ? $bot_options : get_option('mxchat_options');
2973 +
2974 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2975 + //error_log('[MXCHAT-EMBED] Selected embedding model for bot ' . $bot_id . ': ' . $selected_model);
2976 +
2977 + // Determine provider and endpoint
2978 + if (strpos($selected_model, 'voyage') === 0) {
2979 + $api_key = $options['voyage_api_key'] ?? '';
2980 + $endpoint = 'https://api.voyageai.com/v1/embeddings';
2981 + $provider_name = 'Voyage AI';
2982 + //error_log('[MXCHAT-EMBED] Using Voyage AI API for bot ' . $bot_id);
2983 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
2984 + $api_key = $options['gemini_api_key'] ?? '';
2985 + $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
2986 + $provider_name = 'Google Gemini';
2987 + //error_log('[MXCHAT-EMBED] Using Google Gemini API for bot ' . $bot_id);
2988 + } else {
2989 + $api_key = $options['api_key'] ?? '';
2990 + $endpoint = 'https://api.openai.com/v1/embeddings';
2991 + $provider_name = 'OpenAI';
2992 + //error_log('[MXCHAT-EMBED] Using OpenAI API for bot ' . $bot_id);
2993 + }
2994 +
2995 + //error_log('[MXCHAT-EMBED] Using endpoint: ' . $endpoint);
2996 +
2997 + if (empty($api_key)) {
2998 + $error_message = sprintf('Missing %s API key for bot %s. Please configure your API key in the bot settings.', $provider_name, $bot_id);
2999 + //error_log('[MXCHAT-EMBED] Error: ' . $error_message);
3000 + return $error_message;
3001 + }
3002 +
3003 + // Check if text is too long (for OpenAI, roughly estimate tokens as words/0.75)
3004 + $estimated_tokens = ceil(str_word_count($text) / 0.75);
3005 + //error_log('[MXCHAT-EMBED] Estimated token count: ~' . $estimated_tokens);
3006 +
3007 + if ($estimated_tokens > 8000 && strpos($selected_model, 'voyage') === false && strpos($selected_model, 'gemini-embedding') === false) {
3008 + //error_log('[MXCHAT-EMBED] Warning: Text may exceed OpenAI token limits (8K for most models)');
3009 + // Consider truncating text here
3010 + }
3011 +
3012 + // Prepare request body based on provider
3013 + if (strpos($selected_model, 'gemini-embedding') === 0) {
3014 + // Gemini API format
3015 + $request_body = array(
3016 + 'model' => 'models/' . $selected_model,
3017 + 'content' => array(
3018 + 'parts' => array(
3019 + array('text' => $text)
3020 + )
3021 + )
3022 + );
3023 +
3024 + // Set output dimensionality to 1536 for consistency with other models
3025 + $request_body['outputDimensionality'] = 1536;
3026 + } else {
3027 + // OpenAI/Voyage API format
3028 + $request_body = array(
3029 + 'model' => $selected_model,
3030 + 'input' => $text
3031 + );
3032 +
3033 + // Add output_dimension for voyage-3-large model
3034 + if ($selected_model === 'voyage-3-large') {
3035 + $request_body['output_dimension'] = 2048;
3036 + }
3037 + }
3038 +
3039 + //error_log('[MXCHAT-EMBED] Request prepared with model: ' . $selected_model);
3040 +
3041 + // Prepare headers based on provider
3042 + if (strpos($selected_model, 'gemini-embedding') === 0) {
3043 + // Gemini uses API key as query parameter
3044 + $endpoint .= '?key=' . $api_key;
3045 + $headers = array(
3046 + 'Content-Type' => 'application/json'
3047 + );
3048 + } else {
3049 + // OpenAI/Voyage use Bearer token
3050 + $headers = array(
3051 + 'Authorization' => 'Bearer ' . $api_key,
3052 + 'Content-Type' => 'application/json'
3053 + );
3054 + }
3055 +
3056 + // Make API request
3057 + //error_log('[MXCHAT-EMBED] Sending API request to: ' . $endpoint);
3058 + $response = wp_remote_post($endpoint, array(
3059 + 'body' => wp_json_encode($request_body),
3060 + 'headers' => $headers,
3061 + 'timeout' => 60 // Increased timeout for large inputs
3062 + ));
3063 +
3064 + // Handle wp_remote_post errors
3065 + if (is_wp_error($response)) {
3066 + $error_message = $response->get_error_message();
3067 + //error_log('[MXCHAT-EMBED] WP Remote Post Error: ' . $error_message);
3068 + return 'Connection error: ' . $error_message;
3069 + }
3070 +
3071 + // Get and check HTTP response code
3072 + $http_code = wp_remote_retrieve_response_code($response);
3073 + //error_log('[MXCHAT-EMBED] API Response Code: ' . $http_code);
3074 +
3075 + if ($http_code !== 200) {
3076 + $error_body = wp_remote_retrieve_body($response);
3077 + //error_log('[MXCHAT-EMBED] API Error Response Body: ' . $error_body);
3078 +
3079 + // Try to parse error for more details
3080 + $error_json = json_decode($error_body, true);
3081 + if (json_last_error() === JSON_ERROR_NONE && isset($error_json['error'])) {
3082 + $error_type = $error_json['error']['type'] ?? 'unknown';
3083 + $error_message = $error_json['error']['message'] ?? 'No message';
3084 + //error_log('[MXCHAT-EMBED] API Error Type: ' . $error_type);
3085 + //error_log('[MXCHAT-EMBED] API Error Message: ' . $error_message);
3086 +
3087 + // Customize error message for common API errors
3088 + if ($error_type === 'invalid_request_error' && strpos($error_message, 'API key') !== false) {
3089 + $error_message = sprintf('Invalid %s API key for bot %s. Please check your API key in the bot settings.', $provider_name, $bot_id);
3090 + } elseif ($error_type === 'authentication_error') {
3091 + $error_message = sprintf('%s authentication failed for bot %s. Please verify your API key in the bot settings.', $provider_name, $bot_id);
3092 + }
3093 +
3094 + //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
3095 + return $error_message;
3096 + }
3097 +
3098 + $error_message = sprintf("API Error (HTTP %d): Unable to generate embedding for bot %s", $http_code, $bot_id);
3099 + //error_log('[MXCHAT-EMBED] Returning error: ' . $error_message);
3100 + return $error_message;
3101 + }
3102 +
3103 + // Parse response body
3104 + $response_body = wp_remote_retrieve_body($response);
3105 + //error_log('[MXCHAT-EMBED] Received response length: ' . strlen($response_body) . ' bytes');
3106 +
3107 + $response_data = json_decode($response_body, true);
3108 +
3109 + if (json_last_error() !== JSON_ERROR_NONE) {
3110 + $error = json_last_error_msg();
3111 + //error_log('[MXCHAT-EMBED] JSON Parse Error: ' . $error);
3112 + //error_log('[MXCHAT-EMBED] Response preview: ' . substr($response_body, 0, 200));
3113 + return "Failed to parse API response: $error";
3114 + }
3115 +
3116 + // Handle different response formats based on provider
3117 + if (strpos($selected_model, 'gemini-embedding') === 0) {
3118 + // Gemini API response format
3119 + if (isset($response_data['embedding']['values'])) {
3120 + $embedding_dimensions = count($response_data['embedding']['values']);
3121 + //error_log('[MXCHAT-EMBED] Successfully extracted Gemini embedding with ' . $embedding_dimensions . ' dimensions');
3122 +
3123 + // Check if embedding dimensions are as expected (should be 1536)
3124 + if ($embedding_dimensions !== 1536) {
3125 + //error_log('[MXCHAT-EMBED] Warning: Unexpected Gemini embedding dimensions: ' . $embedding_dimensions);
3126 + }
3127 +
3128 + return $response_data['embedding']['values'];
3129 + } else {
3130 + //error_log('[MXCHAT-EMBED] Error: No embedding found in Gemini response');
3131 + //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
3132 +
3133 + if (isset($response_data['error'])) {
3134 + $error_message = "Gemini API Error in response: " . wp_json_encode($response_data['error']);
3135 + //error_log('[MXCHAT-EMBED] ' . $error_message);
3136 + return $error_message;
3137 + }
3138 +
3139 + $error_message = "Invalid Gemini API response format: No embedding found";
3140 + //error_log('[MXCHAT-EMBED] ' . $error_message);
3141 + return $error_message;
3142 + }
3143 + } else {
3144 + // OpenAI/Voyage API response format
3145 + if (isset($response_data['data'][0]['embedding'])) {
3146 + $embedding_dimensions = count($response_data['data'][0]['embedding']);
3147 + //error_log('[MXCHAT-EMBED] Successfully extracted embedding with ' . $embedding_dimensions . ' dimensions');
3148 +
3149 + // Check if embedding dimensions are as expected
3150 + if (($selected_model === 'text-embedding-ada-002' && $embedding_dimensions !== 1536) ||
3151 + ($selected_model === 'voyage-3-large' && $embedding_dimensions !== 2048)) {
3152 + //error_log('[MXCHAT-EMBED] Warning: Unexpected embedding dimensions');
3153 + }
3154 +
3155 + return $response_data['data'][0]['embedding'];
3156 + } else {
3157 + //error_log('[MXCHAT-EMBED] Error: No embedding found in response');
3158 + //error_log('[MXCHAT-EMBED] Response structure: ' . wp_json_encode(array_keys($response_data)));
3159 +
3160 + if (isset($response_data['error'])) {
3161 + $error_message = "API Error in response: " . wp_json_encode($response_data['error']);
3162 + //error_log('[MXCHAT-EMBED] ' . $error_message);
3163 + return $error_message;
3164 + }
3165 +
3166 + $error_message = "Invalid API response format: No embedding found";
3167 + //error_log('[MXCHAT-EMBED] ' . $error_message);
3168 + return $error_message;
3169 + }
3170 + }
3171 +}
3172 +
3173 +/**
3174 + * Get bot-specific options for multi-bot functionality
3175 + * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
3176 + */
3177 +private function get_bot_options($bot_id = 'default') {
3178 + //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
3179 +
3180 + if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
3181 + //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
3182 + return array();
3183 + }
3184 +
3185 + $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
3186 +
3187 + if (!empty($bot_options)) {
3188 + //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
3189 + if (isset($bot_options['similarity_threshold'])) {
3190 + //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
3191 + }
3192 + }
3193 +
3194 + return is_array($bot_options) ? $bot_options : array();
3195 +}
3196 +
3197 +/**
3198 + * Get bot-specific Pinecone configuration
3199 + * Used in the knowledge retrieval functions
3200 + */
3201 +// Also add debugging to your get_bot_pinecone_config function
3202 +private function get_bot_pinecone_config($bot_id = 'default') {
3203 + //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
3204 +
3205 + // If default bot or multi-bot add-on not active, use default Pinecone config
3206 + if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
3207 + //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
3208 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
3209 + $config = array(
3210 + 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
3211 + 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
3212 + 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
3213 + 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
3214 + );
3215 + //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
3216 + return $config;
3217 + }
3218 +
3219 + //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
3220 +
3221 + // Hook for multi-bot add-on to provide bot-specific Pinecone config
3222 + $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
3223 +
3224 + if (!empty($bot_pinecone_config)) {
3225 + //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
3226 + //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
3227 + //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
3228 + //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
3229 + } else {
3230 + //error_log("MXCHAT DEBUG: Filter returned empty config!");
3231 + }
3232 +
3233 + return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
3234 +}
3235 +
3236 +
3237 +public function mxchat_ajax_dismiss_completed_status() {
3238 + try {
3239 + // Verify the request
3240 + check_ajax_referer('mxchat_status_nonce', 'nonce');
3241 +
3242 + if (!current_user_can('manage_options')) {
3243 + wp_send_json_error('Unauthorized access');
3244 + exit;
3245 + }
3246 +
3247 + $card_type = isset($_POST['card_type']) ? sanitize_text_field($_POST['card_type']) : '';
3248 +
3249 + if ($card_type === 'pdf') {
3250 + // Clear PDF status
3251 + $pdf_url = get_transient('mxchat_last_pdf_url');
3252 + if ($pdf_url) {
3253 + delete_transient('mxchat_pdf_status_' . md5($pdf_url));
3254 + delete_transient('mxchat_last_pdf_url');
3255 + }
3256 + } elseif ($card_type === 'sitemap') {
3257 + // Clear sitemap status
3258 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
3259 + if ($sitemap_url) {
3260 + delete_transient('mxchat_sitemap_status_' . md5($sitemap_url));
3261 + delete_transient('mxchat_last_sitemap_url');
3262 + }
3263 + }
3264 +
3265 + wp_send_json_success(array('message' => 'Status dismissed successfully'));
3266 +
3267 + } catch (Exception $e) {
3268 + wp_send_json_error(array('message' => 'Error dismissing status: ' . $e->getMessage()));
3269 + }
3270 +}
3271 +
3272 +/**
3273 + * Render completed status cards on page load
3274 + * This ensures completed processing status persists through page refreshes
3275 + */
3276 +public function mxchat_render_completed_status_cards() {
3277 + $output = '';
3278 +
3279 + // Check for completed PDF status
3280 + $pdf_url = get_transient('mxchat_last_pdf_url');
3281 + if ($pdf_url) {
3282 + $pdf_status = $this->mxchat_get_pdf_processing_status($pdf_url);
3283 + if ($pdf_status && ($pdf_status['status'] === 'complete' || $pdf_status['status'] === 'error')) {
3284 + $output .= $this->mxchat_render_pdf_status_card($pdf_status, $pdf_url);
3285 + }
3286 + }
3287 +
3288 + // Check for completed sitemap status
3289 + $sitemap_url = get_transient('mxchat_last_sitemap_url');
3290 + if ($sitemap_url) {
3291 + $sitemap_status = $this->mxchat_get_sitemap_processing_status($sitemap_url);
3292 + if ($sitemap_status && ($sitemap_status['status'] === 'complete' || $sitemap_status['status'] === 'error')) {
3293 + $output .= $this->mxchat_render_sitemap_status_card($sitemap_status, $sitemap_url);
3294 + }
3295 + }
3296 +
3297 + return $output;
3298 +}
3299 +
3300 +/**
3301 + * Render PDF status card HTML
3302 + */
3303 +private function mxchat_render_pdf_status_card($status, $pdf_url) {
3304 + $html = '<div class="mxchat-status-card" data-card-type="pdf">';
3305 + $html .= '<div class="mxchat-status-header">';
3306 + $html .= '<h4>' . esc_html__('PDF Processing Status', 'mxchat') . '</h4>';
3307 +
3308 + // Add dismiss button for completed status
3309 + if ($status['status'] === 'complete' || $status['status'] === 'error') {
3310 + $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
3311 + }
3312 +
3313 + // Process Batch button for processing status
3314 + if ($status['status'] === 'processing') {
3315 + $html .= '<button type="button" class="mxchat-manual-batch-btn"
3316 + data-process-type="pdf"
3317 + data-url="' . esc_attr($pdf_url) . '">
3318 + ' . esc_html__('Process Batch', 'mxchat') . '</button>';
3319 + }
3320 +
3321 + // Add status badges
3322 + if ($status['status'] === 'error') {
3323 + $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
3324 + } elseif ($status['status'] === 'complete') {
3325 + if ($status['failed_pages'] > 0) {
3326 + $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
3327 + sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_pages']) . '</span>';
3328 + } else {
3329 + $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
3330 + }
3331 + }
3332 +
3333 + $html .= '</div>'; // End header
3334 +
3335 + // Progress bar
3336 + $html .= '<div class="mxchat-progress-bar">';
3337 + $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
3338 + $html .= '</div>';
3339 +
3340 + // Status details
3341 + $html .= '<div class="mxchat-status-details">';
3342 + $html .= '<p>' . sprintf(
3343 + esc_html__('Progress: %d of %d pages (%d%%)', 'mxchat'),
3344 + $status['processed_pages'],
3345 + $status['total_pages'],
3346 + $status['percentage']
3347 + ) . '</p>';
3348 +
3349 + // Show failed pages count if any
3350 + if ($status['failed_pages'] > 0) {
3351 + $html .= '<p><strong>' . esc_html__('Failed pages:', 'mxchat') . '</strong> ' . esc_html($status['failed_pages']) . '</p>';
3352 + }
3353 +
3354 + $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
3355 + $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
3356 +
3357 + // Add completion summary if available AND it's an array
3358 + if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
3359 + $summary = $status['completion_summary'];
3360 + $html .= '<div class="mxchat-completion-summary">';
3361 + $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
3362 + $html .= '<p><strong>' . esc_html__('Total Pages:', 'mxchat') . '</strong> ' . esc_html($summary['total_pages']) . '</p>';
3363 + $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_pages']) . '</p>';
3364 + $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_pages']) . '</p>';
3365 + $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
3366 + $html .= '</div>';
3367 + }
3368 +
3369 + // Add failed pages list if any AND it's an array
3370 + if (isset($status['failed_pages_list']) && is_array($status['failed_pages_list']) && !empty($status['failed_pages_list'])) {
3371 + $html .= $this->mxchat_render_failed_pages_list($status['failed_pages_list']);
3372 + }
3373 +
3374 + // Add error message if any
3375 + if (isset($status['error']) && !empty($status['error'])) {
3376 + $html .= '<div class="mxchat-error-notice">';
3377 + $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
3378 + $html .= '</div>';
3379 + }
3380 +
3381 + $html .= '</div>'; // End details
3382 + $html .= '</div>'; // End card
3383 +
3384 + return $html;
3385 +}
3386 +/**
3387 + * Render sitemap status card HTML
3388 + */
3389 +private function mxchat_render_sitemap_status_card($status, $sitemap_url) {
3390 + $html = '<div class="mxchat-status-card" data-card-type="sitemap">';
3391 + $html .= '<div class="mxchat-status-header">';
3392 + $html .= '<h4>' . esc_html__('Sitemap Processing Status', 'mxchat') . '</h4>';
3393 +
3394 + // Add dismiss button for completed status
3395 + if ($status['status'] === 'complete' || $status['status'] === 'error') {
3396 + $html .= '<button type="button" class="mxchat-dismiss-button">' . esc_html__('Dismiss', 'mxchat') . '</button>';
3397 + }
3398 +
3399 + // Process Batch button for processing status
3400 + if ($status['status'] === 'processing') {
3401 + $html .= '<button type="button" class="mxchat-manual-batch-btn"
3402 + data-process-type="sitemap"
3403 + data-url="' . esc_attr($sitemap_url) . '">
3404 + ' . esc_html__('Process Batch', 'mxchat') . '</button>';
3405 + }
3406 +
3407 + // Add status badges
3408 + if ($status['status'] === 'error') {
3409 + $html .= '<span class="mxchat-status-badge mxchat-status-failed">' . esc_html__('Error', 'mxchat') . '</span>';
3410 + } elseif ($status['status'] === 'complete') {
3411 + if ($status['failed_urls'] > 0) {
3412 + $html .= '<span class="mxchat-status-badge mxchat-status-warning">' .
3413 + sprintf(esc_html__('Completed with %d failures', 'mxchat'), $status['failed_urls']) . '</span>';
3414 + } else {
3415 + $html .= '<span class="mxchat-status-badge mxchat-status-success">' . esc_html__('Complete', 'mxchat') . '</span>';
3416 + }
3417 + }
3418 +
3419 + $html .= '</div>'; // End header
3420 +
3421 + // Progress bar
3422 + $html .= '<div class="mxchat-progress-bar">';
3423 + $html .= '<div class="mxchat-progress-fill" style="width: ' . esc_attr($status['percentage']) . '%"></div>';
3424 + $html .= '</div>';
3425 +
3426 + // Status details
3427 + $html .= '<div class="mxchat-status-details">';
3428 + $html .= '<p>' . sprintf(
3429 + esc_html__('Progress: %d of %d URLs (%d%%)', 'mxchat'),
3430 + $status['processed_urls'],
3431 + $status['total_urls'],
3432 + $status['percentage']
3433 + ) . '</p>';
3434 +
3435 + // Show failed URLs count if any
3436 + if ($status['failed_urls'] > 0) {
3437 + $html .= '<p><strong>' . esc_html__('Failed URLs:', 'mxchat') . '</strong> ' . esc_html($status['failed_urls']) . '</p>';
3438 + }
3439 +
3440 + $html .= '<p><strong>' . esc_html__('Status:', 'mxchat') . '</strong> ' . esc_html(ucfirst($status['status'])) . '</p>';
3441 + $html .= '<p><strong>' . esc_html__('Last update:', 'mxchat') . '</strong> ' . esc_html($status['last_update']) . '</p>';
3442 +
3443 + // Add completion summary if available AND it's an array
3444 + if (isset($status['completion_summary']) && is_array($status['completion_summary']) && !empty($status['completion_summary'])) {
3445 + $summary = $status['completion_summary'];
3446 + $html .= '<div class="mxchat-completion-summary">';
3447 + $html .= '<h5>' . esc_html__('Processing Summary', 'mxchat') . '</h5>';
3448 + $html .= '<p><strong>' . esc_html__('Total URLs:', 'mxchat') . '</strong> ' . esc_html($summary['total_urls']) . '</p>';
3449 + $html .= '<p><strong>' . esc_html__('Successful:', 'mxchat') . '</strong> ' . esc_html($summary['successful_urls']) . '</p>';
3450 + $html .= '<p><strong>' . esc_html__('Failed:', 'mxchat') . '</strong> ' . esc_html($summary['failed_urls']) . '</p>';
3451 + $html .= '<p><strong>' . esc_html__('Completed:', 'mxchat') . '</strong> ' . esc_html($summary['completion_time']) . '</p>';
3452 + $html .= '</div>';
3453 + }
3454 +
3455 + // Add error messages if any (but not the failed URLs list)
3456 + if (!empty($status['error']) || !empty($status['last_error'])) {
3457 + $html .= '<div class="mxchat-error-notice">';
3458 +
3459 + if (!empty($status['error'])) {
3460 + $html .= '<p class="error">' . esc_html($status['error']) . '</p>';
3461 + }
3462 +
3463 + if (!empty($status['last_error'])) {
3464 + $html .= '<p class="last-error">' . esc_html__('Last error:', 'mxchat') . ' ' . esc_html($status['last_error']) . '</p>';
3465 + }
3466 +
3467 + $html .= '</div>';
3468 + }
3469 +
3470 + $html .= '</div>'; // End details
3471 + $html .= '</div>'; // End card
3472 +
3473 + return $html;
3474 +}
3475 +
3476 +
3477 +/**
3478 + * Render failed pages list
3479 + */
3480 +private function mxchat_render_failed_pages_list($failed_pages_list) {
3481 + // Validate that $failed_pages_list is an array and not empty
3482 + if (!is_array($failed_pages_list) || empty($failed_pages_list)) {
3483 + return '';
3484 + }
3485 +
3486 + $html = '<div class="mxchat-error-notice">';
3487 + $html .= '<div class="mxchat-failed-pages-container">';
3488 + $html .= '<h5>' . sprintf(esc_html__('Failed Pages (%d)', 'mxchat'), count($failed_pages_list)) . '</h5>';
3489 + $html .= '<details>';
3490 + $html .= '<summary>' . esc_html__('Show Failed Pages', 'mxchat') . '</summary>';
3491 + $html .= '<div class="mxchat-failed-pages-list">';
3492 +
3493 + // Create table for failed pages
3494 + $html .= '<table class="widefat striped">';
3495 + $html .= '<thead><tr>';
3496 + $html .= '<th>' . esc_html__('Page', 'mxchat') . '</th>';
3497 + $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
3498 + $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
3499 + $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
3500 + $html .= '</tr></thead><tbody>';
3501 +
3502 + // Sort failed pages by most recent
3503 + $sorted_failed_pages = $failed_pages_list;
3504 + usort($sorted_failed_pages, function($a, $b) {
3505 + return ($b['time'] ?? 0) - ($a['time'] ?? 0);
3506 + });
3507 +
3508 + foreach ($sorted_failed_pages as $item) {
3509 + // Ensure $item is an array before accessing its elements
3510 + if (!is_array($item)) {
3511 + continue;
3512 + }
3513 +
3514 + $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
3515 + $html .= '<tr>';
3516 + $html .= '<td>' . esc_html__('Page', 'mxchat') . ' ' . esc_html($item['page'] ?? 'Unknown') . '</td>';
3517 + $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
3518 + $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
3519 + $html .= '<td>' . esc_html($time_ago) . '</td>';
3520 + $html .= '</tr>';
3521 + }
3522 +
3523 + $html .= '</tbody></table>';
3524 + $html .= '</div></details></div></div>';
3525 +
3526 + return $html;
3527 +}
3528 +
3529 +/**
3530 + * Render failed URLs list
3531 + */
3532 +private function mxchat_render_failed_urls_list($failed_urls_list) {
3533 + // Validate that $failed_urls_list is an array and not empty
3534 + if (!is_array($failed_urls_list) || empty($failed_urls_list)) {
3535 + return '';
3536 + }
3537 +
3538 + $html = '<div class="mxchat-failed-urls-container">';
3539 + $html .= '<h5>' . sprintf(esc_html__('Failed URLs (%d)', 'mxchat'), count($failed_urls_list)) . '</h5>';
3540 + $html .= '<details>';
3541 + $html .= '<summary>' . esc_html__('Show Failed URLs', 'mxchat') . '</summary>';
3542 + $html .= '<div class="mxchat-failed-urls-list">';
3543 +
3544 + // Create table for failed URLs
3545 + $html .= '<table class="widefat striped">';
3546 + $html .= '<thead><tr>';
3547 + $html .= '<th>' . esc_html__('URL', 'mxchat') . '</th>';
3548 + $html .= '<th>' . esc_html__('Error', 'mxchat') . '</th>';
3549 + $html .= '<th>' . esc_html__('Retries', 'mxchat') . '</th>';
3550 + $html .= '<th>' . esc_html__('Time', 'mxchat') . '</th>';
3551 + $html .= '</tr></thead><tbody>';
3552 +
3553 + // Sort failed URLs by most recent
3554 + $sorted_failed_urls = $failed_urls_list;
3555 + usort($sorted_failed_urls, function($a, $b) {
3556 + return ($b['time'] ?? 0) - ($a['time'] ?? 0);
3557 + });
3558 +
3559 + // Show up to 50 failed URLs
3560 + $display_urls = array_slice($sorted_failed_urls, 0, 50);
3561 +
3562 + foreach ($display_urls as $item) {
3563 + // Ensure $item is an array before accessing its elements
3564 + if (!is_array($item)) {
3565 + continue;
3566 + }
3567 +
3568 + $url = $item['url'] ?? '';
3569 + $time_ago = isset($item['time']) ? human_time_diff($item['time'], current_time('timestamp')) . ' ' . esc_html__('ago', 'mxchat') : 'Unknown';
3570 +
3571 + // Truncate URL for display
3572 + $display_url = strlen($url) > 50 ? substr($url, 0, 47) . '...' : $url;
3573 +
3574 + $html .= '<tr>';
3575 + $html .= '<td style="word-break: break-all;">';
3576 + if (!empty($url)) {
3577 + $html .= '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($display_url) . '</a>';
3578 + } else {
3579 + $html .= esc_html__('Unknown URL', 'mxchat');
3580 + }
3581 + $html .= '</td>';
3582 + $html .= '<td style="word-break: break-word;">' . esc_html($item['error'] ?? 'Unknown error') . '</td>';
3583 + $html .= '<td>' . esc_html($item['retries'] ?? 'N/A') . '</td>';
3584 + $html .= '<td>' . esc_html($time_ago) . '</td>';
3585 + $html .= '</tr>';
3586 + }
3587 +
3588 + $html .= '</tbody></table>';
3589 +
3590 + if (count($failed_urls_list) > 50) {
3591 + $html .= '<div class="mxchat-failed-urls-more">+ ' .
3592 + (count($failed_urls_list) - 50) .
3593 + ' ' . esc_html__('more failed URLs not shown', 'mxchat') . '</div>';
3594 + }
3595 +
3596 + $html .= '</div></details></div>';
3597 +
3598 + return $html;
3599 +}
3600 +
3601 +/**
3602 + * Get all ACF fields for a specific post
3603 + */
3604 +public function mxchat_get_acf_fields_for_post($post_id) {
3605 + if (!function_exists('get_fields')) {
3606 + return array();
3607 + }
3608 +
3609 + $fields = get_fields($post_id);
3610 + if (!$fields || !is_array($fields)) {
3611 + return array();
3612 + }
3613 +
3614 + return $fields;
3615 +}
3616 +
3617 +/**
3618 + * Format ACF field values for content extraction
3619 + */
3620 +public function mxchat_format_acf_field_value($value, $field_name = '', $post_id = 0) {
3621 + if (empty($value)) {
3622 + return '';
3623 + }
3624 +
3625 + // Handle WP_Post objects first (THIS IS THE KEY FIX)
3626 + if ($value instanceof WP_Post) {
3627 + return $value->post_title ?: '';
3628 + }
3629 +
3630 + // Handle other WP objects
3631 + if (is_object($value)) {
3632 + if (isset($value->post_title)) {
3633 + return $value->post_title;
3634 + } elseif (isset($value->display_name)) {
3635 + return $value->display_name;
3636 + } elseif (isset($value->name)) {
3637 + return $value->name;
3638 + } elseif (method_exists($value, '__toString')) {
3639 + try {
3640 + return (string) $value;
3641 + } catch (Exception $e) {
3642 + return '';
3643 + }
3644 + }
3645 + // For any other objects, return empty string
3646 + return '';
3647 + }
3648 +
3649 + // Handle different ACF field types
3650 + if (is_array($value)) {
3651 + // Check if it's an image/file field
3652 + if (isset($value['url'])) {
3653 + // Image field - return alt text, title, or caption
3654 + if (!empty($value['alt'])) {
3655 + return $value['alt'];
3656 + } elseif (!empty($value['title'])) {
3657 + return $value['title'];
3658 + } elseif (!empty($value['caption'])) {
3659 + return $value['caption'];
3660 + } else {
3661 + return ''; // Don't include just the URL
3662 + }
3663 + }
3664 +
3665 + // Check if it's a post object or relationship field
3666 + if (isset($value['post_title'])) {
3667 + return $value['post_title'];
3668 + }
3669 +
3670 + // Check if it's a user field
3671 + if (isset($value['display_name'])) {
3672 + return $value['display_name'];
3673 + }
3674 +
3675 + // Check if it's a taxonomy term
3676 + if (isset($value['name']) && isset($value['taxonomy'])) {
3677 + return $value['name'];
3678 + }
3679 +
3680 + // Check if it's a select field with label
3681 + if (isset($value['label'])) {
3682 + return $value['label'];
3683 + }
3684 +
3685 + // Check for repeater field or flexible content
3686 + if (is_numeric(key($value))) {
3687 + $sub_values = array();
3688 + foreach ($value as $sub_item) {
3689 + if (is_array($sub_item)) {
3690 + // For repeater/flexible content, extract text values
3691 + $sub_text = $this->mxchat_extract_text_from_acf_array($sub_item);
3692 + if (!empty($sub_text)) {
3693 + $sub_values[] = $sub_text;
3694 + }
3695 + } elseif ($sub_item instanceof WP_Post) {
3696 + // Handle WP_Post objects in arrays
3697 + $sub_values[] = $sub_item->post_title ?: '';
3698 + } else {
3699 + $sub_values[] = (string) $sub_item;
3700 + }
3701 + }
3702 + return implode(', ', array_filter($sub_values));
3703 + }
3704 +
3705 + // For other arrays, try to extract meaningful text
3706 + $text_values = array();
3707 + foreach ($value as $key => $val) {
3708 + if (is_string($val) && !empty(trim($val))) {
3709 + $text_values[] = trim($val);
3710 + } elseif ($val instanceof WP_Post) {
3711 + // Handle WP_Post objects in associative arrays
3712 + $text_values[] = $val->post_title ?: '';
3713 + } elseif (is_array($val) && isset($val['post_title'])) {
3714 + $text_values[] = $val['post_title'];
3715 + } elseif (is_array($val) && isset($val['name'])) {
3716 + $text_values[] = $val['name'];
3717 + }
3718 + }
3719 +
3720 + return implode(', ', array_filter($text_values));
3721 + }
3722 +
3723 + // Handle boolean values
3724 + if (is_bool($value)) {
3725 + return $value ? 'Yes' : 'No';
3726 + }
3727 +
3728 + // Handle numeric values
3729 + if (is_numeric($value)) {
3730 + return (string) $value;
3731 + }
3732 +
3733 + // Handle string values
3734 + if (is_string($value)) {
3735 + return trim($value);
3736 + }
3737 +
3738 + // For anything else that we can't handle, return empty string
3739 + // This prevents the "Object could not be converted to string" error
3740 + return '';
3741 +}
3742 +
3743 +/**
3744 + * Extract text from complex ACF array structures
3745 + */
3746 +private function mxchat_extract_text_from_acf_array($array) {
3747 + if (!is_array($array)) {
3748 + return '';
3749 + }
3750 +
3751 + $text_parts = array();
3752 +
3753 + foreach ($array as $key => $value) {
3754 + if (is_string($value) && !empty(trim($value))) {
3755 + // Skip keys that are likely to be IDs or technical values
3756 + if (!is_numeric($value) || strlen($value) > 10) {
3757 + $text_parts[] = trim($value);
3758 + }
3759 + } elseif ($value instanceof WP_Post) {
3760 + // Handle WP_Post objects
3761 + $text_parts[] = $value->post_title ?: '';
3762 + } elseif (is_array($value)) {
3763 + if (isset($value['post_title'])) {
3764 + $text_parts[] = $value['post_title'];
3765 + } elseif (isset($value['name'])) {
3766 + $text_parts[] = $value['name'];
3767 + } elseif (isset($value['label'])) {
3768 + $text_parts[] = $value['label'];
3769 + }
3770 + } elseif (is_object($value)) {
3771 + // Handle other objects safely
3772 + if (isset($value->post_title)) {
3773 + $text_parts[] = $value->post_title;
3774 + } elseif (isset($value->name)) {
3775 + $text_parts[] = $value->name;
3776 + } elseif (isset($value->display_name)) {
3777 + $text_parts[] = $value->display_name;
3778 + }
3779 + }
3780 + }
3781 +
3782 + return implode(', ', array_filter($text_parts));
3783 +}
3784 +
3785 +public function mxchat_handle_post_update($post_id, $post, $update) {
3786 + // Basic validation checks
3787 + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
3788 + return;
3789 + }
3790 +
3791 + $post_type = $post->post_type;
3792 +
3793 + // Check if sync is enabled for this post type
3794 + $should_sync = false;
3795 +
3796 + // Check built-in post types first
3797 + if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
3798 + $should_sync = true;
3799 + } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
3800 + $should_sync = true;
3801 + } else {
3802 + // Check custom post types
3803 + $option_name = 'mxchat_auto_sync_' . $post_type;
3804 + if (get_option($option_name) === '1') {
3805 + $should_sync = true;
3806 + }
3807 + }
3808 +
3809 + if (!$should_sync) {
3810 + return;
3811 + }
3812 +
3813 + // Check if we have stored the previous status and URL in our transients
3814 + $previous_status_key = 'mxchat_prev_status_' . $post_id;
3815 + $previous_status = get_transient($previous_status_key);
3816 +
3817 + $previous_url_key = 'mxchat_prev_url_' . $post_id;
3818 + $previous_url = get_transient($previous_url_key);
3819 +
3820 + // If the post was previously published but is now not published, remove from knowledge base
3821 + if ($previous_status === 'publish' && $post->post_status !== 'publish') {
3822 + // Use the stored URL from when it was published, or fall back to current permalink
3823 + $source_url = $previous_url ?: get_permalink($post_id);
3824 +
3825 + if ($source_url) {
3826 + // Check if Pinecone is enabled
3827 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
3828 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
3829 +
3830 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
3831 + // Delete from Pinecone
3832 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
3833 + } else {
3834 + // Delete from WordPress DB
3835 + global $wpdb;
3836 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
3837 +
3838 + $result = $wpdb->delete(
3839 + $table_name,
3840 + array('source_url' => $source_url),
3841 + array('%s')
3842 + );
3843 + }
3844 + }
3845 +
3846 + // Clean up the transients and exit early
3847 + delete_transient($previous_status_key);
3848 + delete_transient($previous_url_key);
3849 + return;
3850 + }
3851 +
3852 + // Store the current status for next time (if this is an update)
3853 + if ($update) {
3854 + set_transient($previous_status_key, $post->post_status, DAY_IN_SECONDS);
3855 +
3856 + // If the post is currently published, also store its URL
3857 + if ($post->post_status === 'publish') {
3858 + $current_url = get_permalink($post_id);
3859 + set_transient($previous_url_key, $current_url, DAY_IN_SECONDS);
3860 + }
3861 + }
3862 +
3863 + // Only process currently published content for adding/updating
3864 + if ($post->post_status === 'publish') {
3865 + // Get the source URL
3866 + $source_url = get_permalink($post_id);
3867 +
3868 + // Get content with proper formatting (matching ajax_mxchat_process_selected_content)
3869 + $title = get_the_title($post_id);
3870 + $content = get_post_field('post_content', $post_id);
3871 +
3872 + // Apply WordPress content filters to get properly formatted content
3873 + $content = apply_filters('the_content', $content);
3874 +
3875 + // Strip tags but preserve structure
3876 + $content = wp_strip_all_tags($content);
3877 +
3878 + // Combine title and content
3879 + $final_content = $title . "\n\n" . $content;
3880 +
3881 + // For custom post types like job_listing, include additional fields
3882 + if ($post_type === 'job_listing') {
3883 + // Add job-specific meta if available
3884 + $job_location = get_post_meta($post_id, '_job_location', true);
3885 + if (!empty($job_location)) {
3886 + $final_content .= "\n\nLocation: " . $job_location;
3887 + }
3888 +
3889 + // Get job type terms
3890 + $job_types = get_the_terms($post_id, 'job_listing_type');
3891 + if (!empty($job_types) && !is_wp_error($job_types)) {
3892 + $types = array();
3893 + foreach ($job_types as $type) {
3894 + $types[] = $type->name;
3895 + }
3896 + $final_content .= "\n\nJob Type: " . implode(', ', $types);
3897 + }
3898 +
3899 + // Get company name if available
3900 + $company_name = get_post_meta($post_id, '_company_name', true);
3901 + if (!empty($company_name)) {
3902 + $final_content .= "\n\nCompany: " . $company_name;
3903 + }
3904 + }
3905 +
3906 + // Get API key with proper model detection
3907 + $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 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
3913 + $api_key = $options['gemini_api_key'] ?? '';
3914 + } else {
3915 + $api_key = $options['api_key'] ?? '';
3916 + }
3917 +
3918 + if (empty($api_key)) {
3919 + return;
3920 + }
3921 +
3922 + // Use the centralized utility function for storage
3923 + $result = MxChat_Utils::submit_content_to_db(
3924 + $final_content,
3925 + $source_url,
3926 + $api_key,
3927 + md5($source_url) // Vector ID for Pinecone
3928 + );
3929 + }
3930 +
3931 + // Clean up the stored previous status if not used above
3932 + if ($previous_status !== 'publish' || $post->post_status === 'publish') {
3933 + delete_transient($previous_status_key);
3934 + delete_transient($previous_url_key);
3935 + }
3936 +}
3937 +
3938 +/**
3939 + * Store the post status and URL before update to detect status transitions
3940 + * This runs before the post is actually updated in the database
3941 + */
3942 +public function mxchat_store_pre_update_status($post_id, $data) {
3943 + // Get the current post from database (before update)
3944 + $current_post = get_post($post_id);
3945 +
3946 + if ($current_post) {
3947 + // Store the current status temporarily
3948 + $status_key = 'mxchat_prev_status_' . $post_id;
3949 + set_transient($status_key, $current_post->post_status, HOUR_IN_SECONDS);
3950 +
3951 + // If the post is currently published, also store its URL
3952 + if ($current_post->post_status === 'publish') {
3953 + $url_key = 'mxchat_prev_url_' . $post_id;
3954 + $current_url = get_permalink($post_id);
3955 + set_transient($url_key, $current_url, HOUR_IN_SECONDS);
3956 + }
3957 + }
3958 +}
3959 +
3960 +public function mxchat_handle_post_delete($post_id) {
3961 + // Get post data before it's deleted
3962 + $post = get_post($post_id);
3963 +
3964 + // Basic validation
3965 + if (!$post || wp_is_post_revision($post_id)) {
3966 + return;
3967 + }
3968 +
3969 + $post_type = $post->post_type;
3970 +
3971 + // Check if sync is enabled for this post type
3972 + $should_sync = false;
3973 +
3974 + // Check built-in post types first
3975 + if ($post_type === 'post' && get_option('mxchat_auto_sync_posts') === '1') {
3976 + $should_sync = true;
3977 + } else if ($post_type === 'page' && get_option('mxchat_auto_sync_pages') === '1') {
3978 + $should_sync = true;
3979 + } else {
3980 + // Check custom post types
3981 + $option_name = 'mxchat_auto_sync_' . $post_type;
3982 + if (get_option($option_name) === '1') {
3983 + $should_sync = true;
3984 + }
3985 + }
3986 +
3987 + if (!$should_sync) {
3988 + return;
3989 + }
3990 +
3991 + // Get the URL before post is deleted
3992 + $source_url = get_permalink($post_id);
3993 + if (!$source_url) {
3994 + //error_log('MXChat: Failed to get permalink for post ' . $post_id);
3995 + return;
3996 + }
3997 +
3998 + // Check if Pinecone is enabled
3999 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4000 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4001 +
4002 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4003 + // Delete from Pinecone
4004 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
4005 + } else {
4006 + // Delete from WordPress DB
4007 + global $wpdb;
4008 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4009 +
4010 + $result = $wpdb->delete(
4011 + $table_name,
4012 + array('source_url' => $source_url),
4013 + array('%s')
4014 + );
4015 +
4016 + if ($result === false) {
4017 + //error_log('MXChat: WordPress DB deletion failed for URL: ' . $source_url);
4018 + }
4019 + }
4020 +}
4021 +
4022 +
4023 + /**
4024 + * Deletes data from Pinecone using a source URL
4025 + */
4026 + public function mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options) {
4027 + $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
4028 + $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
4029 +
4030 + if (empty($host) || empty($api_key)) {
4031 + //error_log('MXChat: Pinecone deletion failed - missing configuration');
4032 + return false;
4033 + }
4034 +
4035 + $api_endpoint = "https://{$host}/vectors/delete";
4036 + $vector_id = md5($source_url);
4037 +
4038 + $request_body = array(
4039 + 'ids' => array($vector_id)
4040 + );
4041 +
4042 + $response = wp_remote_post($api_endpoint, array(
4043 + 'headers' => array(
4044 + 'Api-Key' => $api_key,
4045 + 'accept' => 'application/json',
4046 + 'content-type' => 'application/json'
4047 + ),
4048 + 'body' => wp_json_encode($request_body),
4049 + 'timeout' => 30
4050 + ));
4051 +
4052 + if (is_wp_error($response)) {
4053 + //error_log('MXChat: Pinecone deletion error - ' . $response->get_error_message());
4054 + return false;
4055 + }
4056 +
4057 + $response_code = wp_remote_retrieve_response_code($response);
4058 + if ($response_code !== 200) {
4059 + //error_log('MXChat: Pinecone deletion failed with status ' . $response_code);
4060 + return false;
4061 + }
4062 +
4063 + return true;
4064 + }
4065 +
4066 +
4067 +
4068 +public function mxchat_handle_product_change($post_id, $post, $update) {
4069 + if ($post->post_type !== 'product') {
4070 + return;
4071 + }
4072 +
4073 + if ($post->post_status === 'publish') {
4074 + add_action('shutdown', function() use ($post_id) {
4075 + $product = wc_get_product($post_id);
4076 + if ($product) {
4077 + $this->mxchat_store_product_embedding($product);
4078 + }
4079 + });
4080 + }
4081 +}
4082 +
4083 +/**
4084 + * Store WooCommerce product embeddings
4085 + */
4086 +private function mxchat_store_product_embedding($product) {
4087 + if (!isset($this->options['enable_woocommerce_integration']) ||
4088 + !in_array($this->options['enable_woocommerce_integration'], ['1', 'on'])) {
4089 + return;
4090 + }
4091 +
4092 + $source_url = get_permalink($product->get_id());
4093 +
4094 + // Build product content
4095 + $title = $product->get_name();
4096 + $description = $product->get_description();
4097 + $short_description = $product->get_short_description();
4098 + $regular_price = $product->get_regular_price();
4099 + $sale_price = $product->get_sale_price();
4100 + $sku = $product->get_sku();
4101 +
4102 + // Format content consistently
4103 + $content = $title . "\n\n";
4104 +
4105 + if (!empty($description)) {
4106 + $content .= wp_strip_all_tags($description) . "\n\n";
4107 + }
4108 +
4109 + if (!empty($short_description)) {
4110 + $content .= "Short Description: " . wp_strip_all_tags($short_description) . "\n\n";
4111 + }
4112 +
4113 + $content .= "Price: $" . $regular_price . "\n";
4114 +
4115 + if (!empty($sale_price)) {
4116 + $content .= "Sale Price: $" . $sale_price . "\n";
4117 + }
4118 +
4119 + if (!empty($sku)) {
4120 + $content .= "SKU: " . $sku . "\n";
4121 + }
4122 +
4123 + // Get API key with proper model detection
4124 + $options = get_option('mxchat_options');
4125 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4126 +
4127 + if (strpos($selected_model, 'voyage') === 0) {
4128 + $api_key = $options['voyage_api_key'] ?? '';
4129 + } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4130 + $api_key = $options['gemini_api_key'] ?? '';
4131 + } else {
4132 + $api_key = $options['api_key'] ?? '';
4133 + }
4134 +
4135 + if (empty($api_key)) {
4136 + //error_log('MxChat Auto-sync: No API key configured for embedding model');
4137 + return;
4138 + }
4139 +
4140 + // Use the centralized utility function for storage
4141 + $result = MxChat_Utils::submit_content_to_db(
4142 + $content,
4143 + $source_url,
4144 + $api_key,
4145 + md5($source_url) // Vector ID for Pinecone
4146 + );
4147 +
4148 + if (is_wp_error($result)) {
4149 + //error_log('MxChat WooCommerce sync failed for product ' . $product->get_id() . ': ' . $result->get_error_message());
4150 + }
4151 +}
4152 +
4153 +public function mxchat_handle_product_delete($post_id) {
4154 + if (get_post_type($post_id) !== 'product') {
4155 + return;
4156 + }
4157 +
4158 + $source_url = get_permalink($post_id);
4159 +
4160 + // Check if Pinecone is enabled
4161 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4162 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4163 +
4164 + if ($use_pinecone && !empty($pinecone_options['mxchat_pinecone_api_key'])) {
4165 + // Delete from Pinecone
4166 + $this->mxchat_delete_from_pinecone_by_url($source_url, $pinecone_options);
4167 + } else {
4168 + // Delete from WordPress DB
4169 + global $wpdb;
4170 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4171 +
4172 + $wpdb->delete(
4173 + $table_name,
4174 + array('source_url' => $source_url),
4175 + array('%s')
4176 + );
4177 + }
4178 +}
4179 +
4180 +/**
4181 + * Handle individual Pinecone content deletion
4182 + */
4183 +public function mxchat_handle_pinecone_prompt_delete() {
4184 + // Check permissions
4185 + if (!current_user_can('manage_options')) {
4186 + wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
4187 + }
4188 +
4189 + // Verify nonce
4190 + if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'mxchat_delete_pinecone_prompt_nonce')) {
4191 + wp_die(esc_html__('Security check failed.', 'mxchat'));
4192 + }
4193 +
4194 + $vector_id = isset($_GET['vector_id']) ? sanitize_text_field($_GET['vector_id']) : '';
4195 +
4196 + if (empty($vector_id)) {
4197 + set_transient('mxchat_admin_notice_error',
4198 + esc_html__('Invalid vector ID.', 'mxchat'),
4199 + 30
4200 + );
4201 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
4202 + exit;
4203 + }
4204 +
4205 + // Get Pinecone settings
4206 + $pinecone_options = get_option('mxchat_pinecone_addon_options', array());
4207 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4208 +
4209 + if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
4210 + set_transient('mxchat_admin_notice_error',
4211 + esc_html__('Pinecone is not properly configured.', 'mxchat'),
4212 + 30
4213 + );
4214 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
4215 + exit;
4216 + }
4217 +
4218 + // Delete from Pinecone
4219 + $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
4220 + $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
4221 + $vector_id,
4222 + $pinecone_options['mxchat_pinecone_api_key'],
4223 + $pinecone_options['mxchat_pinecone_host']
4224 + );
4225 +
4226 + if ($result['success']) {
4227 + // No cache clearing needed since we removed caching
4228 + set_transient('mxchat_admin_notice_success',
4229 + esc_html__('Entry deleted successfully from Pinecone.', 'mxchat'),
4230 + 30
4231 + );
4232 + } else {
4233 + set_transient('mxchat_admin_notice_error',
4234 + esc_html__('Failed to delete entry: ', 'mxchat') . $result['message'],
4235 + 30
4236 + );
4237 + }
4238 +
4239 + wp_safe_redirect(admin_url('admin.php?page=mxchat-prompts'));
4240 + exit;
4241 +}
4242 +/**
4243 + * Handle individual Pinecone content deletion via AJAX
4244 + */
4245 +public function ajax_mxchat_delete_pinecone_prompt() {
4246 + // Verify nonce and permissions
4247 + if (!check_ajax_referer('mxchat_delete_pinecone_prompt_nonce', 'nonce', false)) {
4248 + wp_send_json_error('Invalid nonce');
4249 + exit;
4250 + }
4251 +
4252 + if (!current_user_can('manage_options')) {
4253 + wp_send_json_error('Unauthorized access');
4254 + exit;
4255 + }
4256 +
4257 + $vector_id = isset($_POST['vector_id']) ? sanitize_text_field($_POST['vector_id']) : '';
4258 + $bot_id = isset($_POST['bot_id']) ? sanitize_text_field($_POST['bot_id']) : 'default';
4259 +
4260 + if (empty($vector_id)) {
4261 + wp_send_json_error('Missing vector ID');
4262 + exit;
4263 + }
4264 +
4265 + // Get bot-specific Pinecone settings
4266 + $pinecone_manager = MxChat_Pinecone_Manager::get_instance();
4267 + $pinecone_options = $pinecone_manager->mxchat_get_bot_pinecone_options($bot_id);
4268 +
4269 + $use_pinecone = ($pinecone_options['mxchat_use_pinecone'] ?? '0') === '1';
4270 +
4271 + if (!$use_pinecone || empty($pinecone_options['mxchat_pinecone_api_key'])) {
4272 + wp_send_json_error('Pinecone is not properly configured for bot: ' . $bot_id);
4273 + exit;
4274 + }
4275 +
4276 + // Delete from the correct Pinecone index
4277 + $result = $pinecone_manager->mxchat_delete_from_pinecone_by_vector_id(
4278 + $vector_id,
4279 + $pinecone_options['mxchat_pinecone_api_key'],
4280 + $pinecone_options['mxchat_pinecone_host']
4281 + );
4282 +
4283 + if ($result['success']) {
4284 + // No cache clearing needed since we removed caching
4285 + wp_send_json_success(array(
4286 + 'message' => 'Entry deleted successfully from Pinecone',
4287 + 'vector_id' => $vector_id,
4288 + 'bot_id' => $bot_id
4289 + ));
4290 + } else {
4291 + wp_send_json_error('Failed to delete from Pinecone: ' . $result['message']);
4292 + }
4293 +
4294 + exit;
4295 +}
4296 +
4297 +/**
4298 + * NEW: Get hierarchical roles for dropdown
4299 + */
4300 +public function mxchat_get_role_options() {
4301 + return array(
4302 + 'public' => __('Public (Everyone)', 'mxchat'),
4303 + 'logged_in' => __('Logged In Users', 'mxchat'),
4304 + 'subscriber' => __('Subscribers & Above', 'mxchat'),
4305 + 'contributor' => __('Contributors & Above', 'mxchat'),
4306 + 'author' => __('Authors & Above', 'mxchat'),
4307 + 'editor' => __('Editors & Above', 'mxchat'),
4308 + 'administrator' => __('Administrators Only', 'mxchat')
4309 + );
4310 +}
4311 +
4312 +/**
4313 + * NEW: Check if user has access to content based on role restriction
4314 + */
4315 +public function mxchat_user_has_content_access($role_restriction) {
4316 + // Public content is always accessible
4317 + if ($role_restriction === 'public' || empty($role_restriction)) {
4318 + return true;
4319 + }
4320 +
4321 + // Check if user is logged in for logged_in restriction
4322 + if ($role_restriction === 'logged_in') {
4323 + return is_user_logged_in();
4324 + }
4325 +
4326 + // If not logged in, no access to role-restricted content
4327 + if (!is_user_logged_in()) {
4328 + return false;
4329 + }
4330 +
4331 + $user = wp_get_current_user();
4332 + $user_roles = $user->roles;
4333 +
4334 + if (empty($user_roles)) {
4335 + return false;
4336 + }
4337 +
4338 + // Define role hierarchy (higher number = higher access)
4339 + $hierarchy = array(
4340 + 'subscriber' => 1,
4341 + 'contributor' => 2,
4342 + 'author' => 3,
4343 + 'editor' => 4,
4344 + 'administrator' => 5
4345 + );
4346 +
4347 + // Get required level
4348 + $required_level = isset($hierarchy[$role_restriction]) ? $hierarchy[$role_restriction] : 0;
4349 +
4350 + // Check if user has required level or higher
4351 + foreach ($user_roles as $user_role) {
4352 + $user_level = isset($hierarchy[$user_role]) ? $hierarchy[$user_role] : 0;
4353 + if ($user_level >= $required_level) {
4354 + return true;
4355 + }
4356 + }
4357 +
4358 + return false;
4359 +}
4360 +
4361 +/**
4362 + * Handle role restriction updates via AJAX
4363 + * UPDATED: Removed cache clearing call since we removed caching
4364 + */
4365 +public function ajax_mxchat_update_role_restriction() {
4366 + // Verify nonce and permissions
4367 + if (!check_ajax_referer('mxchat_update_role_nonce', 'nonce', false)) {
4368 + wp_send_json_error('Invalid nonce');
4369 + exit;
4370 + }
4371 +
4372 + if (!current_user_can('manage_options')) {
4373 + wp_send_json_error('Unauthorized access');
4374 + exit;
4375 + }
4376 +
4377 + $entry_id = isset($_POST['entry_id']) ? sanitize_text_field($_POST['entry_id']) : '';
4378 + $role_restriction = isset($_POST['role_restriction']) ? sanitize_text_field($_POST['role_restriction']) : 'public';
4379 + $data_source = isset($_POST['data_source']) ? sanitize_text_field($_POST['data_source']) : 'wordpress';
4380 +
4381 + if (empty($entry_id)) {
4382 + wp_send_json_error('Invalid entry ID');
4383 + exit;
4384 + }
4385 +
4386 + // Get knowledge manager instance to validate role restriction
4387 + $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
4388 + $valid_roles = array_keys($knowledge_manager->mxchat_get_role_options());
4389 + if (!in_array($role_restriction, $valid_roles)) {
4390 + wp_send_json_error('Invalid role restriction');
4391 + exit;
4392 + }
4393 +
4394 + global $wpdb;
4395 +
4396 + if ($data_source === 'pinecone') {
4397 + // Handle Pinecone role restriction (stored separately in WordPress table)
4398 + $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
4399 +
4400 + // Use REPLACE to insert or update the role restriction
4401 + $result = $wpdb->replace(
4402 + $roles_table,
4403 + array(
4404 + 'vector_id' => $entry_id,
4405 + 'role_restriction' => $role_restriction,
4406 + 'updated_at' => current_time('mysql')
4407 + ),
4408 + array('%s', '%s', '%s')
4409 + );
4410 +
4411 + // No cache clearing needed since we removed caching
4412 +
4413 + } else {
4414 + // Handle WordPress database role restriction (existing functionality)
4415 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
4416 +
4417 + $result = $wpdb->update(
4418 + $table_name,
4419 + array('role_restriction' => $role_restriction),
4420 + array('id' => absint($entry_id)),
4421 + array('%s'),
4422 + array('%d')
4423 + );
4424 + }
4425 +
4426 + if ($result === false) {
4427 + wp_send_json_error('Database update failed: ' . $wpdb->last_error);
4428 + exit;
4429 + }
4430 +
4431 + wp_send_json_success(array(
4432 + 'message' => 'Role restriction updated successfully',
4433 + 'role_restriction' => $role_restriction,
4434 + 'data_source' => $data_source,
4435 + 'entry_id' => $entry_id
4436 + ));
4437 + exit;
4438 +}
4439 +
4440 + // ========================================
4441 + // HELPER METHODS
4442 + // ========================================
4443 +
4444 + /**
4445 + * Check if user has required permissions for content processing
4446 + */
4447 + private function mxchat_check_user_permissions() {
4448 + if (!current_user_can('manage_options')) {
4449 + wp_die(esc_html__('You do not have sufficient permissions.', 'mxchat'));
4450 + }
4451 + }
4452 +
4453 + /**
4454 + * Validate nonce for security
4455 + */
4456 + private function mxchat_validate_nonce($nonce_name, $nonce_action) {
4457 + if (!isset($_POST[$nonce_name]) || !wp_verify_nonce($_POST[$nonce_name], $nonce_action)) {
4458 + wp_die(esc_html__('Security check failed.', 'mxchat'));
4459 + }
4460 + }
4461 +
4462 + /**
4463 + * Get embedding API credentials
4464 + */
4465 + private function mxchat_get_embedding_credentials() {
4466 + $embedding_model = $this->options['embedding_model'] ?? 'text-embedding-ada-002';
4467 +
4468 + if (strpos($embedding_model, 'text-embedding-') !== false) {
4469 + return array(
4470 + 'type' => 'openai',
4471 + 'api_key' => $this->options['api_key'] ?? ''
4472 + );
4473 + } elseif (strpos($embedding_model, 'voyage-') !== false) {
4474 + return array(
4475 + 'type' => 'voyage',
4476 + 'api_key' => $this->options['voyage_api_key'] ?? ''
4477 + );
4478 + } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
4479 + return array(
4480 + 'type' => 'gemini',
4481 + 'api_key' => $this->options['gemini_api_key'] ?? ''
4482 + );
4483 + }
4484 +
4485 + return array('type' => 'unknown', 'api_key' => '');
4486 + }
4487 +
4488 + /**
4489 + * Log processing errors
4490 + */
4491 + private function mxchat_log_processing_error($operation, $error_message) {
4492 + //error_log("MxChat Knowledge Processing {$operation} Error: " . $error_message);
4493 + }
4494 +
4495 + /**
4496 + * Set admin notice transient
4497 + */
4498 + private function mxchat_set_admin_notice($type, $message) {
4499 + set_transient("mxchat_admin_notice_{$type}", $message, 30);
4500 + }
4501 +
4502 + /**
4503 + * Get Pinecone manager instance for vector operations
4504 + */
4505 + private function mxchat_get_pinecone_manager() {
4506 + return MxChat_Pinecone_Manager::get_instance();
4507 + }
4508 +
4509 + // ========================================
4510 + // STATIC ACCESS METHODS
4511 + // ========================================
4512 +
4513 + /**
4514 + * Get singleton instance
4515 + */
4516 + public static function get_instance() {
4517 + static $instance = null;
4518 + if ($instance === null) {
4519 + $instance = new self();
4520 + }
4521 + return $instance;
4522 + }
4523 +}
4524 +
4525 +// Initialize the Knowledge manager
9495 4526 $mxchat_knowledge_manager = MxChat_Knowledge_Manager::get_instance();