PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.1.1
MxChat – AI Chatbot & Content Generation for WordPress v3.1.1
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 | mxchat-basic.php +39 -462 3.2.153.1.1 View file →
@@ -2,9 +2,9 @@
2 2 /**
3 3 * Plugin Name: MxChat
4 4 * Plugin URI: https://mxchat.ai/
5 5 * Description: AI chatbot for WordPress with OpenAI, Claude, xAI, DeepSeek, live agent, PDF uploads, WooCommerce, and training on website data.
6 - * Version: 3.2.15
6 + * Version: 3.1.1
7 7 * Author: MxChat
8 8 * Author URI: https://mxchat.ai
9 9 * License: GPLv2 or later
10 10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
@@ -15,49 +15,15 @@
15 15 if (!defined('ABSPATH')) {
16 16 exit; // Exit if accessed directly.
17 17 }
18 18
19 -
20 -if (!defined('MXCHAT_DEV_MODE')) {
21 - define('MXCHAT_DEV_MODE', false);
22 -}
23 -
19 +// Define plugin version constant for asset versioning
20 +// Reads version from plugin header automatically
24 21 if (!defined('MXCHAT_VERSION')) {
25 22 $plugin_data = get_file_data(__FILE__, array('Version' => 'Version'), 'plugin');
26 - $version = $plugin_data['Version'];
27 - // MXCHAT_BASE_VERSION: the plain header version, stable across requests even in
28 - // dev mode. Use it for anything PERSISTED or COMPARED (the stored
29 - // mxchat_plugin_version option and the migration gate in
30 - // mxchat_check_for_update). MXCHAT_VERSION keeps the time() suffix in dev for
31 - // ASSET cache-busting only — persisting the suffixed value made the version
32 - // comparison churn every request, re-running the full activation/migration
33 - // suite per page load on dev installs.
34 - define('MXCHAT_BASE_VERSION', $version);
35 - if (MXCHAT_DEV_MODE) {
36 - $version .= '.' . time();
37 - }
38 - define('MXCHAT_VERSION', $version);
23 + define('MXCHAT_VERSION', $plugin_data['Version']);
39 24 }
40 25
41 -/**
42 - * Honest, versioned User-Agent for MXChat remote-content ingestion fetches
43 - * (Knowledge Base PDF import, URL import, sitemap / website crawl).
44 - *
45 - * WAF rulesets (SiteGround/ModSecurity, Wordfence, Cloudflare managed rules)
46 - * flag stale spoofed-browser UAs as scrapers and return 403 — which silently
47 - * broke the single most common KB source: self-hosted media on the site's own
48 - * domain. A truthful crawler identifier is the industry norm for well-behaved
49 - * bots and lets a site owner allowlist "MXChatBot" in their WAF. Filterable so
50 - * a locked-down host can supply a different string without a code change.
51 - */
52 -if (!function_exists('mxchat_ingest_user_agent')) {
53 - function mxchat_ingest_user_agent() {
54 - $version = defined('MXCHAT_VERSION') ? MXCHAT_VERSION : '1.0';
55 - $ua = 'MXChatBot/' . $version . ' (+https://mxchat.ai/bot)';
56 - return apply_filters('mxchat_ingest_user_agent', $ua);
57 - }
58 -}
59 -
60 26 function mxchat_load_textdomain() {
61 27 $domain = 'mxchat';
62 28 $locale = determine_locale();
63 29
@@ -73,84 +39,8 @@
73 39 }
74 40 add_action('init', 'mxchat_load_textdomain');
75 41
76 42 /**
77 - * One-time migration: gemini-3-pro-preview was shut down by Google on March 9, 2026.
78 - * Existing installs with the dead ID get auto-remapped to gemini-3.1-pro-preview
79 - * (Google's official migration target) the first time admin_init fires after update.
80 - */
81 -add_action('admin_init', function () {
82 - if (get_option('mxchat_gemini_3_remap_done')) {
83 - return;
84 - }
85 - $opts = get_option('mxchat_options');
86 - if (is_array($opts) && isset($opts['model']) && $opts['model'] === 'gemini-3-pro-preview') {
87 - $opts['model'] = 'gemini-3.1-pro-preview';
88 - update_option('mxchat_options', $opts);
89 - }
90 - if (is_array($opts) && isset($opts['content_model']) && $opts['content_model'] === 'gemini-3-pro-preview') {
91 - $opts['content_model'] = 'gemini-3.1-pro-preview';
92 - update_option('mxchat_options', $opts);
93 - }
94 - update_option('mxchat_gemini_3_remap_done', 1);
95 -});
96 -
97 -/**
98 - * One-time migration: the Grok 2 family was retired by xAI (grok-2, grok-2-1212,
99 - * grok-2-latest, grok-2-vision-1212 all return 400 "Model not found"). Existing
100 - * installs with the dead ID get auto-remapped to grok-4-1-fast-non-reasoning
101 - * (modern, fast, broadly available) the first time admin_init fires after update.
102 - */
103 -add_action('admin_init', function () {
104 - if (get_option('mxchat_grok_2_remap_done')) {
105 - return;
106 - }
107 - $opts = get_option('mxchat_options');
108 - if (is_array($opts) && isset($opts['model']) && $opts['model'] === 'grok-2') {
109 - $opts['model'] = 'grok-4-1-fast-non-reasoning';
110 - update_option('mxchat_options', $opts);
111 - }
112 - if (is_array($opts) && isset($opts['content_model']) && $opts['content_model'] === 'grok-2') {
113 - $opts['content_model'] = 'grok-4-1-fast-non-reasoning';
114 - update_option('mxchat_options', $opts);
115 - }
116 - update_option('mxchat_grok_2_remap_done', 1);
117 -});
118 -
119 -/**
120 - * One-time migration: Anthropic retired the Claude 4 (2025-05-14) snapshots on
121 - * June 15, 2026 — claude-opus-4-20250514 and claude-sonnet-4-20250514 now return
122 - * an API error. Existing installs with a dead ID get auto-remapped to the current
123 - * equivalents Anthropic recommends (Opus 4.8 / Sonnet 4.6) the first time admin_init
124 - * fires after update. Mirrors the gemini-3-pro-preview / grok-2 rescues above.
125 - */
126 -add_action('admin_init', function () {
127 - if (get_option('mxchat_claude_4_retire_remap_done')) {
128 - return;
129 - }
130 - $map = array(
131 - 'claude-opus-4-20250514' => 'claude-opus-4-8',
132 - 'claude-sonnet-4-20250514' => 'claude-sonnet-4-6',
133 - );
134 - $opts = get_option('mxchat_options');
135 - if (is_array($opts)) {
136 - $changed = false;
137 - if (isset($opts['model']) && isset($map[$opts['model']])) {
138 - $opts['model'] = $map[$opts['model']];
139 - $changed = true;
140 - }
141 - if (isset($opts['content_model']) && isset($map[$opts['content_model']])) {
142 - $opts['content_model'] = $map[$opts['content_model']];
143 - $changed = true;
144 - }
145 - if ($changed) {
146 - update_option('mxchat_options', $opts);
147 - }
148 - }
149 - update_option('mxchat_claude_4_retire_remap_done', 1);
150 -});
151 -
152 -/**
153 43 * Exclude MxChat assets from caching plugin optimizations
154 44 *
155 45 * This prevents issues with WP Rocket, LiteSpeed Cache, Autoptimize, WP Super Cache,
156 46 * W3 Total Cache, SG Optimizer, and similar plugins that may break the chatbot by
@@ -353,67 +243,11 @@
353 243 $rejected[] = 'wp-admin/admin-ajax.php';
354 244 return $rejected;
355 245 });
356 246
357 -// ── Page-cache bypass for chat AJAX (companion to the 3.2.6 nonce-race hotfix)
358 -// Each cache plugin gets its own filter export so that visitors hitting an
359 -// edge-cached page never receive cached chat-AJAX responses. The chat send /
360 -// stream send / file upload all POST to /wp-admin/admin-ajax.php with
361 -// `action=mxchat_*`. Without these exports, a cache plugin can stale a response
362 -// and break the per-session nonce flow on the first message.
363 -
364 -// WP Rocket — `rocket_cache_reject_uri` takes a flat array of regex strings.
365 -add_filter('rocket_cache_reject_uri', function($uris) {
366 - if (!is_array($uris)) $uris = array();
367 - $uris[] = '/wp-admin/admin-ajax\.php\?action=mxchat_.*';
368 - return $uris;
369 -});
370 -
371 -// LiteSpeed Cache — `litespeed_cache_no_cache_for_request` short-circuits
372 -// caching when the request matches our chat-AJAX pattern.
373 -add_filter('litespeed_cache_no_cache_for_request', function($no_cache) {
374 - if ($no_cache) return $no_cache;
375 - if (!empty($_SERVER['REQUEST_URI']) &&
376 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
377 - !empty($_REQUEST['action']) &&
378 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
379 - return true;
380 - }
381 - return $no_cache;
382 -});
383 -
384 -// W3 Total Cache — `w3tc_pgcache_request_skip_uri` flips page-cache off when
385 -// the URI matches.
386 -add_filter('w3tc_pgcache_request_skip_uri', function($skip) {
387 - if ($skip) return $skip;
388 - if (!empty($_SERVER['REQUEST_URI']) &&
389 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
390 - !empty($_REQUEST['action']) &&
391 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
392 - return true;
393 - }
394 - return $skip;
395 -});
396 -
397 -// FlyingPress — `flying_press_cacheable` takes a boolean and is run per
398 -// request. Same pattern as LiteSpeed / W3TC.
399 -add_filter('flying_press_cacheable', function($cacheable) {
400 - if (!$cacheable) return $cacheable;
401 - if (!empty($_SERVER['REQUEST_URI']) &&
402 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
403 - !empty($_REQUEST['action']) &&
404 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
405 - return false;
406 - }
407 - return $cacheable;
408 -});
409 -
410 247 // Include classes with error handling
411 248 function mxchat_include_classes() {
412 249 $class_files = array(
413 - 'includes/class-mxchat-model-catalog.php',
414 - 'includes/class-mxchat-live-agent-schedule.php',
415 - 'includes/class-mxchat-tool-registry.php',
416 250 'includes/class-mxchat-integrator.php',
417 251 'includes/class-mxchat-admin.php',
418 252 'includes/class-mxchat-public.php',
419 253 'includes/class-mxchat-utils.php',
@@ -421,11 +255,8 @@
421 255 'includes/class-mxchat-meta-box.php',
422 256 'includes/class-mxchat-chunker.php',
423 257 'includes/class-mxchat-word-handler.php',
424 258 'includes/class-mxchat-content-generator.php',
425 - 'includes/class-mxchat-cache-purge.php',
426 - 'includes/class-mxchat-editor-assistant.php',
427 - 'includes/class-rest-api.php',
428 259 'admin/class-ajax-handler.php',
429 260 'admin/class-pinecone-manager.php',
430 261 'admin/class-knowledge-manager.php'
431 262 );
@@ -437,38 +268,8 @@
437 268 } else {
438 269 //error_log('MxChat: Missing class file - ' . $file);
439 270 }
440 271 }
441 -
442 - // Register the native function-calling admin-post save handler (a41dee).
443 - if (class_exists('MxChat_Tool_Registry')) {
444 - MxChat_Tool_Registry::init();
445 - }
446 -
447 - // Editor Assistant — free, OFF-by-default block-editor AI actions (plan-8cb0cb).
448 - // init() wires REST + streaming AJAX + sidebar enqueue ONLY when the
449 - // mxchat_editor_assistant_enabled option is 'on'; otherwise zero footprint.
450 - if (class_exists('MxChat_Editor_Assistant')) {
451 - MxChat_Editor_Assistant::init();
452 - }
453 -
454 - // Admin pages that aren't classes (procedural include).
455 - if (is_admin()) {
456 - $admin_api_page = plugin_dir_path(__FILE__) . 'includes/admin-api-page.php';
457 - if (file_exists($admin_api_page)) {
458 - require_once $admin_api_page;
459 - }
460 - // f7c7d4 renamed this file admin-dashboard-page.php → admin-onboarding-page.php.
461 - // The require MUST live here (admin bootstrap) and not just inside
462 - // mxchat_add_plugin_page() on the admin_menu hook — admin_menu does NOT
463 - // fire on admin-ajax.php requests, so the wizard's AJAX handlers
464 - // (plan-905439: mxchat_onboarding_kb_status / save_step / mark_step /
465 - // auto_graduate + the f7c7d4 dismiss handler) would never register.
466 - $admin_onboarding_page = plugin_dir_path(__FILE__) . 'includes/admin-onboarding-page.php';
467 - if (file_exists($admin_onboarding_page)) {
468 - require_once $admin_onboarding_page;
469 - }
470 - }
471 272 }
472 273
473 274 /**
474 275 * Lazy-load the PDF parser library only when needed.
@@ -755,9 +556,9 @@
755 556
756 557 // Add index for bot_id
757 558 $wpdb->query("ALTER TABLE {$table_name} ADD KEY bot_id (bot_id)");
758 559
759 - //error_log('MxChat: Successfully added bot_id column to mxchat_pinecone_roles table');
560 + error_log('MxChat: Successfully added bot_id column to mxchat_pinecone_roles table');
760 561 }
761 562
762 563 // Mark migration as complete
763 564 update_option('mxchat_pinecone_roles_migration_version', '2.5.2');
@@ -792,9 +593,9 @@
792 593
793 594 // Add index for better query performance
794 595 $wpdb->query("ALTER TABLE {$table_name} ADD KEY content_type (content_type)");
795 596
796 - //error_log('MxChat: Successfully added content_type column to mxchat_system_prompt_content table');
597 + error_log('MxChat: Successfully added content_type column to mxchat_system_prompt_content table');
797 598 }
798 599
799 600 // Mark migration as complete
800 601 update_option('mxchat_content_type_migration_version', '2.5.6');
@@ -800,33 +601,8 @@
800 601 update_option('mxchat_content_type_migration_version', '2.5.6');
801 602 }
802 603
803 604 /**
804 - * 3.2.4: Backfill the active embedding model option for installs that already
805 - * have KB content but no stamped model. The mismatch warning compares this
806 - * against the user's currently selected model — no per-row column needed.
807 - */
808 -function mxchat_backfill_active_embedding_model() {
809 - global $wpdb;
810 -
811 - if (get_option('mxchat_active_embedding_model', '') !== '') {
812 - return;
813 - }
814 -
815 - $kb_table = $wpdb->prefix . 'mxchat_system_prompt_content';
816 - if ($wpdb->get_var("SHOW TABLES LIKE '{$kb_table}'") !== $kb_table) {
817 - return;
818 - }
819 -
820 - $kb_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$kb_table}");
821 - if ($kb_count > 0) {
822 - $options = get_option('mxchat_options', array());
823 - $current_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
824 - update_option('mxchat_active_embedding_model', $current_model, false);
825 - }
826 -}
827 -
828 -/**
829 605 * 2.5.2: Create queue processing tables for reliable background processing
830 606 */
831 607 function mxchat_create_queue_tables() {
832 608 global $wpdb;
@@ -898,34 +674,8 @@
898 674 dbDelta($sql);
899 675 }
900 676
901 677 /**
902 - * Create per-session satisfaction ratings table (v3.2.6)
903 - * Stores one 👍/👎 rating + optional feedback per chat session.
904 - */
905 -function mxchat_create_session_ratings_table() {
906 - global $wpdb;
907 - $charset_collate = $wpdb->get_charset_collate();
908 -
909 - $table_name = $wpdb->prefix . 'mxchat_session_ratings';
910 - $sql = "CREATE TABLE $table_name (
911 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
912 - session_id varchar(255) NOT NULL,
913 - bot_id varchar(50) NOT NULL DEFAULT 'default',
914 - rating_value tinyint(1) NOT NULL,
915 - rating_feedback text DEFAULT NULL,
916 - created_at datetime NOT NULL,
917 - PRIMARY KEY (id),
918 - UNIQUE KEY session_id (session_id),
919 - KEY bot_id (bot_id),
920 - KEY created_at (created_at)
921 - ) $charset_collate;";
922 -
923 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
924 - dbDelta($sql);
925 -}
926 -
927 -/**
928 678 * 2.5.2: Fix URL column size to support long URLs (especially with UTF-8 encoding)
929 679 * This fixes "url, source_url. The supplied values may be too long" errors
930 680 */
931 681 function mxchat_fix_url_column_size() {
@@ -948,18 +698,8 @@
948 698
949 699 /**
950 700 * Migrate deprecated AI models to their replacements
951 701 * Version 2.5.1: Migrate Claude 3.5 Sonnet (deprecated) to Claude 3.7 Sonnet
952 - * Version 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support
953 - * Without utf8mb4, any bot response containing emojis silently fails to insert.
954 - */
955 -function mxchat_migrate_transcripts_charset() {
956 - global $wpdb;
957 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
958 - $wpdb->query("ALTER TABLE $table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
959 -}
960 -
961 -/**
962 702 * Version 3.0.55: Migrate GPT-4 series models (deprecated 2026-02-17) to GPT-5 series
963 703 */
964 704 function mxchat_migrate_deprecated_models() {
965 705 $options = get_option('mxchat_options', array());
@@ -984,9 +724,9 @@
984 724 if (in_array($current_model, $deprecated_claude_models, true)) {
985 725 $options['model'] = 'claude-opus-4-6';
986 726 $migrated = true;
987 727 $migration_message = sprintf(
988 - 'Your chatbot model has been automatically updated from %s to Claude Opus 4.6 due to Anthropic deprecating older Claude models.',
728 + __('Your chatbot model has been automatically updated from %s to Claude Opus 4.6 due to Anthropic deprecating older Claude models.', 'mxchat'),
989 729 $current_model
990 730 );
991 731 }
992 732
@@ -993,9 +733,9 @@
993 733 // Migrate deprecated Claude Haiku 3.5 to Claude Haiku 4.5
994 734 if ($current_model === 'claude-3-5-haiku-20241022') {
995 735 $options['model'] = 'claude-haiku-4-5-20251001';
996 736 $migrated = true;
997 - $migration_message = 'Your chatbot model has been automatically updated from Claude Haiku 3.5 to Claude Haiku 4.5 due to Anthropic deprecating the older model.';
737 + $migration_message = __('Your chatbot model has been automatically updated from Claude Haiku 3.5 to Claude Haiku 4.5 due to Anthropic deprecating the older model.', 'mxchat');
998 738 }
999 739
1000 740 // Migrate deprecated GPT-4 series and GPT-3.5 Turbo to GPT-5.1 Chat Latest
1001 741 if (in_array($current_model, array('gpt-4o', 'gpt-4.1-2025-04-14', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'), true)) {
@@ -1001,9 +741,9 @@
1001 741 if (in_array($current_model, array('gpt-4o', 'gpt-4.1-2025-04-14', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'), true)) {
1002 742 $options['model'] = 'gpt-5.1-chat-latest';
1003 743 $migrated = true;
1004 744 $migration_message = sprintf(
1005 - 'Your chatbot model has been automatically updated from %s to GPT-5.1 Chat Latest due to OpenAI deprecating older models.',
745 + __('Your chatbot model has been automatically updated from %s to GPT-5.1 Chat Latest due to OpenAI deprecating older models.', 'mxchat'),
1006 746 $current_model
1007 747 );
1008 748 }
1009 749
@@ -1011,26 +751,13 @@
1011 751 if (in_array($current_model, array('gpt-4o-mini', 'gpt-4.1-mini'), true)) {
1012 752 $options['model'] = 'gpt-5-mini';
1013 753 $migrated = true;
1014 754 $migration_message = sprintf(
1015 - 'Your chatbot model has been automatically updated from %s to GPT-5 Mini due to OpenAI deprecating GPT-4 series models.',
755 + __('Your chatbot model has been automatically updated from %s to GPT-5 Mini due to OpenAI deprecating GPT-4 series models.', 'mxchat'),
1016 756 $current_model
1017 757 );
1018 758 }
1019 759
1020 - // Migrate retired DeepSeek ids to DeepSeek V4 Flash — the vendor removed
1021 - // deepseek-chat and deepseek-reasoner on 2026-07-24 (hard cutoff, every
1022 - // request 400s). V4 Flash is DeepSeek's designated successor for the
1023 - // legacy deepseek-chat alias.
1024 - if (in_array($current_model, array('deepseek-chat', 'deepseek-reasoner'), true)) {
1025 - $options['model'] = 'deepseek-v4-flash';
1026 - $migrated = true;
1027 - $migration_message = sprintf(
1028 - 'Your chatbot model has been automatically updated from %s to DeepSeek V4 Flash because DeepSeek retired its older API models on July 24, 2026.',
1029 - $current_model
1030 - );
1031 - }
1032 -
1033 760 if ($migrated) {
1034 761 update_option('mxchat_options', $options);
1035 762 update_option('mxchat_model_migrated_notice', true);
1036 763 update_option('mxchat_model_migration_message', $migration_message);
@@ -1093,26 +820,13 @@
1093 820 enabled_bots LONGTEXT DEFAULT NULL,
1094 821 PRIMARY KEY (id)
1095 822 ) $charset_collate;";
1096 823
1097 - // Individual Intent Phrases Table - each phrase gets its own embedding vector
1098 - $intent_phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
1099 - $sql_intent_phrases_table = "CREATE TABLE $intent_phrases_table (
1100 - id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
1101 - intent_id BIGINT(20) UNSIGNED NOT NULL,
1102 - phrase TEXT NOT NULL,
1103 - embedding_vector LONGTEXT NOT NULL,
1104 - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1105 - PRIMARY KEY (id),
1106 - KEY intent_id (intent_id)
1107 - ) $charset_collate;";
1108 -
1109 824 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
1110 825
1111 826 // Create other tables
1112 827 dbDelta($sql_system_prompt);
1113 828 dbDelta($sql_intents_table);
1114 - dbDelta($sql_intent_phrases_table);
1115 829
1116 830 // Create URL click tracking table
1117 831 mxchat_create_url_clicks_table();
1118 832
@@ -1124,11 +838,8 @@
1124 838
1125 839 // Create transcript translations table
1126 840 mxchat_create_translations_table();
1127 841
1128 - // Create per-session satisfaction ratings table (v3.2.6)
1129 - mxchat_create_session_ratings_table();
1130 -
1131 842 // Ensure additional columns in system prompt table
1132 843 $existing_system_columns = $wpdb->get_results("SHOW COLUMNS FROM $system_prompt_table");
1133 844 if (!empty($existing_system_columns)) {
1134 845 $existing_system_column_names = array_column($existing_system_columns, 'Field');
@@ -1171,17 +882,13 @@
1171 882
1172 883 // Run migration for existing installations
1173 884 mxchat_migrate_pinecone_roles_add_bot_id();
1174 885
1175 - // 3.2.4: Backfill active embedding model option (replaces 3.2.3 column-based tracking)
1176 - mxchat_backfill_active_embedding_model();
1177 -
1178 886 // Setup cron jobs
1179 887 mxchat_setup_cron_jobs();
1180 888
1181 - // Update version (stable base version — never the dev time()-suffixed one,
1182 - // or the check_for_update comparison would churn every request)
1183 - update_option('mxchat_plugin_version', MXCHAT_BASE_VERSION);
889 + // Update version
890 + update_option('mxchat_plugin_version', MXCHAT_VERSION);
1184 891
1185 892 //error_log("MxChat: Activation function completed");
1186 893 }
1187 894
@@ -1196,35 +903,28 @@
1196 903 if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
1197 904 // Set flag to use fallback system
1198 905 update_option('mxchat_use_fallback_rate_limits', true);
1199 906 update_option('mxchat_next_rate_limit_check', time() + 3600);
1200 - // Deliberately NO early return (plan-bc08a6): transcript cleanup below
1201 - // must still be scheduled. DISABLE_WP_CRON only changes HOW cron events
1202 - // execute (a server-side runner hitting wp-cron.php instead of loopback
1203 - // spawns) — scheduling still just writes the cron option. The old early
1204 - // return here meant a deactivate/reactivate cycle on a DISABLE_WP_CRON
1205 - // site permanently lost the transcript cleanup event while the retention
1206 - // setting still claimed to be active.
907 + return;
908 + }
909 +
910 + // Schedule the rate limit reset cron job
911 + $result = wp_schedule_event(time() + 300, 'hourly', 'mxchat_reset_rate_limits');
912 +
913 + if ($result === false) {
914 + // Fallback if scheduling fails
915 + update_option('mxchat_use_fallback_rate_limits', true);
916 + update_option('mxchat_next_rate_limit_check', time() + 3600);
1207 917 } else {
1208 - // Schedule the rate limit reset cron job
1209 - $result = wp_schedule_event(time() + 300, 'hourly', 'mxchat_reset_rate_limits');
1210 -
1211 - if ($result === false) {
1212 - // Fallback if scheduling fails
1213 - update_option('mxchat_use_fallback_rate_limits', true);
1214 - update_option('mxchat_next_rate_limit_check', time() + 3600);
1215 - } else {
1216 - // Clear fallback flags if cron scheduling succeeded
1217 - delete_option('mxchat_use_fallback_rate_limits');
1218 - }
918 + // Clear fallback flags if cron scheduling succeeded
919 + delete_option('mxchat_use_fallback_rate_limits');
1219 920 }
1220 -
1221 - // Schedule transcript cleanup if configured (bucket dropdown OR custom retention-days > 0)
921 +
922 + // Schedule transcript cleanup if configured
1222 923 $transcript_options = get_option('mxchat_transcripts_options', array());
1223 924 $cleanup_interval = isset($transcript_options['mxchat_auto_delete_transcripts']) ? $transcript_options['mxchat_auto_delete_transcripts'] : 'never';
1224 - $custom_retention = isset($transcript_options['mxchat_retention_days']) ? (int) $transcript_options['mxchat_retention_days'] : 0;
1225 -
1226 - if ($cleanup_interval !== 'never' || $custom_retention > 0) {
925 +
926 + if ($cleanup_interval !== 'never') {
1227 927 // Check if not already scheduled
1228 928 if (!wp_next_scheduled('mxchat_cleanup_old_transcripts')) {
1229 929 // Schedule to run daily at 3 AM
1230 930 $next_run = strtotime('tomorrow 3:00 AM');
@@ -1261,25 +961,17 @@
1261 961 return;
1262 962 }
1263 963
1264 964 $next_check = get_option('mxchat_next_rate_limit_check', 0);
1265 -
965 +
1266 966 if (time() >= $next_check) {
1267 - // Reuse the bootstrap's integrator — mxchat_init() creates the global on
1268 - // plugins_loaded (before this init-priority-5 callback), so it's always set
1269 - // here. Constructing a second MxChat_Integrator just to call one method
1270 - // re-registers every hook the plugin has (ajax pairs, wp_footer loader,
1271 - // rest_api_init, admin_init guard) on a duplicate instance for the rest of
1272 - // the request. Defensive construction only if the global is somehow unset.
1273 - // NOTE: MxChat_Integrator::check_fallback_rate_limits() is a second
1274 - // implementation of this same check — if either changes, change both.
1275 - global $mxchat_integrator;
1276 - $integrator = ($mxchat_integrator instanceof MxChat_Integrator)
1277 - ? $mxchat_integrator
1278 - : (class_exists('MxChat_Integrator') ? new MxChat_Integrator() : null);
1279 - if ($integrator && method_exists($integrator, 'mxchat_reset_rate_limits')) {
1280 - $integrator->mxchat_reset_rate_limits();
1281 - update_option('mxchat_next_rate_limit_check', time() + 3600);
967 + // Only run reset if the MxChat_Integrator class exists
968 + if (class_exists('MxChat_Integrator')) {
969 + $integrator = new MxChat_Integrator();
970 + if (method_exists($integrator, 'mxchat_reset_rate_limits')) {
971 + $integrator->mxchat_reset_rate_limits();
972 + update_option('mxchat_next_rate_limit_check', time() + 3600);
973 + }
1282 974 }
1283 975 }
1284 976 }
1285 977
@@ -1291,9 +983,9 @@
1291 983 global $wpdb;
1292 984
1293 985 try {
1294 986 $current_version = get_option('mxchat_plugin_version', '0.0.0');
1295 - $plugin_version = MXCHAT_BASE_VERSION;
987 + $plugin_version = MXCHAT_VERSION;
1296 988
1297 989 // Always ensure critical tables exist (even if version matches)
1298 990 // This handles manual table deletion or fresh installs
1299 991 $chat_table = $wpdb->prefix . 'mxchat_chat_transcripts';
@@ -1355,32 +1047,8 @@
1355 1047 if (version_compare($current_version, '3.0.6', '<')) {
1356 1048 mxchat_migrate_deprecated_models();
1357 1049 }
1358 1050
1359 - // 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support
1360 - if (version_compare($current_version, '3.1.2', '<')) {
1361 - mxchat_migrate_transcripts_charset();
1362 - }
1363 -
1364 - // 3.1.7: Clean up stale shared session email/name entries
1365 - if (version_compare($current_version, '3.1.7', '<')) {
1366 - delete_option('mxchat_email_null');
1367 - delete_option('mxchat_name_null');
1368 - }
1369 -
1370 - // 3.2.4: Backfill active embedding model option for the warning UI
1371 - // (replaces the per-row column tracking from 3.2.3, which was reverted)
1372 - if (version_compare($current_version, '3.2.4', '<')) {
1373 - mxchat_backfill_active_embedding_model();
1374 - }
1375 -
1376 - // 3.2.15: Migrate retired DeepSeek ids (deepseek-chat / deepseek-reasoner
1377 - // were shut off at the vendor on 2026-07-24). The function is idempotent —
1378 - // it only rewrites models on its deprecation lists.
1379 - if (version_compare($current_version, '3.2.15', '<')) {
1380 - mxchat_migrate_deprecated_models();
1381 - }
1382 -
1383 1051 // Run full activation to ensure everything is up to date
1384 1052 mxchat_activate();
1385 1053
1386 1054 // Run migration functions
@@ -1551,9 +1219,9 @@
1551 1219 // Check if we're upgrading TO 3.0.1 and haven't handled this yet
1552 1220 if (version_compare($current_version, $target_version, '<') && !$update_handled) {
1553 1221 // Check if Pro is activated - only show to Pro users
1554 1222 $license_status = get_option('mxchat_license_status', 'inactive');
1555 - $is_pro = ($license_status === 'active');
1223 + $is_pro = ($license_status === 'active' || $license_status === esc_html__('active', 'mxchat'));
1556 1224
1557 1225 if ($is_pro) {
1558 1226 // Set flag to show the theme migration notification banner
1559 1227 update_option('mxchat_show_theme_migration_notice', true);
@@ -1605,23 +1273,8 @@
1605 1273 if (class_exists('MxChat_Content_Generator')) {
1606 1274 new MxChat_Content_Generator();
1607 1275 }
1608 1276
1609 - // Initialize cache purge globally — settings writes can happen on any
1610 - // request type (admin screens, admin-ajax autosave, wp-cli), and the
1611 - // deferred-purge cron event fires on front-end requests.
1612 - if (class_exists('MxChat_Cache_Purge')) {
1613 - MxChat_Cache_Purge::init();
1614 - }
1615 -
1616 - // Initialize REST API globally — endpoints must be registered on
1617 - // every request (admin and frontend) so they're reachable via /wp-json/.
1618 - // Endpoints are auth-gated and locked until the site owner generates
1619 - // a token in MxChat → API Access.
1620 - if (class_exists('MxChat_Rest_Api')) {
1621 - new MxChat_Rest_Api();
1622 - }
1623 -
1624 1277 // Initialize public classes
1625 1278 if (class_exists('MxChat_Public')) {
1626 1279 $mxchat_public = new MxChat_Public();
1627 1280 }
@@ -1666,25 +1319,11 @@
1666 1319
1667 1320 mxchat_migrate_pinecone_roles_add_bot_id();
1668 1321 mxchat_migrate_add_content_type_column();
1669 1322 mxchat_migrate_add_translations_table();
1670 - mxchat_migrate_add_session_ratings_table();
1671 1323 }
1672 1324
1673 1325 /**
1674 - * Migration: Create per-session satisfaction ratings table (v3.2.6)
1675 - * For users upgrading from versions before 3.2.6
1676 - */
1677 -function mxchat_migrate_add_session_ratings_table() {
1678 - $migration_key = 'mxchat_session_ratings_table_created';
1679 - if (get_option($migration_key)) {
1680 - return;
1681 - }
1682 - mxchat_create_session_ratings_table();
1683 - update_option($migration_key, '3.2.6');
1684 -}
1685 -
1686 -/**
1687 1326 * Migration: Create transcript translations table (v3.0.4)
1688 1327 * For users upgrading from versions before 3.0.4
1689 1328 */
1690 1329 function mxchat_migrate_add_translations_table() {
@@ -1727,67 +1366,5 @@
1727 1366 return $schedules;
1728 1367 });
1729 1368
1730 1369 // Register deactivation hook
1731 -register_deactivation_hook(__FILE__, 'mxchat_deactivate');
1732 -
1733 -/**
1734 - * Per-session satisfaction rating: AJAX save handler (v3.2.6).
1735 - * Records one 👍/👎 + optional feedback per chat session. The UNIQUE KEY on
1736 - * session_id makes this naturally idempotent — only the first rating per
1737 - * session is stored; duplicate POSTs are silent no-ops.
1738 - */
1739 -function mxchat_save_session_rating() {
1740 - global $wpdb;
1741 -
1742 - $session_id = isset($_POST['session_id']) ? sanitize_text_field(wp_unslash($_POST['session_id'])) : '';
1743 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field(wp_unslash($_POST['bot_id'])) : 'default';
1744 - $rating_raw = isset($_POST['rating']) ? (int) $_POST['rating'] : 0;
1745 - $feedback = isset($_POST['feedback']) ? sanitize_textarea_field(wp_unslash($_POST['feedback'])) : '';
1746 -
1747 - if ($session_id === '' || ($rating_raw !== 1 && $rating_raw !== -1)) {
1748 - wp_send_json_error(array('message' => 'invalid_input'), 400);
1749 - }
1750 -
1751 - if (strlen($feedback) > 1000) {
1752 - $feedback = substr($feedback, 0, 1000);
1753 - }
1754 -
1755 - $table_name = $wpdb->prefix . 'mxchat_session_ratings';
1756 - $existing = $wpdb->get_var($wpdb->prepare(
1757 - "SELECT id FROM $table_name WHERE session_id = %s LIMIT 1",
1758 - $session_id
1759 - ));
1760 -
1761 - if ($existing) {
1762 - if ($feedback !== '') {
1763 - $wpdb->update(
1764 - $table_name,
1765 - array('rating_feedback' => $feedback),
1766 - array('id' => (int) $existing),
1767 - array('%s'),
1768 - array('%d')
1769 - );
1770 - }
1771 - wp_send_json_success(array('updated' => true));
1772 - }
1773 -
1774 - $inserted = $wpdb->insert(
1775 - $table_name,
1776 - array(
1777 - 'session_id' => $session_id,
1778 - 'bot_id' => $bot_id !== '' ? $bot_id : 'default',
1779 - 'rating_value' => $rating_raw,
1780 - 'rating_feedback' => $feedback !== '' ? $feedback : null,
1781 - 'created_at' => current_time('mysql'),
1782 - ),
1783 - array('%s', '%s', '%d', '%s', '%s')
1784 - );
1785 -
1786 - if ($inserted === false) {
1787 - wp_send_json_error(array('message' => 'db_insert_failed'), 500);
1788 - }
1789 -
1790 - wp_send_json_success(array('saved' => true));
1791 -}
1792 -add_action('wp_ajax_mxchat_save_rating', 'mxchat_save_session_rating');
1793 -add_action('wp_ajax_nopriv_mxchat_save_rating', 'mxchat_save_session_rating');
1370 +register_deactivation_hook(__FILE__, 'mxchat_deactivate');