PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.25
MxChat – AI Chatbot & Content Generation for WordPress v3.2.25
3.2.25 3.2.24 3.2.23 3.2.22 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 All 156 releases
← All changes | includes/admin-knowledge-page.php +748 -142 3.2.12 → 3.2.25 View file →
@@ -73,9 +73,9 @@
73 73 <span><?php esc_html_e('Auto-Sync', 'mxchat'); ?></span>
74 74 </button>
75 75 <button class="mxch-mobile-nav-link" data-target="chunking">
76 76 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
77 - <span><?php esc_html_e('Content Chunking', 'mxchat'); ?></span>
77 + <span><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
78 78 </button>
79 79 <button class="mxch-mobile-nav-link" data-target="role-restrictions">
80 80 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
81 81 <span><?php esc_html_e('Role Restrictions', 'mxchat'); ?></span>
@@ -172,9 +172,9 @@
172 172 <button class="mxch-nav-link" data-target="chunking">
173 173 <span class="mxch-nav-link-icon">
174 174 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
175 175 </span>
176 - <span class="mxch-nav-link-text"><?php esc_html_e('Content Chunking', 'mxchat'); ?></span>
176 + <span class="mxch-nav-link-text"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
177 177 </button>
178 178 </div>
179 179
180 180 <div class="mxch-nav-item" data-section="role-restrictions">
@@ -283,8 +283,27 @@
283 283 mxchat_render_knowledge_page_scripts();
284 284 }
285 285
286 286 /**
287 + * Whether the selected bot answers from the WordPress database (plan 1f39d9).
288 + * That store is one table every bot reads. A bot has knowledge of its own only
289 + * on Pinecone (its own host or namespace) or an OpenAI Vector Store, so the
290 + * selector must not promise a per-bot knowledge base here.
291 + */
292 +function mxchat_knowledge_bot_uses_shared_database($bot_id) {
293 + $pinecone = class_exists('MxChat_Pinecone_Documents') ? MxChat_Pinecone_Documents::config($bot_id) : array();
294 + if (!empty($pinecone['use_pinecone'])) {
295 + return false;
296 + }
297 + $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
298 + $vectorstore = apply_filters('mxchat_get_bot_vectorstore_config', array(
299 + 'use_vectorstore' => (is_array($vectorstore_options) ? ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') : '0') === '1',
300 + 'vectorstore_ids' => is_array($vectorstore_options) ? ($vectorstore_options['mxchat_vectorstore_ids'] ?? '') : '',
301 + ), $bot_id);
302 + return empty($vectorstore['use_vectorstore']);
303 +}
304 +
305 +/**
287 306 * Render Multi-Bot Selector
288 307 */
289 308 function mxchat_render_knowledge_bot_selector($multibot_active, $current_bot_id) {
290 309 if (!$multibot_active || !class_exists('MxChat_Multi_Bot_Manager')) {
@@ -307,9 +326,15 @@
307 326 </option>
308 327 <?php endforeach; ?>
309 328 </select>
310 329 <span style="color: var(--mxch-text-secondary); font-size: 13px;">
311 - <?php esc_html_e('Content will be added to the selected bot\'s knowledge base', 'mxchat'); ?>
330 + <?php
331 + if (mxchat_knowledge_bot_uses_shared_database($current_bot_id)) {
332 + esc_html_e('This bot answers from the WordPress database, which is shared with all bots: content added here is available to every bot that uses it. A bot gets a knowledge base of its own on Pinecone (its own host or namespace) or an OpenAI Vector Store.', 'mxchat');
333 + } else {
334 + esc_html_e('Content will be added to the selected bot\'s knowledge base', 'mxchat');
335 + }
336 + ?>
312 337 </span>
313 338 <span id="mxchat-bot-save-status" style="display: none; color: var(--mxch-success); font-size: 13px;">
314 339 ✓ <?php esc_html_e('Saved', 'mxchat'); ?>
315 340 </span>
@@ -439,8 +464,20 @@
439 464 $required_key_type = 'Voyage AI';
440 465 } elseif (strpos($embedding_model, 'gemini-embedding-') !== false) {
441 466 $required_key_type = 'Google Gemini';
442 467 }
468 +
469 + // Custom Provider embeddings (plan 418afd): the dropdown above is disabled
470 + // on such a site and no OpenAI, Voyage or Gemini key is needed. Ask the
471 + // same pre-flight the importers ask, so this notice cannot disagree with them.
472 + $embeds_with_custom_provider = is_array($options) && isset($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on';
473 + $custom_embedding_blocker = '';
474 + if ($embeds_with_custom_provider) {
475 + $custom_preflight = MxChat_Utils::embedding_preflight($options);
476 + $has_required_key = !empty($custom_preflight['ok']);
477 + $required_key_type = 'Custom Provider';
478 + $custom_embedding_blocker = (string) ($custom_preflight['reason'] ?? '');
479 + }
443 480 ?>
444 481 <div id="import-options" class="mxch-section active">
445 482 <div class="mxch-content-header">
446 483 <h1 class="mxch-content-title"><?php esc_html_e('Import Options', 'mxchat'); ?></h1>
@@ -456,9 +493,18 @@
456 493 <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/>
457 494 <?php endif; ?>
458 495 </svg>
459 496 <div>
460 - <?php if ($has_required_key): ?>
497 + <?php if ($embeds_with_custom_provider && $has_required_key): ?>
498 + <?php
499 + /* translators: %s: the embedding model name configured on the Custom Provider */
500 + printf(esc_html__('Knowledge is embedded through your Custom Provider (model: %s). No OpenAI, Voyage AI or Google Gemini key is needed for imports.', 'mxchat'), esc_html(MxChat_Utils::resolve_custom_embedding_model($options)));
501 + ?>
502 + <?php elseif ($embeds_with_custom_provider): ?>
503 + <strong><?php esc_html_e('Important:', 'mxchat'); ?></strong>
504 + <?php echo esc_html($custom_embedding_blocker); ?>
505 + <a href="<?php echo admin_url('admin.php?page=mxchat-max#api-keys'); ?>"><?php esc_html_e('Go to API Key Settings', 'mxchat'); ?></a>
506 + <?php elseif ($has_required_key): ?>
461 507 <?php echo wp_kses_post(sprintf(__('We detected your %s API key. <strong>Remember to add credits to your %s account</strong> before using the knowledgebase.', 'mxchat'), $required_key_type, $required_key_type)); ?>
462 508 <?php else: ?>
463 509 <strong><?php esc_html_e('Important:', 'mxchat'); ?></strong>
464 510 <?php echo sprintf(esc_html__('Before importing knowledge, you must add a %s API key with sufficient credits in the Chatbot settings.', 'mxchat'), $required_key_type); ?>
@@ -555,8 +601,30 @@
555 601 <h4><?php esc_html_e('PDF Upload', 'mxchat'); ?></h4>
556 602 <p><?php esc_html_e('Upload a PDF file from your computer.', 'mxchat'); ?></p>
557 603 </div>
558 604 </button>
605 +
606 + <!-- Document File Upload Option (plan 0485e5) -->
607 + <button type="button" class="mxchat-import-box" data-option="document-upload" data-type="document-upload">
608 + <div class="mxchat-import-icon">
609 + <span class="dashicons dashicons-media-text"></span>
610 + </div>
611 + <div class="mxchat-import-content">
612 + <h4><?php esc_html_e('Document Upload', 'mxchat'); ?></h4>
613 + <p><?php esc_html_e('Upload a Word document, text, or Markdown file (.docx, .txt, .md).', 'mxchat'); ?></p>
614 + </div>
615 + </button>
616 +
617 + <!-- YouTube Video Import Option -->
618 + <button type="button" class="mxchat-import-box" data-option="youtube" data-type="youtube">
619 + <div class="mxchat-import-icon">
620 + <span class="dashicons dashicons-youtube"></span>
621 + </div>
622 + <div class="mxchat-import-content">
623 + <h4><?php esc_html_e('YouTube', 'mxchat'); ?></h4>
624 + <p><?php esc_html_e('Index a video so the chat can surface it as a playable embed.', 'mxchat'); ?></p>
625 + </div>
626 + </button>
559 627 </div>
560 628
561 629 <!-- Detected Sitemaps Section (hidden by default, shown when Sitemap Import is clicked) -->
562 630 <div id="mxchat-detected-sitemaps" class="mxchat-detected-sitemaps" style="display: none;">
@@ -653,13 +721,89 @@
653 721 </div>
654 722 <p class="mxch-field-description">
655 723 <?php esc_html_e('Select a PDF file from your computer to import into the knowledge base. Maximum file size depends on your server settings.', 'mxchat'); ?>
656 724 </p>
725 + <div class="mxch-field">
726 + <input type="url" name="source_page_url" id="mxchat-pdf-source-url" class="mxch-input" placeholder="<?php esc_attr_e('Enter source URL (Optional)', 'mxchat'); ?>">
727 + <p class="mxch-field-description">
728 + <?php esc_html_e('The page this PDF lives on. Answers drawn from it link to that page. Leave empty and answers name the file and page without a link.', 'mxchat'); ?>
729 + </p>
730 + </div>
657 731 <button type="submit" name="submit_pdf_file" class="mxch-btn mxch-btn-primary">
658 732 <?php esc_html_e('Import PDF', 'mxchat'); ?>
659 733 </button>
660 734 </form>
661 735 </div>
736 +
737 + <div class="mxchat-import-input-area" id="mxchat-document-upload-area" style="display: none; margin-top: 20px;">
738 + <form id="mxchat-document-upload-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_document_file')); ?>" enctype="multipart/form-data">
739 + <?php wp_nonce_field('mxchat_submit_document_file_action', 'mxchat_submit_document_file_nonce'); ?>
740 + <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
741 + <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
742 + <?php endif; ?>
743 + <div class="mxch-field">
744 + <input type="file" name="document_file" id="mxchat-document-file-input" accept=".docx,.txt,.md" required>
745 + </div>
746 + <p class="mxch-field-description">
747 + <?php esc_html_e('Select a .docx, .txt, or .md file to import into the knowledge base. The text is extracted and indexed; the file itself is not stored. Re-uploading a file with the same name replaces its previous content.', 'mxchat'); ?>
748 + </p>
749 + <div class="mxch-field">
750 + <input type="url" name="source_page_url" id="mxchat-document-source-url" class="mxch-input" placeholder="<?php esc_attr_e('Enter source URL (Optional)', 'mxchat'); ?>">
751 + <p class="mxch-field-description">
752 + <?php esc_html_e('The page this document lives on. Answers drawn from it link to that page. Leave empty and answers name the file without a link.', 'mxchat'); ?>
753 + </p>
754 + </div>
755 + <button type="submit" name="submit_document_file" class="mxch-btn mxch-btn-primary">
756 + <?php esc_html_e('Import Document', 'mxchat'); ?>
757 + </button>
758 + </form>
759 + </div>
760 +
761 + <?php
762 + // Prefill state: set when an auto-import found no transcript — the
763 + // handler bounced back here so the admin can augment the entry with
764 + // a manual description (re-importing the same URL updates the row).
765 + $yt_prefill = isset($_GET['mxchat_yt_prefill']) && $_GET['mxchat_yt_prefill'] === '1';
766 + $yt_prefill_url = $yt_prefill && isset($_GET['yt_url']) ? esc_url_raw(rawurldecode(wp_unslash($_GET['yt_url']))) : '';
767 + $yt_prefill_title = $yt_prefill && isset($_GET['yt_title']) ? sanitize_text_field(rawurldecode(wp_unslash($_GET['yt_title']))) : '';
768 + ?>
769 + <div class="mxchat-import-input-area" id="mxchat-youtube-input-area" style="display: none; margin-top: 20px;">
770 + <form id="mxchat-youtube-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_youtube')); ?>"<?php echo $yt_prefill ? ' data-yt-prefill="1"' : ''; ?>>
771 + <?php wp_nonce_field('mxchat_submit_youtube_action', 'mxchat_submit_youtube_nonce'); ?>
772 + <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
773 + <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
774 + <?php endif; ?>
775 + <div class="mxch-field">
776 + <input type="url" name="youtube_url" id="mxchat-youtube-url" class="mxch-input" placeholder="<?php esc_attr_e('Paste a YouTube video URL (watch, youtu.be, or Shorts link)', 'mxchat'); ?>" required value="<?php echo esc_attr($yt_prefill_url); ?>">
777 + </div>
778 + <div class="mxch-field">
779 + <span class="mxch-field-label"><?php esc_html_e('How should this video be described for matching?', 'mxchat'); ?></span>
780 + <div style="display: flex; flex-direction: column; gap: 6px; margin-top: 8px;">
781 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
782 + <input type="radio" name="youtube_description_mode" value="auto" <?php checked(!$yt_prefill); ?>>
783 + <span>
784 + <strong><?php esc_html_e('Auto-fetch', 'mxchat'); ?></strong>
785 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('We pull the video\'s title, channel, and captions (when available) to describe it.', 'mxchat'); ?></span>
786 + </span>
787 + </label>
788 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
789 + <input type="radio" name="youtube_description_mode" value="manual" <?php checked($yt_prefill); ?>>
790 + <span>
791 + <strong><?php esc_html_e('Write my own', 'mxchat'); ?></strong>
792 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('You write the description the chatbot matches against — best for curated guides.', 'mxchat'); ?></span>
793 + </span>
794 + </label>
795 + </div>
796 + </div>
797 + <div class="mxch-field" id="mxchat-youtube-description-field" style="<?php echo $yt_prefill ? '' : 'display: none;'; ?>">
798 + <textarea name="youtube_description" id="mxchat-youtube-description" class="mxch-textarea" rows="5" placeholder="<?php esc_attr_e('Describe what this video covers and the questions it answers...', 'mxchat'); ?>"><?php echo esc_textarea($yt_prefill_title !== '' ? $yt_prefill_title . "\n\n" : ''); ?></textarea>
799 + <p class="mxch-field-description"><?php esc_html_e('This text is what gets matched against visitor questions. Importing the same video URL again updates its existing entry.', 'mxchat'); ?></p>
800 + </div>
801 + <button type="submit" name="submit_youtube" value="1" class="mxch-btn mxch-btn-primary">
802 + <?php esc_html_e('Import Video', 'mxchat'); ?>
803 + </button>
804 + </form>
805 + </div>
662 806 <?php endif; ?>
663 807 </div>
664 808 </div>
665 809 </div>
@@ -810,9 +954,10 @@
810 954 <div class="mxch-card">
811 955 <div class="mxch-card-header">
812 956 <h3 class="mxch-card-title">
813 957 <?php esc_html_e('Knowledge Entries', 'mxchat'); ?>
814 - <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);">(<?php echo esc_html($total_records); ?>)</span>
958 + <?php $count_unit = $count_unit ?? ''; ?>
959 + <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);" data-count="<?php echo esc_attr($total_records); ?>" data-unit="<?php echo esc_attr($count_unit); ?>"<?php if ($count_unit === 'vectors') : ?> title="<?php esc_attr_e('Pinecone reports vectors, not documents; a long entry is stored as several vectors.', 'mxchat'); ?>"<?php endif; ?>>(<?php echo esc_html(number_format_i18n($total_records)); ?><?php if ($count_unit === 'vectors') : ?> <?php esc_html_e('vectors', 'mxchat'); ?><?php endif; ?>)</span>
815 960 <?php if (!empty($use_vectorstore)) : ?>
816 961 <span style="display: inline-flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; margin-left: 8px; background: #fff3e0; color: #e65100;">
817 962 <span class="dashicons dashicons-database" style="font-size: 14px;"></span>
818 963 <?php esc_html_e('Not in use', 'mxchat'); ?>
@@ -982,16 +1127,9 @@
982 1127 </span>
983 1128 </div>
984 1129 </td>
985 1130 <td class="mxchat-url-cell" style="padding: 12px 16px; font-size: 13px;">
986 - <?php if (!empty($source_url) && strpos($source_url, 'mxchat://') !== 0 && strpos($source_url, '_ungrouped_') !== 0) : ?>
987 - <a href="<?php echo esc_url($source_url); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
988 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
989 - <?php esc_html_e('View Source', 'mxchat'); ?>
990 - </a>
991 - <?php else : ?>
992 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
993 - <?php endif; ?>
1131 + <?php echo MxChat_Knowledge_Manager::mxchat_source_cell_html($source_url, $current_bot_id, true); ?>
994 1132 </td>
995 1133 <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
996 1134 <button type="button"
997 1135 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
@@ -1002,9 +1140,8 @@
1002 1140 data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1003 1141 title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1004 1142 <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1005 1143 </button>
1006 - <?php if ($data_source !== 'pinecone') : ?>
1007 1144 <button type="button"
1008 1145 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1009 1146 data-source-url="<?php echo esc_attr($source_url); ?>"
1010 1147 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
@@ -1013,9 +1150,8 @@
1013 1150 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1014 1151 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1015 1152 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1016 1153 </button>
1017 - <?php endif; ?>
1018 1154 <button type="button"
1019 1155 class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
1020 1156 data-source-url="<?php echo esc_attr($source_url); ?>"
1021 1157 data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
@@ -1142,16 +1278,10 @@
1142 1278 $actual_source = $source_url;
1143 1279 if (strpos($source_url, '_ungrouped_') === 0) {
1144 1280 $actual_source = $prompt->source_url ?? '';
1145 1281 }
1146 - if (!empty($actual_source) && strpos($actual_source, 'mxchat://') !== 0) : ?>
1147 - <a href="<?php echo esc_url($actual_source); ?>" target="_blank" style="color: var(--mxch-primary); text-decoration: none;">
1148 - <span class="dashicons dashicons-external" style="font-size: 14px;"></span>
1149 - <?php esc_html_e('View', 'mxchat'); ?>
1150 - </a>
1151 - <?php else : ?>
1152 - <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
1153 - <?php endif; ?>
1282 + echo MxChat_Knowledge_Manager::mxchat_source_cell_html($actual_source, $current_bot_id, false);
1283 + ?>
1154 1284 </td>
1155 1285 <td style="padding: 12px 16px; white-space: nowrap;">
1156 1286 <button type="button"
1157 1287 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
@@ -1162,9 +1292,8 @@
1162 1292 data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1163 1293 title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1164 1294 <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1165 1295 </button>
1166 - <?php if ($data_source !== 'pinecone') : ?>
1167 1296 <button type="button"
1168 1297 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1169 1298 data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1170 1299 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
@@ -1173,9 +1302,8 @@
1173 1302 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1174 1303 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1175 1304 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1176 1305 </button>
1177 - <?php endif; ?>
1178 1306 <?php if ($data_source === 'pinecone') : ?>
1179 1307 <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);">
1180 1308 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1181 1309 </button>
@@ -1193,10 +1321,21 @@
1193 1321 </table>
1194 1322 </div>
1195 1323
1196 1324 <!-- Pagination wrapper - always present so JS can populate it after processing -->
1197 - <div id="mxchat-kb-pagination" class="mxchat-kb-pagination-wrapper" style="<?php echo $total_pages > 1 ? 'padding: 16px; border-top: 1px solid var(--mxch-card-border); text-align: center;' : ''; ?>" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>" data-search="<?php echo esc_attr($search_query); ?>" data-content-type="<?php echo esc_attr($content_type_filter); ?>">
1198 - <?php if ($total_pages > 1) : ?>
1325 + <?php
1326 + // Plan dd6e10: Pinecone's list endpoint is a cursor — there is no
1327 + // random access to page N, so over 500 vectors the screen offers
1328 + // Previous / Next only. The same markup is built for AJAX page
1329 + // loads by MxChat_Knowledge_Manager::mxchat_cursor_pagination_html().
1330 + $cursor_pagination = !empty($cursor_pagination);
1331 + $has_next = !empty($has_next);
1332 + $show_pagination = $cursor_pagination ? ($current_page > 1 || $has_next) : ($total_pages > 1);
1333 + ?>
1334 + <div id="mxchat-kb-pagination" class="mxchat-kb-pagination-wrapper" style="<?php echo $show_pagination ? 'padding: 16px; border-top: 1px solid var(--mxch-card-border); text-align: center;' : ''; ?>" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>" data-search="<?php echo esc_attr($search_query); ?>" data-content-type="<?php echo esc_attr($content_type_filter); ?>">
1335 + <?php if ($cursor_pagination) : ?>
1336 + <?php echo MxChat_Knowledge_Manager::mxchat_cursor_pagination_html($current_page, $has_next, $search_query, $content_type_filter); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped inside ?>
1337 + <?php elseif ($total_pages > 1) : ?>
1199 1338 <div class="mxchat-ajax-pagination" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>">
1200 1339 <?php if ($current_page > 1) : ?>
1201 1340 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page - 1); ?>"><?php esc_html_e('&laquo; Previous', 'mxchat'); ?></a>
1202 1341 <?php endif; ?>
@@ -1235,13 +1374,97 @@
1235 1374 <?php endif; ?>
1236 1375 </div>
1237 1376 </div>
1238 1377 </div>
1378 +
1379 + <?php mxchat_render_kb_reimport_card(!empty($current_bot_id) ? $current_bot_id : 'default'); ?>
1239 1380 </div>
1240 1381 <?php
1241 1382 }
1242 1383
1243 1384 /**
1385 + * "Re-import affected entries" card (plan 89739b).
1386 + *
1387 + * The owner-facing side of wp mxchat kb-reimport: check first (no embedding
1388 + * request, nothing written), read the list, then confirm. Both halves run in
1389 + * resumable steps through the mxchat_kb_reimport AJAX action; the script is
1390 + * js/knowledge-reimport.js. Everything dynamic is drawn there; this is the
1391 + * resting markup.
1392 + */
1393 +function mxchat_render_kb_reimport_card($current_bot_id = 'default') {
1394 + if (!class_exists('MxChat_Knowledge_Manager')) {
1395 + return;
1396 + }
1397 + ?>
1398 + <div class="mxch-card mxch-kb-reimport" id="mxchat-kb-reimport"
1399 + data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_kb_reimport')); ?>"
1400 + data-bot="<?php echo esc_attr($current_bot_id); ?>">
1401 + <div class="mxch-card-header">
1402 + <h3 class="mxch-card-title">
1403 + <svg class="mxch-card-title-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>
1404 + <?php esc_html_e('Re-import affected entries', 'mxchat'); ?>
1405 + </h3>
1406 + </div>
1407 + <div class="mxch-card-body">
1408 + <p class="mxch-field-description"><?php esc_html_e('Earlier versions stored some imported text incorrectly: currency signs and accents were dropped, HTML character codes were left in, and right-to-left PDF text was saved backwards. This finds the entries that still hold that text and rebuilds them from their source. Checking changes nothing.', 'mxchat'); ?></p>
1409 +
1410 + <div class="mxch-kb-reimport-controls">
1411 + <div class="mxch-field">
1412 + <label class="mxch-field-label" for="mxchat-kb-reimport-before"><?php esc_html_e('Entries last written before', 'mxchat'); ?></label>
1413 + <input type="date" id="mxchat-kb-reimport-before" class="mxch-input mxch-input-sm" value="<?php echo esc_attr(MxChat_Knowledge_Manager::REIMPORT_DEFAULT_BEFORE); ?>" max="<?php echo esc_attr(gmdate('Y-m-d', time() + DAY_IN_SECONDS)); ?>" />
1414 + </div>
1415 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-kb-reimport-check"><?php esc_html_e('Check knowledge base', 'mxchat'); ?></button>
1416 + </div>
1417 + <p class="mxch-field-hint"><?php esc_html_e('The date is the release that carried the last of these fixes. Choose a later one if this site was updated after that.', 'mxchat'); ?></p>
1418 +
1419 + <!-- Also offer entries whose page was edited after they were imported (plan a00ef8); off by default -->
1420 + <div class="mxch-field mxch-kb-reimport-edited">
1421 + <label class="toggle-switch">
1422 + <input type="checkbox" id="mxchat-kb-reimport-edited" name="mxchat_kb_reimport_edited" value="1" />
1423 + <span class="slider"></span>
1424 + </label>
1425 + <div class="mxch-kb-reimport-edited-text">
1426 + <label class="mxch-field-label" for="mxchat-kb-reimport-edited"><?php esc_html_e('Also rewrite entries whose page was edited after they were imported', 'mxchat'); ?></label>
1427 + <p class="mxch-field-description"><?php esc_html_e('An entry older than its post, page, product or media file is rebuilt from it as it reads today. A site with auto-sync on does not need this.', 'mxchat'); ?></p>
1428 + </div>
1429 + </div>
1430 +
1431 + <div class="mxch-kb-reimport-progress" hidden>
1432 + <div class="mxch-progress-bar" hidden><div class="mxch-progress-bar-fill"></div></div>
1433 + <p class="mxch-progress-label" aria-live="polite"></p>
1434 + </div>
1435 +
1436 + <div class="mxch-notice mxch-notice-error mxch-kb-reimport-error" role="alert" hidden>
1437 + <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
1438 + <span></span>
1439 + </div>
1440 +
1441 + <div class="mxch-kb-reimport-results" hidden>
1442 + <div class="mxch-notice mxch-notice-success mxch-kb-reimport-done" hidden>
1443 + <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
1444 + <span></span>
1445 + </div>
1446 + <div class="mxch-kb-reimport-counts"></div>
1447 + <ul class="mxch-kb-reimport-list mxch-kb-reimport-entries" hidden></ul>
1448 + <p class="mxch-field-hint mxch-kb-reimport-more" hidden></p>
1449 + <details class="mxch-disclosure mxch-kb-reimport-others" hidden>
1450 + <summary class="mxch-disclosure-summary"><span class="mxch-disclosure-summary-text"></span><span class="mxch-disclosure-summary-hint"><?php esc_html_e('Never changed or removed by this tool', 'mxchat'); ?></span></summary>
1451 + <div class="mxch-disclosure-body">
1452 + <ul class="mxch-kb-reimport-list mxch-kb-reimport-notes"></ul>
1453 + </div>
1454 + </details>
1455 + </div>
1456 + </div>
1457 + <div class="mxch-card-footer mxch-kb-reimport-footer" hidden>
1458 + <button type="button" class="mxch-btn mxch-btn-primary" id="mxchat-kb-reimport-apply"></button>
1459 + <button type="button" class="mxch-btn mxch-btn-ghost" id="mxchat-kb-reimport-clear"><?php esc_html_e('Clear', 'mxchat'); ?></button>
1460 + <span class="mxch-field-hint mxch-kb-reimport-cost"><?php esc_html_e('Each rewritten entry is embedded again with your embedding provider.', 'mxchat'); ?></span>
1461 + </div>
1462 + </div>
1463 + <?php
1464 +}
1465 +
1466 +/**
1244 1467 * Render Auto-Sync Section
1245 1468 */
1246 1469 function mxchat_render_auto_sync_section($knowledge_manager) {
1247 1470 ?>
@@ -1327,13 +1550,16 @@
1327 1550 $chunk_size = $chunking_settings['chunk_size'];
1328 1551 ?>
1329 1552 <div id="chunking" class="mxch-section">
1330 1553 <div class="mxch-content-header">
1331 - <h1 class="mxch-content-title"><?php esc_html_e('Content Chunking', 'mxchat'); ?></h1>
1332 - <p class="mxch-content-subtitle"><?php esc_html_e('Split large content into smaller segments for more accurate semantic search.', 'mxchat'); ?></p>
1554 + <h1 class="mxch-content-title"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></h1>
1555 + <p class="mxch-content-subtitle"><?php esc_html_e('Control how content is split for indexing and how the knowledge base is searched.', 'mxchat'); ?></p>
1333 1556 </div>
1334 1557
1335 1558 <div class="mxch-card">
1559 + <div class="mxch-card-header">
1560 + <h2 class="mxch-card-title"><?php esc_html_e('Chunking', 'mxchat'); ?></h2>
1561 + </div>
1336 1562 <div class="mxch-card-body mxchat-autosave-section">
1337 1563 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1338 1564 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1339 1565 <span><?php esc_html_e('Chunking improves retrieval quality for long documents. All chunks are reassembled before sending to the AI. Only applies to new submissions.', 'mxchat'); ?></span>
@@ -1355,8 +1581,83 @@
1355 1581 <p class="mxch-field-description"><?php esc_html_e('Recommended: 4000 characters (~1000 tokens). Range: 1000-10000. Larger chunks preserve more context.', 'mxchat'); ?></p>
1356 1582 </div>
1357 1583 </div>
1358 1584 </div>
1585 +
1586 + <?php
1587 + // Retrieval tuning (plan 11720c) — the hybrid keyword boost moved here
1588 + // from Settings → Behavior: it is KB retrieval tuning, so it lives with
1589 + // its natural siblings. SAME option key and save transport as before —
1590 + // the name routes to the mxchat_save_setting handler, whose
1591 + // mxchat_hybrid_keyword_toggle case still runs capability detection.
1592 + ?>
1593 + <div class="mxch-card">
1594 + <div class="mxch-card-header">
1595 + <h2 class="mxch-card-title"><?php esc_html_e('Retrieval', 'mxchat'); ?></h2>
1596 + </div>
1597 + <div class="mxch-card-body mxchat-autosave-section">
1598 + <div class="mxch-field">
1599 + <div style="display: flex; align-items: center; gap: 12px;">
1600 + <label class="mxchat-toggle-switch">
1601 + <input type="checkbox" name="mxchat_hybrid_keyword_toggle" id="mxchat_hybrid_keyword_toggle" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_hybrid_keyword_toggle', 'off'), 'on'); ?>>
1602 + <span class="mxchat-toggle-slider"></span>
1603 + </label>
1604 + <span style="font-weight: 500;"><?php esc_html_e('Hybrid keyword boost (WordPress database and Pinecone document indexes)', 'mxchat'); ?></span>
1605 + </div>
1606 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Combines keyword matching with vector similarity when searching your WordPress-database knowledge base or a Pinecone document index. Helps exact-token questions — product codes, SKUs, error codes, names — find the right document even when semantic similarity alone would miss it.', 'mxchat'); ?></p>
1607 + <p class="mxch-field-hint"><?php esc_html_e('Applies to the WordPress-database knowledge base and to Pinecone when the index type is Document index with full-text search; a classic Pinecone vector index is unaffected. Changes which sources are retrieved on existing installs, so test after enabling. The Transcripts sources panel labels how each match was found while this is on.', 'mxchat'); ?></p>
1608 + </div>
1609 + </div>
1610 + </div>
1611 +
1612 + <?php
1613 + // Video cards (plan f52492). Lives on the Knowledge screen because that
1614 + // is where the owner imported the video in the first place, and beside
1615 + // Retrieval because it is a retrieval-confidence question. Standalone
1616 + // options — see the mxchat_video_embed_* cases in class-ajax-handler.php.
1617 + $video_embed_enabled = MxChat_Utils::video_embed_enabled();
1618 + $video_embed_threshold = (int) round(MxChat_Utils::video_embed_threshold() * 100);
1619 + $mxchat_core_options = get_option('mxchat_options', array());
1620 + $site_threshold = isset($mxchat_core_options['similarity_threshold'])
1621 + ? (int) $mxchat_core_options['similarity_threshold']
1622 + : 35;
1623 + ?>
1624 + <div class="mxch-card">
1625 + <div class="mxch-card-header">
1626 + <h2 class="mxch-card-title"><?php esc_html_e('Video Cards', 'mxchat'); ?></h2>
1627 + </div>
1628 + <div class="mxch-card-body mxchat-autosave-section">
1629 + <div class="mxch-field" style="margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--mxch-card-border);">
1630 + <div style="display: flex; align-items: center; gap: 12px;">
1631 + <label class="mxchat-toggle-switch">
1632 + <input type="checkbox" name="mxchat_video_embed_enabled" id="mxchat_video_embed_enabled" class="mxchat-autosave-field" value="on" <?php checked($video_embed_enabled, true); ?>>
1633 + <span class="mxchat-toggle-slider"></span>
1634 + </label>
1635 + <span style="font-weight: 500;"><?php esc_html_e('Show a video card when a YouTube source answers the question', 'mxchat'); ?></span>
1636 + </div>
1637 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Adds a click-to-play card beneath the reply when the answer came from a YouTube video you imported. Turn this off to stop video cards entirely without removing the videos from your knowledge base.', 'mxchat'); ?></p>
1638 + </div>
1639 +
1640 + <div class="mxch-field" id="mxchat-video-embed-threshold-field" style="<?php echo $video_embed_enabled ? '' : 'display: none;'; ?>">
1641 + <label class="mxch-field-label" for="mxchat_video_embed_threshold"><?php esc_html_e('Video Card Match Threshold (%)', 'mxchat'); ?></label>
1642 + <?php // Slider matching the site-wide Similarity Threshold exactly
1643 + // (plan 7afd54): same .slider-container / .range-slider /
1644 + // .range-value markup; the shared admin JS live-updates the
1645 + // <id>_value span on input and autosaves on change (once per
1646 + // drag release, so no request storm). Range/default/clamp
1647 + // unchanged — presentation only. ?>
1648 + <div class="slider-container">
1649 + <input type="range" name="mxchat_video_embed_threshold" id="mxchat_video_embed_threshold" class="range-slider mxchat-autosave-field" value="<?php echo esc_attr($video_embed_threshold); ?>" min="20" max="95" step="1">
1650 + <span id="mxchat_video_embed_threshold_value" class="range-value"><?php echo esc_html((int) $video_embed_threshold); ?></span>
1651 + </div>
1652 + <p class="mxch-field-description"><?php
1653 + /* translators: %d is the site-wide Similarity Threshold percentage. */
1654 + printf(esc_html__('How closely the video must match the question before its card is shown. Higher means fewer, more relevant cards. This is separate from the site-wide Similarity Threshold (currently %d%%), which decides what the AI reads — a video is worth reading from at a lower score than it is worth putting on screen.', 'mxchat'), (int) $site_threshold);
1655 + ?></p>
1656 + <p class="mxch-field-hint"><?php esc_html_e('Range 20-95. Raise this if visitors are shown videos unrelated to what they asked; lower it if a video that clearly answers the question is not appearing. The right number depends on your embedding model: older models such as text-embedding-ada-002 score even unrelated content around 70%, so those sites usually need 80 or higher before this has any effect. The Transcripts sources panel shows the match percentage of each source, which is the number to compare against.', 'mxchat'); ?></p>
1657 + </div>
1658 + </div>
1659 + </div>
1359 1660 </div>
1360 1661 <?php
1361 1662 }
1362 1663
@@ -1363,22 +1664,22 @@
1363 1664 /**
1364 1665 * Render Role Restrictions Section
1365 1666 */
1366 1667 function mxchat_render_role_restrictions_section($knowledge_manager) {
1367 - $role_options = array(
1368 - 'public' => __('Public (Everyone)', 'mxchat'),
1369 - 'logged_in' => __('Logged In Users', 'mxchat'),
1370 - 'subscriber' => __('Subscribers & Above', 'mxchat'),
1371 - 'contributor' => __('Contributors & Above', 'mxchat'),
1372 - 'author' => __('Authors & Above', 'mxchat'),
1373 - 'editor' => __('Editors & Above', 'mxchat'),
1374 - 'administrator' => __('Administrators Only', 'mxchat')
1375 - );
1668 + // Same source as the per-entry dropdown, so the two selectors cannot drift
1669 + // and both offer the site's custom roles (plan 6ab002). This screen used to
1670 + // hold its own hardcoded copy of the five built-ins.
1671 + $role_options = (is_object($knowledge_manager) && method_exists($knowledge_manager, 'mxchat_get_role_options'))
1672 + ? $knowledge_manager->mxchat_get_role_options()
1673 + : array(
1674 + 'public' => __('Public (Everyone)', 'mxchat'),
1675 + 'logged_in' => __('Logged In Users', 'mxchat'),
1676 + );
1376 1677 ?>
1377 1678 <div id="role-restrictions" class="mxch-section">
1378 1679 <div class="mxch-content-header">
1379 1680 <h1 class="mxch-content-title"><?php esc_html_e('Role-Based Content Restrictions', 'mxchat'); ?></h1>
1380 - <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict content access based on WordPress tags.', 'mxchat'); ?></p>
1681 + <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict tagged posts by role. Pages and untagged content are restricted per entry in the Knowledge list.', 'mxchat'); ?></p>
1381 1682 </div>
1382 1683
1383 1684 <div class="mxch-card">
1384 1685 <div class="mxch-card-body">
@@ -1383,9 +1684,9 @@
1383 1684 <div class="mxch-card">
1384 1685 <div class="mxch-card-body">
1385 1686 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1386 1687 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1387 - <span><strong><?php esc_html_e('How it works:', 'mxchat'); ?></strong> <?php esc_html_e('Add a tag below and select which role should have access. Content with that tag will be restricted to that role level.', 'mxchat'); ?></span>
1688 + <span><strong><?php esc_html_e('How it works:', 'mxchat'); ?></strong> <?php esc_html_e('Add a tag below and select which role should have access. Content with that tag will be restricted to that role level. Tags apply to posts — pages and other content have no tags, so restrict those from the role dropdown on the entry itself in the Knowledge list. Any role on this site can be chosen, including custom ones.', 'mxchat'); ?></span>
1388 1689 </div>
1389 1690
1390 1691 <h4 style="margin: 0 0 16px 0;"><?php esc_html_e('Add Tag-Role Mapping', 'mxchat'); ?></h4>
1391 1692 <div style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 24px;">
@@ -1456,8 +1757,23 @@
1456 1757 </div>
1457 1758
1458 1759 <div class="mxch-card">
1459 1760 <div class="mxch-card-body mxchat-autosave-section">
1761 + <?php
1762 + // Install-level ACF→PDF import extraction (plan 11720c) — moved
1763 + // here from a per-import checkbox in the content-selector modal.
1764 + // Default OFF, matching its own "recommended only if…" guidance.
1765 + ?>
1766 + <div class="mxch-field" style="margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--mxch-card-border);">
1767 + <div style="display: flex; align-items: center; gap: 12px;">
1768 + <label class="mxchat-toggle-switch">
1769 + <input type="checkbox" name="mxchat_acf_pdf_extraction" id="mxchat_acf_pdf_extraction" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_acf_pdf_extraction', '0'), '1'); ?>>
1770 + <span class="mxchat-toggle-slider"></span>
1771 + </label>
1772 + <span style="font-weight: 500;"><?php esc_html_e('Extract text from PDFs linked in ACF File/Image fields during imports', 'mxchat'); ?></span>
1773 + </div>
1774 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Applies to WordPress Content imports. Adds 200-500ms per post and ~30 KB to each knowledge base entry. Recommended only if your ACF setup uses PDF attachments for primary content. Editor-save extraction has its own toggle under Auto-Sync.', 'mxchat'); ?></p>
1775 + </div>
1460 1776 <?php if (!function_exists('acf_get_field_groups')): ?>
1461 1777 <div class="mxch-notice mxch-notice-info">
1462 1778 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1463 1779 <span><?php esc_html_e('Advanced Custom Fields (ACF) plugin is not detected. Install and activate ACF to use this feature.', 'mxchat'); ?></span>
@@ -1472,30 +1788,55 @@
1472 1788 </div>
1473 1789 <?php else: ?>
1474 1790 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1475 1791 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1476 - <span><?php esc_html_e('Toggle fields ON to include them in knowledge base embeddings. Toggle OFF to exclude sensitive or irrelevant fields.', 'mxchat'); ?></span>
1792 + <span><?php esc_html_e('Toggle fields ON to include them in knowledge base embeddings. Toggle OFF to exclude sensitive or irrelevant fields. Exclusions apply everywhere a field appears — ACF location rules (post types, templates) do not scope this list.', 'mxchat'); ?></span>
1477 1793 </div>
1478 1794
1479 - <?php foreach ($all_acf_fields as $group_title => $fields): ?>
1480 - <div class="mxchat-acf-field-group" style="margin-bottom: 24px;">
1481 - <h4 style="margin: 0 0 12px 0; color: var(--mxch-text-primary); font-weight: 600;">
1482 - <?php echo esc_html($group_title); ?>
1483 - </h4>
1795 + <?php foreach ($all_acf_fields as $group_key => $group):
1796 + $fields = $group['fields'];
1797 + // Group toggle state derives from the per-field state —
1798 + // never stored on its own (plan bf57e0).
1799 + $excluded_count = 0;
1800 + foreach ($fields as $field) {
1801 + if (in_array($field['key'], $excluded_fields, true) || in_array($field['name'], $excluded_fields, true)) {
1802 + $excluded_count++;
1803 + }
1804 + }
1805 + $group_all_included = ($excluded_count === 0);
1806 + $group_partial = ($excluded_count > 0 && $excluded_count < count($fields));
1807 + ?>
1808 + <div class="mxchat-acf-field-group" style="margin-bottom: 24px;" data-mxchat-acf-group="<?php echo esc_attr($group_key); ?>">
1809 + <div style="display: flex; align-items: center; justify-content: space-between; margin: 0 0 12px 0;">
1810 + <h4 style="margin: 0; color: var(--mxch-text-primary); font-weight: 600;">
1811 + <?php echo esc_html($group['title']); ?>
1812 + </h4>
1813 + <label class="mxchat-toggle-switch" title="<?php esc_attr_e('Include or exclude every field in this group', 'mxchat'); ?>">
1814 + <input type="checkbox"
1815 + class="mxchat-acf-group-toggle"
1816 + data-group-key="<?php echo esc_attr($group_key); ?>"
1817 + data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1818 + <?php if ($group_partial): ?>data-indeterminate="1"<?php endif; ?>
1819 + <?php checked($group_all_included, true); ?>>
1820 + <span class="mxchat-toggle-slider"></span>
1821 + </label>
1822 + </div>
1484 1823 <div style="background: #f8fafc; border-radius: var(--mxch-radius-md); padding: 16px;">
1485 1824 <?php foreach ($fields as $field):
1486 - $field_name = $field['name'];
1487 - $is_enabled = !in_array($field_name, $excluded_fields);
1825 + // Toggles identify fields by ACF field KEY — names are shared
1826 + // across groups and collide (plan 30e81f). Legacy name entries
1827 + // (group inactive at migration time) still count as excluded.
1828 + $is_enabled = !in_array($field['key'], $excluded_fields, true) && !in_array($field['name'], $excluded_fields, true);
1488 1829 ?>
1489 1830 <div style="display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #e2e8f0;">
1490 1831 <div>
1491 1832 <span style="font-weight: 500;"><?php echo esc_html($field['label']); ?></span>
1492 - <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field_name); ?>)</span>
1833 + <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field['name']); ?>)</span>
1493 1834 <span style="color: var(--mxch-text-muted); font-size: 11px; margin-left: 8px; background: #e2e8f0; padding: 2px 6px; border-radius: 4px;"><?php echo esc_html($field['type']); ?></span>
1494 1835 </div>
1495 1836 <label class="mxchat-toggle-switch">
1496 1837 <input type="checkbox"
1497 - name="mxchat_acf_field_<?php echo esc_attr($field_name); ?>"
1838 + name="mxchat_acf_field_<?php echo esc_attr($field['key']); ?>"
1498 1839 class="mxchat-autosave-field"
1499 1840 value="on"
1500 1841 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1501 1842 <?php checked($is_enabled, true); ?>>
@@ -1532,8 +1873,28 @@
1532 1873 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1533 1874 <span><?php esc_html_e('Enter post meta keys to include in embeddings. This is useful for OptionTree fields, theme meta boxes, or any custom post meta that is not managed by ACF.', 'mxchat'); ?></span>
1534 1875 </div>
1535 1876
1877 + <!-- Meta-key discovery scanner (plan-mxchat-20260709-fe8e4e): mirror the ACF picker's
1878 + discover-for-you UX for non-ACF meta, so owners click keys instead of typing them blind. -->
1879 + <div class="mxch-field mxchat-meta-scan">
1880 + <label class="mxch-field-label"><?php esc_html_e('Discover meta keys', 'mxchat'); ?></label>
1881 + <p class="mxch-field-description" style="margin-top: 0;">
1882 + <?php esc_html_e('Scan your published content for custom meta keys, then click any you want to include. This gives non-ACF meta the same discover-and-click experience as the ACF picker.', 'mxchat'); ?>
1883 + </p>
1884 + <div class="mxchat-meta-scan-controls">
1885 + <button type="button" id="mxchat-scan-meta-btn" class="mxch-btn mxch-btn-secondary" data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_prompts_setting_nonce')); ?>">
1886 + <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
1887 + <span class="mxchat-scan-label"><?php esc_html_e('Scan my content for meta keys', 'mxchat'); ?></span>
1888 + </button>
1889 + <label class="mxchat-meta-scan-internal">
1890 + <input type="checkbox" id="mxchat-scan-internal">
1891 + <?php esc_html_e('Include internal keys (underscore-prefixed)', 'mxchat'); ?>
1892 + </label>
1893 + </div>
1894 + <div id="mxchat-meta-scan-results" class="mxchat-meta-scan-results" aria-live="polite"></div>
1895 + </div>
1896 +
1536 1897 <div class="mxch-field">
1537 1898 <label class="mxch-field-label" for="mxchat_custom_meta_whitelist">
1538 1899 <?php esc_html_e('Meta Key Whitelist', 'mxchat'); ?>
1539 1900 </label>
@@ -1546,9 +1907,9 @@
1546 1907 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1547 1908 style="font-family: monospace;"
1548 1909 ><?php echo esc_textarea($whitelist); ?></textarea>
1549 1910 <p class="mxch-field-description">
1550 - <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Only string values are included.', 'mxchat'); ?>
1911 + <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Text values are added as-is; array or repeater values are flattened into a comma-separated list. Empty values are skipped.', 'mxchat'); ?>
1551 1912 </p>
1552 1913 </div>
1553 1914
1554 1915 <div style="margin-top: 20px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
@@ -1555,9 +1916,9 @@
1555 1916 <h4 style="margin: 0 0 12px 0; font-size: 14px;"><?php esc_html_e('How to find meta keys:', 'mxchat'); ?></h4>
1556 1917 <ul style="margin: 0; padding-left: 20px; color: var(--mxch-text-secondary); font-size: 13px;">
1557 1918 <li><?php esc_html_e('Check your theme documentation for meta key names', 'mxchat'); ?></li>
1558 1919 <li><?php esc_html_e('Look in the wp_postmeta database table', 'mxchat'); ?></li>
1559 - <li><?php esc_html_e('Use a plugin like "Show Post Meta" to view meta keys on any post', 'mxchat'); ?></li>
1920 + <li><?php esc_html_e('Use the Scan my content for meta keys button above to discover the keys on your published content automatically', 'mxchat'); ?></li>
1560 1921 <li><?php esc_html_e('Meta keys starting with underscore (_) are usually hidden/internal', 'mxchat'); ?></li>
1561 1922 </ul>
1562 1923 </div>
1563 1924 </div>
@@ -1617,8 +1978,59 @@
1617 1978 <a href="https://app.pinecone.io/" target="_blank"><?php esc_html_e('Open Pinecone Dashboard', 'mxchat'); ?></a>
1618 1979 </p>
1619 1980 </div>
1620 1981
1982 + <?php
1983 + // Index type (plan 362c31): the classic vector index (default) or a
1984 + // document index with full-text search. Same key, host and namespace.
1985 + $pc_index_type = (($pinecone_options['mxchat_pinecone_index_type'] ?? 'vector') === 'document') ? 'document' : 'vector';
1986 + $pc_docs_cloud = $pinecone_options['mxchat_pinecone_docs_cloud'] ?? 'aws';
1987 + $pc_docs_region = $pinecone_options['mxchat_pinecone_docs_region'] ?? 'us-east-1';
1988 + $pc_docs_lang = $pinecone_options['mxchat_pinecone_docs_language'] ?? 'en';
1989 + $pc_verified = $pinecone_options['mxchat_pinecone_docs_verified_host'] ?? '';
1990 + $pc_vector_host = $pinecone_options['mxchat_pinecone_vector_host'] ?? '';
1991 + $pc_current_host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1992 + $pc_docs_class = class_exists('MxChat_Pinecone_Documents');
1993 + $pc_expected_dim = $pc_docs_class ? MxChat_Pinecone_Documents::expected_dimension() : 1536;
1994 + $pc_migration = $pc_docs_class ? MxChat_Pinecone_Documents::migration_get() : array();
1995 + $pc_host_verified = ($pc_current_host !== '' && strtolower(trim($pc_current_host, '/')) === strtolower($pc_verified));
1996 + $pc_clouds = $pc_docs_class ? MxChat_Pinecone_Documents::clouds() : array('aws' => 'AWS');
1997 + $pc_languages = $pc_docs_class ? MxChat_Pinecone_Documents::languages() : array('en' => 'English');
1998 + ?>
1999 + <div class="mxch-field" id="mxchat-pinecone-index-type-field">
2000 + <span class="mxch-field-label"><?php esc_html_e('Index type', 'mxchat'); ?></span>
2001 + <div style="display: flex; flex-direction: column; gap: 6px; margin-top: 8px;">
2002 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
2003 + <input type="radio" name="mxchat_pinecone_addon_options[mxchat_pinecone_index_type]" class="mxchat-pinecone-index-type" value="vector" <?php checked($pc_index_type, 'vector'); ?>>
2004 + <span>
2005 + <strong><?php esc_html_e('Vector index (current)', 'mxchat'); ?></strong>
2006 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('The classic Pinecone index. Nothing changes for existing setups.', 'mxchat'); ?></span>
2007 + </span>
2008 + </label>
2009 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
2010 + <input type="radio" name="mxchat_pinecone_addon_options[mxchat_pinecone_index_type]" class="mxchat-pinecone-index-type" value="document" <?php checked($pc_index_type, 'document'); ?>>
2011 + <span>
2012 + <strong><?php esc_html_e('Document index with full-text search (new)', 'mxchat'); ?></strong>
2013 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('A Pinecone document index keeps a keyword index of your content next to the vectors, so questions with part numbers, SKUs and codes find the exact record. It is a new index: create one below and copy your existing index into it, no re-embedding needed. Turn on the Hybrid keyword boost under Retrieval to use it.', 'mxchat'); ?></span>
2014 + </span>
2015 + </label>
2016 + </div>
2017 + </div>
2018 + <script type="application/json" id="mxchat-pinecone-docs-state"><?php echo wp_json_encode(array(
2019 + 'verified_host' => $pc_verified,
2020 + 'expected_dimension' => (int) $pc_expected_dim,
2021 + // 47c61f: the status notice's two texts, so the page can
2022 + // keep it in step with the host typed in the field.
2023 + 'status_checked' => __('Document index checked: %1$s (%2$d dimensions).', 'mxchat'),
2024 + 'status_unchecked' => __('Not checked yet. Create a document index below, or enter its host above and click Check index. The index type only saves once the host has been checked.', 'mxchat'),
2025 + 'migration' => $pc_migration,
2026 + // 3e83e4: what the LIVE chatbot is on right now, so the
2027 + // page can tell a copy-in-progress from a switch.
2028 + 'index_type' => $pc_index_type,
2029 + 'stored_host' => strtolower(trim((string) $pc_current_host, '/')),
2030 + 'stored_index' => (string) ($pinecone_options['mxchat_pinecone_index'] ?? ''),
2031 + )); ?></script>
2032 +
1621 2033 <div class="mxch-field">
1622 2034 <label class="mxch-field-label" for="mxchat_pinecone_environment"><?php esc_html_e('Region', 'mxchat'); ?></label>
1623 2035 <input type="text" id="mxchat_pinecone_environment" name="mxchat_pinecone_addon_options[mxchat_pinecone_environment]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_environment'] ?? ''); ?>" class="mxch-input" placeholder="e.g., gcp-starter">
1624 2036 <p class="mxch-field-description"><?php esc_html_e('Your Pinecone environment/region (e.g., gcp-starter, us-west1-gcp)', 'mxchat'); ?></p>
@@ -1638,8 +2050,90 @@
1638 2050 </label>
1639 2051 <input type="text" id="mxchat_pinecone_host" name="mxchat_pinecone_addon_options[mxchat_pinecone_host]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_host'] ?? ''); ?>" class="mxch-input" placeholder="e.g., my-index-xyz123.svc.pinecone.io">
1640 2052 <p class="mxch-field-description"><?php esc_html_e('The hostname from your Pinecone index URL (exclude https://). Found in index details.', 'mxchat'); ?></p>
1641 2053 </div>
2054 +
2055 + <div class="mxchat-pinecone-docs-settings" <?php echo $pc_index_type === 'document' ? '' : 'style="display: none;"'; ?>>
2056 + <div class="mxch-notice <?php echo $pc_host_verified ? 'mxch-notice-success' : 'mxch-notice-warning'; ?>" id="mxchat-pinecone-docs-status" style="margin-bottom: 20px;">
2057 + <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
2058 + <span id="mxchat-pinecone-docs-status-text"><?php
2059 + if ($pc_host_verified) {
2060 + printf(esc_html__('Document index checked: %1$s (%2$d dimensions).', 'mxchat'), esc_html($pc_verified), (int) $pc_expected_dim);
2061 + } else {
2062 + esc_html_e('Not checked yet. Create a document index below, or enter its host above and click Check index. The index type only saves once the host has been checked.', 'mxchat');
2063 + }
2064 + ?></span>
2065 + </div>
2066 +
2067 + <div class="mxch-field">
2068 + <label class="mxch-field-label" for="mxchat_pinecone_docs_cloud"><?php esc_html_e('Cloud and region for a new index', 'mxchat'); ?></label>
2069 + <div style="display: flex; gap: 8px; flex-wrap: wrap;">
2070 + <select id="mxchat_pinecone_docs_cloud" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_cloud]" class="mxch-select" style="max-width: 160px;">
2071 + <?php foreach ($pc_clouds as $pc_cloud_key => $pc_cloud_label) : ?>
2072 + <option value="<?php echo esc_attr($pc_cloud_key); ?>" <?php selected($pc_docs_cloud, $pc_cloud_key); ?>><?php echo esc_html($pc_cloud_label); ?></option>
2073 + <?php endforeach; ?>
2074 + </select>
2075 + <input type="text" id="mxchat_pinecone_docs_region" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_region]" value="<?php echo esc_attr($pc_docs_region); ?>" class="mxch-input" style="max-width: 220px;" placeholder="us-east-1">
2076 + <select id="mxchat_pinecone_docs_language" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_language]" class="mxch-select" style="max-width: 200px;">
2077 + <?php foreach ($pc_languages as $pc_lang_key => $pc_lang_label) : ?>
2078 + <option value="<?php echo esc_attr($pc_lang_key); ?>" <?php selected($pc_docs_lang, $pc_lang_key); ?>><?php echo esc_html($pc_lang_label); ?></option>
2079 + <?php endforeach; ?>
2080 + </select>
2081 + </div>
2082 + <p class="mxch-field-description"><?php printf(esc_html__('Create index for me makes a new document index named after the Index Name field (a free name ending in -docs is suggested when you switch to this type), sized for your embedding model (%d dimensions, cosine), with full-text search on the content in the chosen language, then fills in the host. Check index confirms the host above points at a document index MxChat can use. Neither moves your chatbot — that happens when you save.', 'mxchat'), (int) $pc_expected_dim); ?></p>
2083 + </div>
2084 +
2085 + <div class="mxch-field" style="display: flex; gap: 8px; flex-wrap: wrap;">
2086 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-create"><?php esc_html_e('Create index for me', 'mxchat'); ?></button>
2087 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-check"><?php esc_html_e('Check index', 'mxchat'); ?></button>
2088 + </div>
2089 + <div id="mxchat-pinecone-docs-result" style="display: none;"></div>
2090 + <p class="mxch-field-hint" id="mxchat-pinecone-docs-gate" style="display: none;"><?php esc_html_e('Check the document index host before saving.', 'mxchat'); ?></p>
2091 + <div class="mxch-field" id="mxchat-pinecone-docs-switch-gate" style="display: none;">
2092 + <p class="mxch-field-hint" id="mxchat-pinecone-docs-switch-gate-text"></p>
2093 + <label class="mxch-field-hint" style="display: flex; align-items: center; gap: 8px; cursor: pointer;">
2094 + <input type="checkbox" id="mxchat_pinecone_docs_switch_anyway" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_switch_anyway]" value="1">
2095 + <span><?php esc_html_e('Switch anyway — point the chatbot at the document index now and copy the rest afterwards', 'mxchat'); ?></span>
2096 + </label>
2097 + </div>
2098 +
2099 + <div class="mxch-field" style="margin-top: 20px;">
2100 + <label class="mxch-field-label" for="mxchat_pinecone_vector_host"><?php esc_html_e('Copy from vector index host', 'mxchat'); ?></label>
2101 + <input type="text" id="mxchat_pinecone_vector_host" name="mxchat_pinecone_addon_options[mxchat_pinecone_vector_host]" value="<?php echo esc_attr($pc_vector_host); ?>" class="mxch-input" placeholder="my-index-xyz123.svc.pinecone.io">
2102 + <p class="mxch-field-description"><?php esc_html_e('Migrate copies every record of that index into the document index above — text, vector and metadata, same ids — with no embedding calls. It runs while your chatbot keeps answering from the vector index; save the Pinecone settings afterwards to switch. Safe to run again: records already copied are simply replaced, and an interrupted copy continues where it stopped. Both indexes must use the same embedding dimension.', 'mxchat'); ?></p>
2103 + </div>
2104 + <div class="mxch-field" style="display: flex; gap: 12px; align-items: center; flex-wrap: wrap;">
2105 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-migrate"><?php esc_html_e('Migrate', 'mxchat'); ?></button>
2106 + <?php /* e50d2e: offered only when the source index holds records in more than one namespace (the JS shows it from the start response). No name attribute — it is not a saved setting. */ ?>
2107 + <label class="mxch-toggle" id="mxchat-pinecone-docs-all-namespaces" style="display: none;" title="<?php esc_attr_e('Copy the records of every namespace of the vector index, each into the same-named namespace of the document index, in one run.', 'mxchat'); ?>">
2108 + <input type="checkbox" class="mxch-toggle-input" id="mxchat_pinecone_docs_all_namespaces" value="1">
2109 + <span class="mxch-toggle-switch"></span>
2110 + <span class="mxch-toggle-label"><?php esc_html_e('Copy every namespace', 'mxchat'); ?></span>
2111 + </label>
2112 + <span id="mxchat-pinecone-docs-migrate-counts" class="mxch-field-description"></span>
2113 + </div>
2114 + <div id="mxchat-pinecone-docs-migrate-progress" style="display: none; margin-bottom: 16px;">
2115 + <div class="mxch-progress-bar"><div class="mxch-progress-bar-fill" style="width: 0%;"></div></div>
2116 + <div class="mxch-progress-label"></div>
2117 + </div>
2118 +
2119 + <div class="mxch-field" id="mxchat-pinecone-docs-delete-old" style="display: none;">
2120 + <label class="mxch-field-label" for="mxchat_pinecone_docs_delete_confirm"><?php esc_html_e('Delete old index', 'mxchat'); ?></label>
2121 + <p class="mxch-field-description"><?php esc_html_e('Available once the copy count matches. Type the old index name to confirm. This deletes the whole vector index at Pinecone and cannot be undone.', 'mxchat'); ?></p>
2122 + <div style="display: flex; gap: 8px; flex-wrap: wrap;">
2123 + <input type="text" id="mxchat_pinecone_docs_delete_confirm" class="mxch-input" style="max-width: 320px;" placeholder="<?php esc_attr_e('old index name', 'mxchat'); ?>" autocomplete="off">
2124 + <button type="button" class="mxch-btn mxch-btn-danger" id="mxchat-pinecone-docs-delete-old-btn"><?php esc_html_e('Delete old index', 'mxchat'); ?></button>
2125 + </div>
2126 + </div>
2127 + </div>
2128 +
2129 + <div class="mxch-field">
2130 + <label class="mxch-field-label" for="mxchat_pinecone_top_k">
2131 + <?php esc_html_e('Results Per Query (topK)', 'mxchat'); ?>
2132 + </label>
2133 + <input type="number" id="mxchat_pinecone_top_k" name="mxchat_pinecone_addon_options[mxchat_pinecone_top_k]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_top_k'] ?? '50'); ?>" class="mxch-input" min="1" max="1000" step="1" placeholder="50">
2134 + <p class="mxch-field-description"><?php esc_html_e('How many candidate matches each Pinecone query requests (1-1000, default 50). Higher values improve grouping of chunked content at the cost of slightly larger responses.', 'mxchat'); ?></p>
2135 + </div>
1642 2136 </div>
1643 2137
1644 2138 <?php submit_button(__('Save Pinecone Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1645 2139 </form>
@@ -1656,12 +2150,16 @@
1656 2150 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
1657 2151 $use_vectorstore = $vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0';
1658 2152 $vectorstore_ids = $vectorstore_options['mxchat_vectorstore_ids'] ?? '';
1659 2153 $max_results = $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5;
2154 + $sync_enabled = $vectorstore_options['mxchat_vectorstore_sync_enabled'] ?? '0';
2155 + $sync_store_id = $vectorstore_options['mxchat_vectorstore_sync_store_id'] ?? '';
2156 + $sync_config = class_exists('MxChat_Vectorstore_Manager') ? MxChat_Vectorstore_Manager::get_sync_config('default') : array('enabled' => false, 'store_id' => '');
2157 + $mapped_count = class_exists('MxChat_Vectorstore_Manager') ? MxChat_Vectorstore_Manager::mapped_file_count() : 0;
1660 2158
1661 2159 // Get current chat model to check compatibility
1662 2160 $mxchat_options = get_option('mxchat_options', array());
1663 - $current_model = $mxchat_options['model'] ?? 'gpt-5.1-chat-latest';
2161 + $current_model = $mxchat_options['model'] ?? 'gpt-5.6-sol';
1664 2162 $is_openai_model = preg_match('/^(gpt-|o1-|o3-)/', $current_model);
1665 2163 ?>
1666 2164 <div id="openai-vectorstore" class="mxch-section">
1667 2165 <div class="mxch-content-header">
@@ -1672,9 +2170,9 @@
1672 2170 <div class="mxch-card">
1673 2171 <div class="mxch-card-body">
1674 2172 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1675 2173 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1676 - <span><strong><?php esc_html_e('Requires OpenAI Chat Model.', 'mxchat'); ?></strong> <?php esc_html_e('Vector Store search only works with OpenAI models (gpt-5.1-chat-latest, gpt-5-mini, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
2174 + <span><strong><?php esc_html_e('Requires OpenAI Chat Model.', 'mxchat'); ?></strong> <?php esc_html_e('Vector Store search only works with OpenAI models (gpt-5.6-sol, gpt-5.6-terra, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
1677 2175 </div>
1678 2176
1679 2177 <?php if (!$is_openai_model && $use_vectorstore === '1'): ?>
1680 2178 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 20px;">
@@ -1727,10 +2225,143 @@
1727 2225 <p class="mxch-field-description"><?php esc_html_e('Maximum number of content chunks to retrieve from the Vector Store.', 'mxchat'); ?></p>
1728 2226 </div>
1729 2227 </div>
1730 2228
2229 + <div class="mxch-field" style="border-top: 1px solid var(--mxch-border, #e2e4e7); padding-top: 20px; margin-top: 4px;">
2230 + <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 8px;">
2231 + <label class="mxchat-toggle-switch">
2232 + <input type="checkbox" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_sync_enabled]" value="1" <?php checked($sync_enabled, '1'); ?> id="mxchat_vectorstore_sync_enabled">
2233 + <span class="mxchat-toggle-slider"></span>
2234 + </label>
2235 + <span style="font-weight: 500;"><?php esc_html_e('Sync Knowledge Base to Vector Store', 'mxchat'); ?></span>
2236 + </div>
2237 + <p class="mxch-field-description" style="margin-bottom: 16px;">
2238 + <?php esc_html_e('Keeps your Vector Store up to date automatically: knowledge base additions, edits, and deletions are mirrored to the store (one file per entry, replaced on change). Without this, the store is frozen at whatever you uploaded manually. Role-restricted entries are never uploaded — OpenAI file search cannot filter by user role.', 'mxchat'); ?>
2239 + </p>
2240 +
2241 + <label class="mxch-field-label" for="mxchat_vectorstore_sync_store_id">
2242 + <?php esc_html_e('Sync Target Vector Store ID', 'mxchat'); ?>
2243 + </label>
2244 + <input type="text" id="mxchat_vectorstore_sync_store_id" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_sync_store_id]" value="<?php echo esc_attr($sync_store_id); ?>" class="mxch-input" placeholder="<?php echo esc_attr($sync_config['store_id'] ?: 'vs_abc123xyz'); ?>">
2245 + <p class="mxch-field-description">
2246 + <?php esc_html_e('Leave blank to use the first Vector Store ID above. Syncing writes to exactly one store.', 'mxchat'); ?>
2247 + </p>
2248 + </div>
2249 +
1731 2250 <?php submit_button(__('Save Vector Store Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1732 2251 </form>
2252 +
2253 + <?php if ($sync_config['enabled']): ?>
2254 + <div class="mxch-field" id="mxchat-vs-import" style="border-top: 1px solid var(--mxch-border, #e2e4e7); padding-top: 20px;"
2255 + data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_admin_nonce')); ?>">
2256 + <label class="mxch-field-label"><?php esc_html_e('Import Existing Knowledge Base', 'mxchat'); ?></label>
2257 + <p class="mxch-field-description">
2258 + <?php printf(esc_html__('One-time (re-runnable) import of every knowledge base entry into the sync target store. Runs in the background via WP-Cron; safe to interrupt and resume — already-imported, unchanged entries are skipped. Currently mirrored: %s files.', 'mxchat'), '<strong id="mxchat-vs-mapped">' . esc_html(number_format_i18n($mapped_count)) . '</strong>'); ?>
2259 + </p>
2260 + <div style="display: flex; align-items: center; gap: 8px; margin: 10px 0;">
2261 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-vs-import-start"><?php esc_html_e('Import Knowledge Base to Vector Store', 'mxchat'); ?></button>
2262 + <button type="button" class="mxch-btn" id="mxchat-vs-import-resume" style="display:none;"><?php esc_html_e('Resume Import', 'mxchat'); ?></button>
2263 + <button type="button" class="mxch-btn" id="mxchat-vs-import-cancel" style="display:none;"><?php esc_html_e('Cancel', 'mxchat'); ?></button>
2264 + </div>
2265 + <div id="mxchat-vs-import-bar" style="display:none; height: 8px; background: var(--mxch-border, #e2e4e7); border-radius: 4px; overflow: hidden; max-width: 420px;">
2266 + <div id="mxchat-vs-import-fill" style="height: 100%; width: 0%; background: #2271b1; transition: width .4s;"></div>
2267 + </div>
2268 + <p class="mxch-field-description" id="mxchat-vs-import-status" style="margin-top: 8px;"></p>
2269 + </div>
2270 +
2271 + <script>
2272 + (function() {
2273 + var wrap = document.getElementById('mxchat-vs-import');
2274 + if (!wrap) { return; }
2275 + var nonce = wrap.getAttribute('data-nonce');
2276 + var startBtn = document.getElementById('mxchat-vs-import-start');
2277 + var resumeBtn = document.getElementById('mxchat-vs-import-resume');
2278 + var cancelBtn = document.getElementById('mxchat-vs-import-cancel');
2279 + var bar = document.getElementById('mxchat-vs-import-bar');
2280 + var fill = document.getElementById('mxchat-vs-import-fill');
2281 + var statusEl = document.getElementById('mxchat-vs-import-status');
2282 + var mappedEl = document.getElementById('mxchat-vs-mapped');
2283 + var pollTimer = null;
2284 +
2285 + function post(action, done) {
2286 + var body = 'action=' + encodeURIComponent(action) + '&nonce=' + encodeURIComponent(nonce);
2287 + fetch(ajaxurl, {
2288 + method: 'POST',
2289 + credentials: 'same-origin',
2290 + headers: {'Content-Type': 'application/x-www-form-urlencoded'},
2291 + body: body
2292 + }).then(function(r) { return r.json(); }).then(done).catch(function() {
2293 + statusEl.textContent = '<?php echo esc_js(__('Request failed — check the browser console.', 'mxchat')); ?>';
2294 + });
2295 + }
2296 +
2297 + function render(state, mapped) {
2298 + if (typeof mapped === 'number' && mappedEl) { mappedEl.textContent = mapped.toLocaleString(); }
2299 + var running = state.status === 'running';
2300 + startBtn.style.display = running ? 'none' : '';
2301 + cancelBtn.style.display = running ? '' : 'none';
2302 + resumeBtn.style.display = (running && state.stalled) ? '' : 'none';
2303 + bar.style.display = (running || state.status === 'done') ? '' : 'none';
2304 + var pct = state.total > 0 ? Math.min(100, Math.round(state.processed / state.total * 100)) : 0;
2305 + if (state.status === 'done') { pct = 100; }
2306 + fill.style.width = pct + '%';
2307 +
2308 + var bits = [];
2309 + if (state.status === 'running') { bits.push(state.stalled ? '<?php echo esc_js(__('Import stalled (cron missed) — click Resume.', 'mxchat')); ?>' : '<?php echo esc_js(__('Importing…', 'mxchat')); ?>'); }
2310 + if (state.status === 'done') { bits.push('<?php echo esc_js(__('Import complete.', 'mxchat')); ?>'); }
2311 + if (state.status === 'cancelled') { bits.push('<?php echo esc_js(__('Import cancelled.', 'mxchat')); ?>'); }
2312 + if (state.status === 'error') { bits.push('<?php echo esc_js(__('Import error:', 'mxchat')); ?> ' + state.last_error); }
2313 + if (state.status !== 'idle') {
2314 + bits.push(state.processed + '/' + state.total
2315 + + ' — ' + state.imported + ' <?php echo esc_js(__('uploaded', 'mxchat')); ?>'
2316 + + ', ' + state.unchanged + ' <?php echo esc_js(__('unchanged', 'mxchat')); ?>'
2317 + + ', ' + state.skipped_restricted + ' <?php echo esc_js(__('skipped (role-restricted)', 'mxchat')); ?>'
2318 + + ', ' + state.failed + ' <?php echo esc_js(__('failed', 'mxchat')); ?>');
2319 + if (state.failed > 0 && state.last_error && state.status !== 'error') {
2320 + bits.push('<?php echo esc_js(__('Last error:', 'mxchat')); ?> ' + state.last_error);
2321 + }
2322 + }
2323 + statusEl.textContent = bits.join(' ');
2324 +
2325 + if (running && !pollTimer) {
2326 + pollTimer = setInterval(function() {
2327 + post('mxchat_vectorstore_import_status', function(res) {
2328 + if (res && res.success) { render(res.data.state, res.data.mapped); }
2329 + });
2330 + }, 3000);
2331 + } else if (!running && pollTimer) {
2332 + clearInterval(pollTimer);
2333 + pollTimer = null;
2334 + }
2335 + }
2336 +
2337 + startBtn.addEventListener('click', function() {
2338 + startBtn.disabled = true;
2339 + statusEl.textContent = '<?php echo esc_js(__('Starting…', 'mxchat')); ?>';
2340 + post('mxchat_vectorstore_import_start', function(res) {
2341 + startBtn.disabled = false;
2342 + if (res && res.success) { render(res.data.state); }
2343 + else { statusEl.textContent = (res && res.data && res.data.message) ? res.data.message : '<?php echo esc_js(__('Could not start the import.', 'mxchat')); ?>'; }
2344 + });
2345 + });
2346 + resumeBtn.addEventListener('click', function() {
2347 + post('mxchat_vectorstore_import_resume', function(res) {
2348 + if (res && res.success) { render(res.data.state); }
2349 + });
2350 + });
2351 + cancelBtn.addEventListener('click', function() {
2352 + post('mxchat_vectorstore_import_cancel', function(res) {
2353 + if (res && res.success) { render(res.data.state); }
2354 + });
2355 + });
2356 +
2357 + // Pick up an import already in flight when the page loads.
2358 + post('mxchat_vectorstore_import_status', function(res) {
2359 + if (res && res.success && res.data.state.status !== 'idle') { render(res.data.state, res.data.mapped); }
2360 + });
2361 + })();
2362 + </script>
2363 + <?php endif; ?>
1733 2364 </div>
1734 2365 </div>
1735 2366 </div>
1736 2367 <?php
@@ -1779,8 +2410,20 @@
1779 2410 <option value="unprocessed"><?php esc_html_e('Not In Knowledge Base', 'mxchat'); ?></option>
1780 2411 </select>
1781 2412 </div>
1782 2413 </div>
2414 + <?php
2415 + // Shown only while Media is the selected type (plan 01010a). Media lists
2416 + // just the files whose text MxChat can read; saying so here is clearer
2417 + // than listing images and audio as rows that cannot be selected.
2418 + ?>
2419 + <div class="mxchat-kb-filter-note" id="mxchat-kb-media-note" hidden>
2420 + <svg class="mxchat-kb-filter-note-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
2421 + <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
2422 + <path d="M14 2v6h6"></path>
2423 + </svg>
2424 + <span><?php esc_html_e('Media lists PDF, Word, text and Markdown files — the types MxChat can read text from. Images, audio and video are not shown.', 'mxchat'); ?></span>
2425 + </div>
1783 2426 <div class="mxchat-kb-content-selection">
1784 2427 <div class="mxchat-kb-selection-header">
1785 2428 <label>
1786 2429 <input type="checkbox" id="mxchat-kb-select-all">
@@ -1800,15 +2443,8 @@
1800 2443 </div>
1801 2444 </div>
1802 2445 <div class="mxchat-kb-modal-footer">
1803 2446 <div class="mxchat-kb-pagination"></div>
1804 - <div class="mxchat-kb-acf-pdf-option" style="display:flex; align-items:flex-start; gap:8px; padding:8px 0; font-size:12px; color: var(--mxch-text-secondary, #64748b);">
1805 - <input type="checkbox" id="mxchat-kb-acf-pdf-extract" style="margin-top:2px;">
1806 - <label for="mxchat-kb-acf-pdf-extract" style="cursor:pointer; line-height:1.4;">
1807 - <strong style="color: var(--mxch-text-primary, #1a1a2e);"><?php esc_html_e('Also extract text from PDFs linked in ACF File/Image fields', 'mxchat'); ?></strong><br>
1808 - <span><?php esc_html_e('Adds 200-500ms per post and ~30 KB to each KB entry. Recommended only if your ACF setup uses PDF attachments for primary content.', 'mxchat'); ?></span>
1809 - </label>
1810 - </div>
1811 2447 <div class="mxchat-kb-footer-actions">
1812 2448 <button type="button" id="mxchat-kb-process-selected" class="mxchat-kb-button-primary" disabled>
1813 2449 <?php esc_html_e('Process Selected Content', 'mxchat'); ?>
1814 2450 <span class="mxchat-kb-selected-count">(0)</span>
@@ -1939,9 +2575,18 @@
1939 2575 if (btn) { e.preventDefault(); open(btn); }
1940 2576 });
1941 2577 if (closeX) closeX.addEventListener('click', close);
1942 2578 if (closeBtn) closeBtn.addEventListener('click', close);
1943 - modal.addEventListener('click', function(e) { if (e.target === modal) close(); });
2579 + // Overlay click closes, but only when the whole gesture happened on the
2580 + // overlay — otherwise selecting indexed text and releasing past the
2581 + // dialog edge dispatches the click on the overlay and closes it.
2582 + var downOnOverlay = false;
2583 + modal.addEventListener('mousedown', function(e) { downOnOverlay = (e.target === modal); });
2584 + modal.addEventListener('click', function(e) {
2585 + var overlayGesture = downOnOverlay;
2586 + downOnOverlay = false;
2587 + if (e.target === modal && overlayGesture) close();
2588 + });
1944 2589 document.addEventListener('keydown', function(e) {
1945 2590 if (e.key === 'Escape' && modal.classList.contains('active')) close();
1946 2591 });
1947 2592
@@ -2073,83 +2718,15 @@
2073 2718 'use strict';
2074 2719
2075 2720 // Wait for DOM
2076 2721 document.addEventListener('DOMContentLoaded', function() {
2077 - initKnowledgeNavigation();
2078 - initMobileMenu();
2722 + // Tab navigation + mobile menu live in the shared shell script
2723 + // (js/admin-sidebar.js), enqueued for this page by plan c192a4.
2079 2724 initImportOptions();
2080 2725 initCustomPostTypesToggle();
2081 2726 // Sitemap detection is now initialized when user clicks the Sitemap Import option
2082 2727 });
2083 2728
2084 - function initKnowledgeNavigation() {
2085 - const navLinks = document.querySelectorAll('.mxch-nav-link[data-target], .mxch-mobile-nav-link[data-target]');
2086 - const sections = document.querySelectorAll('.mxch-section');
2087 -
2088 - function showSection(targetId) {
2089 - // Hide all sections
2090 - sections.forEach(section => section.classList.remove('active'));
2091 - // Show target section
2092 - const target = document.getElementById(targetId);
2093 - if (target) {
2094 - target.classList.add('active');
2095 - }
2096 - // Update nav links
2097 - navLinks.forEach(link => {
2098 - link.classList.toggle('active', link.dataset.target === targetId);
2099 - });
2100 - // Update URL hash
2101 - history.replaceState(null, null, '#' + targetId);
2102 - }
2103 -
2104 - navLinks.forEach(link => {
2105 - link.addEventListener('click', function(e) {
2106 - e.preventDefault();
2107 - const targetId = this.dataset.target;
2108 - showSection(targetId);
2109 - // Close mobile menu if open
2110 - const mobileMenu = document.querySelector('.mxch-mobile-menu');
2111 - const overlay = document.querySelector('.mxch-mobile-overlay');
2112 - if (mobileMenu) mobileMenu.classList.remove('open');
2113 - if (overlay) overlay.classList.remove('open');
2114 - });
2115 - });
2116 -
2117 - // Handle initial hash
2118 - const hash = window.location.hash.substring(1);
2119 - if (hash && document.getElementById(hash)) {
2120 - showSection(hash);
2121 - }
2122 - }
2123 -
2124 - function initMobileMenu() {
2125 - const menuBtn = document.querySelector('.mxch-mobile-menu-btn');
2126 - const closeBtn = document.querySelector('.mxch-mobile-menu-close');
2127 - const overlay = document.querySelector('.mxch-mobile-overlay');
2128 - const menu = document.querySelector('.mxch-mobile-menu');
2129 -
2130 - if (menuBtn && menu) {
2131 - menuBtn.addEventListener('click', function() {
2132 - menu.classList.add('open');
2133 - if (overlay) overlay.classList.add('open');
2134 - });
2135 - }
2136 -
2137 - if (closeBtn && menu) {
2138 - closeBtn.addEventListener('click', function() {
2139 - menu.classList.remove('open');
2140 - if (overlay) overlay.classList.remove('open');
2141 - });
2142 - }
2143 -
2144 - if (overlay && menu) {
2145 - overlay.addEventListener('click', function() {
2146 - menu.classList.remove('open');
2147 - overlay.classList.remove('open');
2148 - });
2149 - }
2150 - }
2151 -
2152 2729 function initImportOptions() {
2153 2730 const importBoxes = document.querySelectorAll('.mxchat-import-box');
2154 2731 const urlInputArea = document.getElementById('mxchat-url-input-area');
2155 2732 const contentInputArea = document.getElementById('mxchat-content-input-area');
@@ -2260,9 +2837,14 @@
2260 2837
2261 2838 if (!modal) return;
2262 2839
2263 2840 var currentEntry = {};
2841 + var loadedContent = '';
2264 2842
2843 + var EDIT_I18N = {
2844 + discard: <?php echo wp_json_encode( __('Discard your unsaved changes to this entry?', 'mxchat') ); ?>
2845 + };
2846 +
2265 2847 // Bind edit buttons (delegated)
2266 2848 document.addEventListener('click', function(e) {
2267 2849 var btn = e.target.closest('.mxchat-edit-entry-btn');
2268 2850 if (btn) {
@@ -2270,13 +2852,16 @@
2270 2852 openEditModal(btn);
2271 2853 }
2272 2854 });
2273 2855
2274 - // Close handlers
2275 - if (closeBtn) closeBtn.addEventListener('click', closeModal);
2276 - if (cancelBtn) cancelBtn.addEventListener('click', closeModal);
2277 - modal.addEventListener('click', function(e) {
2278 - if (e.target === modal) closeModal();
2856 + // Close handlers. The overlay deliberately has no click-to-close: a click
2857 + // fires on the common ancestor of its mousedown and mouseup, so releasing
2858 + // a text-selection drag past the dialog edge targets the overlay and would
2859 + // discard the edit. Closing is explicit only, and confirms when dirty.
2860 + if (closeBtn) closeBtn.addEventListener('click', requestClose);
2861 + if (cancelBtn) cancelBtn.addEventListener('click', requestClose);
2862 + document.addEventListener('keydown', function(e) {
2863 + if (e.key === 'Escape' && modal.classList.contains('active')) requestClose();
2279 2864 });
2280 2865
2281 2866 // Character count
2282 2867 if (textarea) {
@@ -2298,8 +2883,9 @@
2298 2883 };
2299 2884
2300 2885 // Reset state
2301 2886 textarea.value = '';
2887 + loadedContent = '';
2302 2888 textarea.placeholder = 'Loading content...';
2303 2889 textarea.disabled = true;
2304 2890 saveBtn.disabled = true;
2305 2891 notice.textContent = '';
@@ -2330,8 +2916,9 @@
2330 2916 .then(function(r) { return r.json(); })
2331 2917 .then(function(resp) {
2332 2918 if (resp.success) {
2333 2919 textarea.value = resp.data.content || '';
2920 + loadedContent = textarea.value;
2334 2921 textarea.disabled = false;
2335 2922 textarea.placeholder = 'Edit your content here...';
2336 2923 saveBtn.disabled = false;
2337 2924 currentEntry.contentType = resp.data.content_type || 'content';
@@ -2386,13 +2973,23 @@
2386 2973 if (saveSpin) saveSpin.style.display = 'none';
2387 2974 saveBtn.disabled = false;
2388 2975
2389 2976 if (resp.success) {
2390 - showNotice('Saved successfully!', 'success');
2391 - setTimeout(function() {
2392 - closeModal();
2393 - window.location.reload();
2394 - }, 1000);
2977 + if (currentEntry.dataSource === 'pinecone') {
2978 + // Pinecone list reads are eventually consistent — the reloaded
2979 + // table may show the old text for up to ~60 seconds.
2980 + showNotice('Saved! Pinecone changes may take up to 60 seconds to appear — use "Refresh Entries" if the list still shows the old text.', 'success');
2981 + setTimeout(function() {
2982 + closeModal();
2983 + window.location.reload();
2984 + }, 2500);
2985 + } else {
2986 + showNotice('Saved successfully!', 'success');
2987 + setTimeout(function() {
2988 + closeModal();
2989 + window.location.reload();
2990 + }, 1000);
2991 + }
2395 2992 } else {
2396 2993 showNotice((resp.data && resp.data.message) || 'Save failed.', 'error');
2397 2994 }
2398 2995 })
@@ -2401,8 +2998,17 @@
2401 2998 if (saveSpin) saveSpin.style.display = 'none';
2402 2999 saveBtn.disabled = false;
2403 3000 showNotice('Network error: ' + err.message, 'error');
2404 3001 });
3002 + }
3003 +
3004 + function isDirty() {
3005 + return !textarea.disabled && textarea.value !== loadedContent;
3006 + }
3007 +
3008 + function requestClose() {
3009 + if (isDirty() && !window.confirm(EDIT_I18N.discard)) return;
3010 + closeModal();
2405 3011 }
2406 3012
2407 3013 function closeModal() {
2408 3014 modal.classList.remove('active');