PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.5.7
MxChat – AI Chatbot & Content Generation for WordPress v2.5.7
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 +32 -736 3.2.82.5.7 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.8
6 + * Version: 2.5.7
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,339 +15,20 @@
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 - if (MXCHAT_DEV_MODE) {
28 - $version .= '.' . time();
29 - }
30 - define('MXCHAT_VERSION', $version);
23 + define('MXCHAT_VERSION', $plugin_data['Version']);
31 24 }
32 25
33 26 function mxchat_load_textdomain() {
34 - $domain = 'mxchat';
35 - $locale = determine_locale();
36 -
37 - // First, try to load from /wp-content/languages/plugins/ (preserved during updates)
38 - $mo_file = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo';
39 - if (file_exists($mo_file)) {
40 - load_textdomain($domain, $mo_file);
41 - return;
42 - }
43 -
44 - // Fallback to plugin's /languages directory
45 - load_plugin_textdomain($domain, false, dirname(plugin_basename(__FILE__)) . '/languages');
27 + load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages');
46 28 }
47 29 add_action('init', 'mxchat_load_textdomain');
48 30
49 -/**
50 - * One-time migration: gemini-3-pro-preview was shut down by Google on March 9, 2026.
51 - * Existing installs with the dead ID get auto-remapped to gemini-3.1-pro-preview
52 - * (Google's official migration target) the first time admin_init fires after update.
53 - */
54 -add_action('admin_init', function () {
55 - if (get_option('mxchat_gemini_3_remap_done')) {
56 - return;
57 - }
58 - $opts = get_option('mxchat_options');
59 - if (is_array($opts) && isset($opts['model']) && $opts['model'] === 'gemini-3-pro-preview') {
60 - $opts['model'] = 'gemini-3.1-pro-preview';
61 - update_option('mxchat_options', $opts);
62 - }
63 - if (is_array($opts) && isset($opts['content_model']) && $opts['content_model'] === 'gemini-3-pro-preview') {
64 - $opts['content_model'] = 'gemini-3.1-pro-preview';
65 - update_option('mxchat_options', $opts);
66 - }
67 - update_option('mxchat_gemini_3_remap_done', 1);
68 -});
69 -
70 -/**
71 - * One-time migration: the Grok 2 family was retired by xAI (grok-2, grok-2-1212,
72 - * grok-2-latest, grok-2-vision-1212 all return 400 "Model not found"). Existing
73 - * installs with the dead ID get auto-remapped to grok-4-1-fast-non-reasoning
74 - * (modern, fast, broadly available) the first time admin_init fires after update.
75 - */
76 -add_action('admin_init', function () {
77 - if (get_option('mxchat_grok_2_remap_done')) {
78 - return;
79 - }
80 - $opts = get_option('mxchat_options');
81 - if (is_array($opts) && isset($opts['model']) && $opts['model'] === 'grok-2') {
82 - $opts['model'] = 'grok-4-1-fast-non-reasoning';
83 - update_option('mxchat_options', $opts);
84 - }
85 - if (is_array($opts) && isset($opts['content_model']) && $opts['content_model'] === 'grok-2') {
86 - $opts['content_model'] = 'grok-4-1-fast-non-reasoning';
87 - update_option('mxchat_options', $opts);
88 - }
89 - update_option('mxchat_grok_2_remap_done', 1);
90 -});
91 -
92 -/**
93 - * Exclude MxChat assets from caching plugin optimizations
94 - *
95 - * This prevents issues with WP Rocket, LiteSpeed Cache, Autoptimize, WP Super Cache,
96 - * W3 Total Cache, SG Optimizer, and similar plugins that may break the chatbot by
97 - * removing "unused" CSS, minifying/combining JS, or deferring/delaying jQuery.
98 - *
99 - * Both chat-script.js and floating-script.js depend on jQuery, so jQuery must also
100 - * be excluded from any optimization that changes load order or timing.
101 - */
102 -
103 -// ── WP Rocket ────────────────────────────────────────────────────────────────
104 -
105 -// Exclude from Remove Unused CSS (RUCSS)
106 -add_filter('rocket_rucss_inline_atts_exclusions', function($exclusions) {
107 - if (!is_array($exclusions)) $exclusions = array();
108 - $exclusions[] = 'mxchat';
109 - return $exclusions;
110 -});
111 -
112 -// Exclude CSS from minification/combination
113 -add_filter('rocket_exclude_css', function($excluded) {
114 - if (!is_array($excluded)) $excluded = array();
115 - $excluded[] = '/plugins/mxchat-basic/css/chat-style.css';
116 - return $excluded;
117 -});
118 -
119 -// Exclude JS from minification/combination
120 -add_filter('rocket_exclude_js', function($excluded) {
121 - if (!is_array($excluded)) $excluded = array();
122 - $excluded[] = '/plugins/mxchat-basic/js/chat-script.js';
123 - $excluded[] = '/plugins/mxchat-basic/js/floating-script.js';
124 - $excluded[] = '/jquery-core';
125 - $excluded[] = '/jquery.min.js';
126 - $excluded[] = '/jquery.js';
127 - $excluded[] = '/jquery-migrate';
128 - return $excluded;
129 -});
130 -
131 -// Exclude JS from defer
132 -add_filter('rocket_exclude_defer_js', function($excluded) {
133 - if (!is_array($excluded)) $excluded = array();
134 - $excluded[] = '/plugins/mxchat-basic/js/chat-script.js';
135 - $excluded[] = '/plugins/mxchat-basic/js/floating-script.js';
136 - $excluded[] = '/jquery-core';
137 - $excluded[] = '/jquery.min.js';
138 - $excluded[] = '/jquery.js';
139 - $excluded[] = '/jquery-migrate';
140 - return $excluded;
141 -});
142 -
143 -// Exclude from delay JS execution
144 -add_filter('rocket_delay_js_exclusions', function($excluded) {
145 - if (!is_array($excluded)) $excluded = array();
146 - $excluded[] = 'mxchat';
147 - $excluded[] = 'chat-script';
148 - $excluded[] = 'floating-script';
149 - $excluded[] = '/jquery-core';
150 - $excluded[] = '/jquery.min.js';
151 - $excluded[] = '/jquery.js';
152 - $excluded[] = '/jquery-migrate';
153 - return $excluded;
154 -});
155 -
156 -// ── LiteSpeed Cache ──────────────────────────────────────────────────────────
157 -
158 -// Exclude CSS from optimization
159 -add_filter('litespeed_optimize_css_excludes', function($excluded) {
160 - if (!is_array($excluded)) $excluded = array();
161 - $excluded[] = 'chat-style.css';
162 - $excluded[] = 'mxchat';
163 - return $excluded;
164 -});
165 -
166 -// Exclude from UCSS (Unique CSS) - prevents LiteSpeed from stripping "unused" MxChat CSS
167 -add_filter('litespeed_ucss_whitelist', function($whitelist) {
168 - if (!is_array($whitelist)) $whitelist = array();
169 - $whitelist[] = '.mxchat-chatbot-wrapper';
170 - $whitelist[] = '.floating-chatbot';
171 - $whitelist[] = '.floating-chatbot-button';
172 - $whitelist[] = '.chatbot-top-bar';
173 - $whitelist[] = '.mxchat-chatbot';
174 - $whitelist[] = '.chat-container';
175 - $whitelist[] = '.chat-box';
176 - $whitelist[] = '.bot-message';
177 - $whitelist[] = '.input-container';
178 - $whitelist[] = '.chat-input';
179 - $whitelist[] = '.send-button';
180 - $whitelist[] = '.pre-chat-message';
181 - $whitelist[] = '.mxchat-popular-questions';
182 - $whitelist[] = '.chat-toolbar';
183 - $whitelist[] = '.exit-chat';
184 - $whitelist[] = '.email-blocker';
185 - return $whitelist;
186 -});
187 -
188 -// Exclude CSS from CCSS (Critical CSS) generation
189 -add_filter('litespeed_optm_ccss_exc', function($excluded) {
190 - if (!is_array($excluded)) $excluded = array();
191 - $excluded[] = 'chat-style.css';
192 - $excluded[] = 'mxchat';
193 - return $excluded;
194 -});
195 -
196 -// Exclude JS from defer
197 -add_filter('litespeed_optm_js_defer_exc', function($excluded) {
198 - if (!is_array($excluded)) $excluded = array();
199 - $excluded[] = 'chat-script.js';
200 - $excluded[] = 'floating-script.js';
201 - $excluded[] = 'mxchat';
202 - $excluded[] = 'jquery.min.js';
203 - $excluded[] = 'jquery.js';
204 - return $excluded;
205 -});
206 -
207 -// Exclude JS from combining
208 -add_filter('litespeed_optm_js_exc', function($excluded) {
209 - if (!is_array($excluded)) $excluded = array();
210 - $excluded[] = 'chat-script.js';
211 - $excluded[] = 'floating-script.js';
212 - $excluded[] = 'mxchat';
213 - $excluded[] = 'jquery.min.js';
214 - $excluded[] = 'jquery.js';
215 - return $excluded;
216 -});
217 -
218 -// Exclude JS from delayed execution
219 -add_filter('litespeed_optm_js_delay_exc', function($excluded) {
220 - if (!is_array($excluded)) $excluded = array();
221 - $excluded[] = 'chat-script.js';
222 - $excluded[] = 'floating-script.js';
223 - $excluded[] = 'mxchat';
224 - return $excluded;
225 -});
226 -
227 -// Exclude from Guest Mode optimization
228 -add_filter('litespeed_guest_optm_exc', function($excluded) {
229 - if (!is_array($excluded)) $excluded = array();
230 - $excluded[] = 'mxchat';
231 - $excluded[] = 'chat-style';
232 - $excluded[] = 'chat-script';
233 - $excluded[] = 'floating-script';
234 - return $excluded;
235 -});
236 -
237 -// ── Autoptimize ──────────────────────────────────────────────────────────────
238 -
239 -// Exclude CSS from optimization (comma-separated strings)
240 -add_filter('autoptimize_filter_css_exclude', function($excluded) {
241 - if (!is_string($excluded)) $excluded = '';
242 - return $excluded . ', mxchat, chat-style.css';
243 -});
244 -
245 -// Exclude JS from optimization (comma-separated strings)
246 -add_filter('autoptimize_filter_js_exclude', function($excluded) {
247 - if (!is_string($excluded)) $excluded = '';
248 - return $excluded . ', mxchat, chat-script.js, floating-script.js, jquery.min.js, jquery.js';
249 -});
250 -
251 -// ── SG Optimizer (SiteGround) ────────────────────────────────────────────────
252 -
253 -add_filter('sgo_js_minify_exclude', function($excluded) {
254 - if (!is_array($excluded)) $excluded = array();
255 - $excluded[] = 'chat-script.js';
256 - $excluded[] = 'floating-script.js';
257 - $excluded[] = 'jquery.min.js';
258 - return $excluded;
259 -});
260 -
261 -add_filter('sgo_javascript_combine_exclude', function($excluded) {
262 - if (!is_array($excluded)) $excluded = array();
263 - $excluded[] = 'chat-script.js';
264 - $excluded[] = 'floating-script.js';
265 - $excluded[] = 'jquery.min.js';
266 - return $excluded;
267 -});
268 -
269 -add_filter('sgo_js_async_exclude', function($excluded) {
270 - if (!is_array($excluded)) $excluded = array();
271 - $excluded[] = 'chat-script.js';
272 - $excluded[] = 'floating-script.js';
273 - $excluded[] = 'jquery.min.js';
274 - return $excluded;
275 -});
276 -
277 -// ── W3 Total Cache ───────────────────────────────────────────────────────────
278 -
279 -add_filter('w3tc_minify_js_do_tag_minification', function($do_minify, $script_tag, $file) {
280 - if (strpos($file, 'chat-script.js') !== false ||
281 - strpos($file, 'floating-script.js') !== false ||
282 - strpos($file, 'jquery.min.js') !== false ||
283 - strpos($file, 'jquery.js') !== false) {
284 - return false;
285 - }
286 - return $do_minify;
287 -}, 10, 3);
288 -
289 -// ── WP Super Cache ──────────────────────────────────────────────────────────
290 -
291 -add_filter('wpsc_rejected_uri', function($rejected) {
292 - if (!is_array($rejected)) $rejected = array();
293 - $rejected[] = 'wp-admin/admin-ajax.php';
294 - return $rejected;
295 -});
296 -
297 -// ── Page-cache bypass for chat AJAX (companion to the 3.2.6 nonce-race hotfix)
298 -// Each cache plugin gets its own filter export so that visitors hitting an
299 -// edge-cached page never receive cached chat-AJAX responses. The chat send /
300 -// stream send / file upload all POST to /wp-admin/admin-ajax.php with
301 -// `action=mxchat_*`. Without these exports, a cache plugin can stale a response
302 -// and break the per-session nonce flow on the first message.
303 -
304 -// WP Rocket — `rocket_cache_reject_uri` takes a flat array of regex strings.
305 -add_filter('rocket_cache_reject_uri', function($uris) {
306 - if (!is_array($uris)) $uris = array();
307 - $uris[] = '/wp-admin/admin-ajax\.php\?action=mxchat_.*';
308 - return $uris;
309 -});
310 -
311 -// LiteSpeed Cache — `litespeed_cache_no_cache_for_request` short-circuits
312 -// caching when the request matches our chat-AJAX pattern.
313 -add_filter('litespeed_cache_no_cache_for_request', function($no_cache) {
314 - if ($no_cache) return $no_cache;
315 - if (!empty($_SERVER['REQUEST_URI']) &&
316 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
317 - !empty($_REQUEST['action']) &&
318 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
319 - return true;
320 - }
321 - return $no_cache;
322 -});
323 -
324 -// W3 Total Cache — `w3tc_pgcache_request_skip_uri` flips page-cache off when
325 -// the URI matches.
326 -add_filter('w3tc_pgcache_request_skip_uri', function($skip) {
327 - if ($skip) return $skip;
328 - if (!empty($_SERVER['REQUEST_URI']) &&
329 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
330 - !empty($_REQUEST['action']) &&
331 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
332 - return true;
333 - }
334 - return $skip;
335 -});
336 -
337 -// FlyingPress — `flying_press_cacheable` takes a boolean and is run per
338 -// request. Same pattern as LiteSpeed / W3TC.
339 -add_filter('flying_press_cacheable', function($cacheable) {
340 - if (!$cacheable) return $cacheable;
341 - if (!empty($_SERVER['REQUEST_URI']) &&
342 - strpos($_SERVER['REQUEST_URI'], '/wp-admin/admin-ajax.php') !== false &&
343 - !empty($_REQUEST['action']) &&
344 - strpos((string) $_REQUEST['action'], 'mxchat_') === 0) {
345 - return false;
346 - }
347 - return $cacheable;
348 -});
349 -
350 31 // Include classes with error handling
351 32 function mxchat_include_classes() {
352 33 $class_files = array(
353 34 'includes/class-mxchat-integrator.php',
@@ -355,12 +36,10 @@
355 36 'includes/class-mxchat-public.php',
356 37 'includes/class-mxchat-utils.php',
357 38 'includes/class-mxchat-user.php',
358 39 'includes/class-mxchat-meta-box.php',
359 - 'includes/class-mxchat-chunker.php',
40 + 'includes/pdf-parser/alt_autoload.php',
360 41 'includes/class-mxchat-word-handler.php',
361 - 'includes/class-mxchat-content-generator.php',
362 - 'includes/class-rest-api.php',
363 42 'admin/class-ajax-handler.php',
364 43 'admin/class-pinecone-manager.php',
365 44 'admin/class-knowledge-manager.php'
366 45 );
@@ -372,45 +51,11 @@
372 51 } else {
373 52 //error_log('MxChat: Missing class file - ' . $file);
374 53 }
375 54 }
376 -
377 - // Admin pages that aren't classes (procedural include).
378 - if (is_admin()) {
379 - $admin_api_page = plugin_dir_path(__FILE__) . 'includes/admin-api-page.php';
380 - if (file_exists($admin_api_page)) {
381 - require_once $admin_api_page;
382 - }
383 - // f7c7d4 renamed this file admin-dashboard-page.php → admin-onboarding-page.php.
384 - // The require MUST live here (admin bootstrap) and not just inside
385 - // mxchat_add_plugin_page() on the admin_menu hook — admin_menu does NOT
386 - // fire on admin-ajax.php requests, so the wizard's AJAX handlers
387 - // (plan-905439: mxchat_onboarding_kb_status / save_step / mark_step /
388 - // auto_graduate + the f7c7d4 dismiss handler) would never register.
389 - $admin_onboarding_page = plugin_dir_path(__FILE__) . 'includes/admin-onboarding-page.php';
390 - if (file_exists($admin_onboarding_page)) {
391 - require_once $admin_onboarding_page;
392 - }
393 - }
394 55 }
395 56
396 57 /**
397 - * Lazy-load the PDF parser library only when needed.
398 - * Avoids loading 44 files on every page request.
399 - */
400 -function mxchat_load_pdf_parser() {
401 - if (class_exists('\Smalot\PdfParser\Parser')) {
402 - return true;
403 - }
404 - $autoload_path = plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php';
405 - if (file_exists($autoload_path)) {
406 - require_once $autoload_path;
407 - return true;
408 - }
409 - return false;
410 -}
411 -
412 -/**
413 58 * Create URL click tracking table
414 59 */
415 60 function mxchat_create_url_clicks_table() {
416 61 global $wpdb;
@@ -493,13 +138,12 @@
493 138
494 139 // Define all required columns and their types
495 140 $required_columns = [
496 141 'user_identifier' => 'VARCHAR(255) DEFAULT NULL',
497 - 'user_email' => 'VARCHAR(255) DEFAULT NULL',
142 + 'user_email' => 'VARCHAR(255) DEFAULT NULL',
498 143 'user_name' => 'VARCHAR(100) DEFAULT NULL',
499 144 'originating_page_url' => 'TEXT DEFAULT NULL',
500 - 'originating_page_title' => 'VARCHAR(500) DEFAULT NULL',
501 - 'rag_context' => 'LONGTEXT DEFAULT NULL'
145 + 'originating_page_title' => 'VARCHAR(500) DEFAULT NULL'
502 146 ];
503 147
504 148 // Get existing columns
505 149 $existing_columns = $wpdb->get_results("SHOW COLUMNS FROM $table_name");
@@ -678,9 +322,9 @@
678 322
679 323 // Add index for bot_id
680 324 $wpdb->query("ALTER TABLE {$table_name} ADD KEY bot_id (bot_id)");
681 325
682 - //error_log('MxChat: Successfully added bot_id column to mxchat_pinecone_roles table');
326 + error_log('MxChat: Successfully added bot_id column to mxchat_pinecone_roles table');
683 327 }
684 328
685 329 // Mark migration as complete
686 330 update_option('mxchat_pinecone_roles_migration_version', '2.5.2');
@@ -715,9 +359,9 @@
715 359
716 360 // Add index for better query performance
717 361 $wpdb->query("ALTER TABLE {$table_name} ADD KEY content_type (content_type)");
718 362
719 - //error_log('MxChat: Successfully added content_type column to mxchat_system_prompt_content table');
363 + error_log('MxChat: Successfully added content_type column to mxchat_system_prompt_content table');
720 364 }
721 365
722 366 // Mark migration as complete
723 367 update_option('mxchat_content_type_migration_version', '2.5.6');
@@ -723,33 +367,8 @@
723 367 update_option('mxchat_content_type_migration_version', '2.5.6');
724 368 }
725 369
726 370 /**
727 - * 3.2.4: Backfill the active embedding model option for installs that already
728 - * have KB content but no stamped model. The mismatch warning compares this
729 - * against the user's currently selected model — no per-row column needed.
730 - */
731 -function mxchat_backfill_active_embedding_model() {
732 - global $wpdb;
733 -
734 - if (get_option('mxchat_active_embedding_model', '') !== '') {
735 - return;
736 - }
737 -
738 - $kb_table = $wpdb->prefix . 'mxchat_system_prompt_content';
739 - if ($wpdb->get_var("SHOW TABLES LIKE '{$kb_table}'") !== $kb_table) {
740 - return;
741 - }
742 -
743 - $kb_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$kb_table}");
744 - if ($kb_count > 0) {
745 - $options = get_option('mxchat_options', array());
746 - $current_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
747 - update_option('mxchat_active_embedding_model', $current_model, false);
748 - }
749 -}
750 -
751 -/**
752 371 * 2.5.2: Create queue processing tables for reliable background processing
753 372 */
754 373 function mxchat_create_queue_tables() {
755 374 global $wpdb;
@@ -797,58 +416,8 @@
797 416 //error_log("MxChat: Queue tables created/updated successfully");
798 417 }
799 418
800 419 /**
801 - * Create transcript translations table for persisting translations
802 - */
803 -function mxchat_create_translations_table() {
804 - global $wpdb;
805 - $charset_collate = $wpdb->get_charset_collate();
806 -
807 - $table_name = $wpdb->prefix . 'mxchat_transcript_translations';
808 - $sql = "CREATE TABLE $table_name (
809 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
810 - session_id varchar(255) NOT NULL,
811 - language_code varchar(10) NOT NULL,
812 - translations longtext NOT NULL,
813 - created_at datetime NOT NULL,
814 - updated_at datetime NOT NULL,
815 - PRIMARY KEY (id),
816 - UNIQUE KEY session_lang (session_id, language_code),
817 - KEY session_id (session_id)
818 - ) $charset_collate;";
819 -
820 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
821 - dbDelta($sql);
822 -}
823 -
824 -/**
825 - * Create per-session satisfaction ratings table (v3.2.6)
826 - * Stores one 👍/👎 rating + optional feedback per chat session.
827 - */
828 -function mxchat_create_session_ratings_table() {
829 - global $wpdb;
830 - $charset_collate = $wpdb->get_charset_collate();
831 -
832 - $table_name = $wpdb->prefix . 'mxchat_session_ratings';
833 - $sql = "CREATE TABLE $table_name (
834 - id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
835 - session_id varchar(255) NOT NULL,
836 - bot_id varchar(50) NOT NULL DEFAULT 'default',
837 - rating_value tinyint(1) NOT NULL,
838 - rating_feedback text DEFAULT NULL,
839 - created_at datetime NOT NULL,
840 - PRIMARY KEY (id),
841 - UNIQUE KEY session_id (session_id),
842 - KEY bot_id (bot_id),
843 - KEY created_at (created_at)
844 - ) $charset_collate;";
845 -
846 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
847 - dbDelta($sql);
848 -}
849 -
850 -/**
851 420 * 2.5.2: Fix URL column size to support long URLs (especially with UTF-8 encoding)
852 421 * This fixes "url, source_url. The supplied values may be too long" errors
853 422 */
854 423 function mxchat_fix_url_column_size() {
@@ -871,80 +440,22 @@
871 440
872 441 /**
873 442 * Migrate deprecated AI models to their replacements
874 443 * Version 2.5.1: Migrate Claude 3.5 Sonnet (deprecated) to Claude 3.7 Sonnet
875 - * Version 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support
876 - * Without utf8mb4, any bot response containing emojis silently fails to insert.
877 444 */
878 -function mxchat_migrate_transcripts_charset() {
879 - global $wpdb;
880 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
881 - $wpdb->query("ALTER TABLE $table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
882 -}
883 -
884 -/**
885 - * Version 3.0.55: Migrate GPT-4 series models (deprecated 2026-02-17) to GPT-5 series
886 - */
887 445 function mxchat_migrate_deprecated_models() {
888 446 $options = get_option('mxchat_options', array());
889 - $migrated = false;
890 - $migration_message = '';
891 -
892 - if (!isset($options['model'])) {
893 - return;
894 - }
895 -
896 - $current_model = $options['model'];
897 -
898 - // Migrate deprecated Claude models to Claude Opus 4.6 (recommended replacement per Anthropic)
899 - $deprecated_claude_models = array(
900 - 'claude-3-5-sonnet-20240620', // Retired Oct 28, 2025
901 - 'claude-3-5-sonnet-20241022', // Retired Oct 28, 2025
902 - 'claude-3-7-sonnet-20250219', // Retiring Feb 19, 2026
903 - 'claude-3-opus-20240229', // Retired Jan 5, 2026
904 - 'claude-3-sonnet-20240229', // Legacy
905 - 'claude-3-haiku-20240307', // Legacy
906 - );
907 - if (in_array($current_model, $deprecated_claude_models, true)) {
908 - $options['model'] = 'claude-opus-4-6';
909 - $migrated = true;
910 - $migration_message = sprintf(
911 - 'Your chatbot model has been automatically updated from %s to Claude Opus 4.6 due to Anthropic deprecating older Claude models.',
912 - $current_model
913 - );
914 - }
915 -
916 - // Migrate deprecated Claude Haiku 3.5 to Claude Haiku 4.5
917 - if ($current_model === 'claude-3-5-haiku-20241022') {
918 - $options['model'] = 'claude-haiku-4-5-20251001';
919 - $migrated = true;
920 - $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.';
921 - }
922 -
923 - // Migrate deprecated GPT-4 series and GPT-3.5 Turbo to GPT-5.1 Chat Latest
924 - if (in_array($current_model, array('gpt-4o', 'gpt-4.1-2025-04-14', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'), true)) {
925 - $options['model'] = 'gpt-5.1-chat-latest';
926 - $migrated = true;
927 - $migration_message = sprintf(
928 - 'Your chatbot model has been automatically updated from %s to GPT-5.1 Chat Latest due to OpenAI deprecating older models.',
929 - $current_model
930 - );
931 - }
932 -
933 - // Migrate deprecated GPT-4o Mini and GPT-4.1 Mini to GPT-5 Mini
934 - if (in_array($current_model, array('gpt-4o-mini', 'gpt-4.1-mini'), true)) {
935 - $options['model'] = 'gpt-5-mini';
936 - $migrated = true;
937 - $migration_message = sprintf(
938 - 'Your chatbot model has been automatically updated from %s to GPT-5 Mini due to OpenAI deprecating GPT-4 series models.',
939 - $current_model
940 - );
941 - }
942 -
943 - if ($migrated) {
447 +
448 + // Check if model is set and is the deprecated Claude 3.5 Sonnet
449 + if (isset($options['model']) && $options['model'] === 'claude-3-5-sonnet-20241022') {
450 + // Update to Claude 3.7 Sonnet (the replacement model)
451 + $options['model'] = 'claude-3-7-sonnet-20250219';
944 452 update_option('mxchat_options', $options);
453 +
454 + // Set a flag to show admin notice
945 455 update_option('mxchat_model_migrated_notice', true);
946 - update_option('mxchat_model_migration_message', $migration_message);
456 +
457 + //error_log('MxChat: Migrated deprecated Claude 3.5 Sonnet to Claude 3.7 Sonnet');
947 458 }
948 459 }
949 460
950 461 /**
@@ -951,19 +462,17 @@
951 462 * Show admin notice after model migration
952 463 */
953 464 function mxchat_show_migration_notice() {
954 465 if (get_option('mxchat_model_migrated_notice')) {
955 - $migration_message = get_option('mxchat_model_migration_message', __('Your chatbot model has been automatically updated due to a model deprecation.', 'mxchat'));
956 466 ?>
957 467 <div class="notice notice-info is-dismissible">
958 468 <p>
959 469 <strong><?php esc_html_e('MxChat Model Updated', 'mxchat'); ?></strong><br>
960 - <?php echo esc_html($migration_message); ?>
470 + <?php esc_html_e('Your chatbot model has been automatically updated from Claude 3.5 Sonnet to Claude 3.7 Sonnet due to the deprecation of the previous model by Anthropic. Claude 3.7 Sonnet offers improved performance and capabilities.', 'mxchat'); ?>
961 471 </p>
962 472 </div>
963 473 <?php
964 474 delete_option('mxchat_model_migrated_notice');
965 - delete_option('mxchat_model_migration_message');
966 475 }
967 476 }
968 477
969 478 function mxchat_activate() {
@@ -1003,26 +512,13 @@
1003 512 enabled_bots LONGTEXT DEFAULT NULL,
1004 513 PRIMARY KEY (id)
1005 514 ) $charset_collate;";
1006 515
1007 - // Individual Intent Phrases Table - each phrase gets its own embedding vector
1008 - $intent_phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
1009 - $sql_intent_phrases_table = "CREATE TABLE $intent_phrases_table (
1010 - id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
1011 - intent_id BIGINT(20) UNSIGNED NOT NULL,
1012 - phrase TEXT NOT NULL,
1013 - embedding_vector LONGTEXT NOT NULL,
1014 - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1015 - PRIMARY KEY (id),
1016 - KEY intent_id (intent_id)
1017 - ) $charset_collate;";
1018 -
1019 516 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
1020 517
1021 518 // Create other tables
1022 519 dbDelta($sql_system_prompt);
1023 520 dbDelta($sql_intents_table);
1024 - dbDelta($sql_intent_phrases_table);
1025 521
1026 522 // Create URL click tracking table
1027 523 mxchat_create_url_clicks_table();
1028 524
@@ -1031,14 +527,8 @@
1031 527
1032 528 // NEW 2.5.2: Create queue processing tables
1033 529 mxchat_create_queue_tables();
1034 530
1035 - // Create transcript translations table
1036 - mxchat_create_translations_table();
1037 -
1038 - // Create per-session satisfaction ratings table (v3.2.6)
1039 - mxchat_create_session_ratings_table();
1040 -
1041 531 // Ensure additional columns in system prompt table
1042 532 $existing_system_columns = $wpdb->get_results("SHOW COLUMNS FROM $system_prompt_table");
1043 533 if (!empty($existing_system_columns)) {
1044 534 $existing_system_column_names = array_column($existing_system_columns, 'Field');
@@ -1081,11 +571,8 @@
1081 571
1082 572 // Run migration for existing installations
1083 573 mxchat_migrate_pinecone_roles_add_bot_id();
1084 574
1085 - // 3.2.4: Backfill active embedding model option (replaces 3.2.3 column-based tracking)
1086 - mxchat_backfill_active_embedding_model();
1087 -
1088 575 // Setup cron jobs
1089 576 mxchat_setup_cron_jobs();
1090 577
1091 578 // Update version
@@ -1120,14 +607,13 @@
1120 607 // Clear fallback flags if cron scheduling succeeded
1121 608 delete_option('mxchat_use_fallback_rate_limits');
1122 609 }
1123 610
1124 - // Schedule transcript cleanup if configured (bucket dropdown OR custom retention-days > 0)
611 + // Schedule transcript cleanup if configured
1125 612 $transcript_options = get_option('mxchat_transcripts_options', array());
1126 613 $cleanup_interval = isset($transcript_options['mxchat_auto_delete_transcripts']) ? $transcript_options['mxchat_auto_delete_transcripts'] : 'never';
1127 - $custom_retention = isset($transcript_options['mxchat_retention_days']) ? (int) $transcript_options['mxchat_retention_days'] : 0;
1128 -
1129 - if ($cleanup_interval !== 'never' || $custom_retention > 0) {
614 +
615 + if ($cleanup_interval !== 'never') {
1130 616 // Check if not already scheduled
1131 617 if (!wp_next_scheduled('mxchat_cleanup_old_transcripts')) {
1132 618 // Schedule to run daily at 3 AM
1133 619 $next_run = strtotime('tomorrow 3:00 AM');
@@ -1142,9 +628,8 @@
1142 628 function mxchat_deactivate() {
1143 629 // Clear scheduled cron jobs
1144 630 wp_clear_scheduled_hook('mxchat_reset_rate_limits');
1145 631 wp_clear_scheduled_hook('mxchat_cleanup_old_transcripts');
1146 - wp_clear_scheduled_hook('mxchat_send_delayed_transcript');
1147 632
1148 633 // Clear fallback options
1149 634 delete_option('mxchat_use_fallback_rate_limits');
1150 635 delete_option('mxchat_next_rate_limit_check');
@@ -1204,14 +689,11 @@
1204 689
1205 690 // Version-specific migrations
1206 691 if ($current_version !== $plugin_version) {
1207 692 //error_log("MxChat: Version change detected: $current_version -> $plugin_version");
1208 -
693 +
1209 694 // Run live agent update BEFORE updating the stored version
1210 695 mxchat_handle_live_agent_update();
1211 -
1212 - // Run theme migration notice for 3.0.1 (AI theme CSS structure changes)
1213 - mxchat_handle_theme_migration_notice();
1214 696
1215 697 // Run role restriction migration for 2.4.1
1216 698 if (version_compare($current_version, '2.4.1', '<')) {
1217 699 mxchat_add_role_restriction_column();
@@ -1232,43 +714,9 @@
1232 714 mxchat_create_queue_tables();
1233 715 mxchat_fix_url_column_size(); // NEW: Fix URL column size for long URLs
1234 716 //error_log("MxChat: Queue tables created and URL columns updated for upgrade to 2.5.2");
1235 717 }
1236 -
1237 - // 2.6.0: Ensure rag_context column exists for retrieved documents feature
1238 - if (version_compare($current_version, '2.6.0', '<')) {
1239 - $chat_table = $wpdb->prefix . 'mxchat_chat_transcripts';
1240 - mxchat_ensure_all_columns($chat_table);
1241 - //error_log("MxChat: rag_context column migration for 2.6.0");
1242 - }
1243 -
1244 - // 3.0.5: Migrate deprecated Gemini embedding model
1245 - if (version_compare($current_version, '3.0.5', '<')) {
1246 - mxchat_migrate_gemini_embedding_model();
1247 - }
1248 -
1249 - // 3.0.6: Migrate deprecated OpenAI and Claude models
1250 - if (version_compare($current_version, '3.0.6', '<')) {
1251 - mxchat_migrate_deprecated_models();
1252 - }
1253 -
1254 - // 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support
1255 - if (version_compare($current_version, '3.1.2', '<')) {
1256 - mxchat_migrate_transcripts_charset();
1257 - }
1258 -
1259 - // 3.1.7: Clean up stale shared session email/name entries
1260 - if (version_compare($current_version, '3.1.7', '<')) {
1261 - delete_option('mxchat_email_null');
1262 - delete_option('mxchat_name_null');
1263 - }
1264 -
1265 - // 3.2.4: Backfill active embedding model option for the warning UI
1266 - // (replaces the per-row column tracking from 3.2.3, which was reverted)
1267 - if (version_compare($current_version, '3.2.4', '<')) {
1268 - mxchat_backfill_active_embedding_model();
1269 - }
1270 -
718 +
1271 719 // Run full activation to ensure everything is up to date
1272 720 mxchat_activate();
1273 721
1274 722 // Run migration functions
@@ -1400,59 +848,31 @@
1400 848 function mxchat_handle_live_agent_update() {
1401 849 // Get the CURRENT stored version (before it gets updated)
1402 850 $current_version = get_option('mxchat_plugin_version', '0.0.0');
1403 851 $new_version = '2.2.2';
1404 -
852 +
1405 853 // Only run this once for the update to 2.2.2
1406 854 $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false);
1407 -
855 +
1408 856 // Check if we're upgrading TO 2.2.2 and haven't handled this yet
1409 857 if (version_compare($current_version, $new_version, '<') && !$update_handled) {
1410 858 $options = get_option('mxchat_options', array());
1411 -
859 +
1412 860 // Check if live agent was previously enabled
1413 861 if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') {
1414 862 // Disable live agent
1415 863 $options['live_agent_status'] = 'off';
1416 864 update_option('mxchat_options', $options);
1417 -
865 +
1418 866 // Set flag to show the notification banner
1419 867 update_option('mxchat_show_live_agent_disabled_notice', true);
1420 868 }
1421 -
869 +
1422 870 // Mark this update as handled
1423 871 update_option('mxchat_live_agent_update_2_2_2_handled', true);
1424 872 }
1425 873 }
1426 874
1427 -/**
1428 - * Handle theme migration notice for version 3.0.1
1429 - * Shows a dismissible notice to Pro users about migrating AI-generated themes
1430 - */
1431 -function mxchat_handle_theme_migration_notice() {
1432 - // Get the CURRENT stored version (before it gets updated)
1433 - $current_version = get_option('mxchat_plugin_version', '0.0.0');
1434 - $target_version = '3.0.1';
1435 -
1436 - // Only run this once for the update to 3.0.1
1437 - $update_handled = get_option('mxchat_theme_migration_update_3_0_1_handled', false);
1438 -
1439 - // Check if we're upgrading TO 3.0.1 and haven't handled this yet
1440 - if (version_compare($current_version, $target_version, '<') && !$update_handled) {
1441 - // Check if Pro is activated - only show to Pro users
1442 - $license_status = get_option('mxchat_license_status', 'inactive');
1443 - $is_pro = ($license_status === 'active');
1444 -
1445 - if ($is_pro) {
1446 - // Set flag to show the theme migration notification banner
1447 - update_option('mxchat_show_theme_migration_notice', true);
1448 - }
1449 -
1450 - // Mark this update as handled (whether Pro or not)
1451 - update_option('mxchat_theme_migration_update_3_0_1_handled', true);
1452 - }
1453 -}
1454 -
1455 875 // Initialize plugin safely
1456 876 function mxchat_init() {
1457 877 // Include all class files first
1458 878 mxchat_include_classes();
@@ -1484,25 +904,10 @@
1484 904 // Initialize meta box class
1485 905 if (class_exists('MxChat_Meta_Box')) {
1486 906 new MxChat_Meta_Box();
1487 907 }
1488 -
1489 908 }
1490 -
1491 - // Initialize content generator globally — it registers wp_head hook
1492 - // for frontend CSS injection, plus wp_ajax_ hooks for admin.
1493 - if (class_exists('MxChat_Content_Generator')) {
1494 - new MxChat_Content_Generator();
1495 - }
1496 -
1497 - // Initialize REST API globally — endpoints must be registered on
1498 - // every request (admin and frontend) so they're reachable via /wp-json/.
1499 - // Endpoints are auth-gated and locked until the site owner generates
1500 - // a token in MxChat → API Access.
1501 - if (class_exists('MxChat_Rest_Api')) {
1502 - new MxChat_Rest_Api();
1503 - }
1504 -
909 +
1505 910 // Initialize public classes
1506 911 if (class_exists('MxChat_Public')) {
1507 912 $mxchat_public = new MxChat_Public();
1508 913 }
@@ -1546,57 +951,10 @@
1546 951 $checked = true;
1547 952
1548 953 mxchat_migrate_pinecone_roles_add_bot_id();
1549 954 mxchat_migrate_add_content_type_column();
1550 - mxchat_migrate_add_translations_table();
1551 - mxchat_migrate_add_session_ratings_table();
1552 955 }
1553 956
1554 -/**
1555 - * Migration: Create per-session satisfaction ratings table (v3.2.6)
1556 - * For users upgrading from versions before 3.2.6
1557 - */
1558 -function mxchat_migrate_add_session_ratings_table() {
1559 - $migration_key = 'mxchat_session_ratings_table_created';
1560 - if (get_option($migration_key)) {
1561 - return;
1562 - }
1563 - mxchat_create_session_ratings_table();
1564 - update_option($migration_key, '3.2.6');
1565 -}
1566 -
1567 -/**
1568 - * Migration: Create transcript translations table (v3.0.4)
1569 - * For users upgrading from versions before 3.0.4
1570 - */
1571 -function mxchat_migrate_add_translations_table() {
1572 - $migration_key = 'mxchat_translations_table_created';
1573 -
1574 - // Check if migration already ran
1575 - if (get_option($migration_key)) {
1576 - return;
1577 - }
1578 -
1579 - // Create the translations table
1580 - mxchat_create_translations_table();
1581 -
1582 - // Mark migration as complete
1583 - update_option($migration_key, '3.0.4');
1584 -}
1585 -
1586 -/**
1587 - * Migration: Update deprecated Gemini embedding model (v3.0.5)
1588 - * Updates gemini-embedding-exp-03-07 to gemini-embedding-001 for users who had it selected
1589 - */
1590 -function mxchat_migrate_gemini_embedding_model() {
1591 - $options = get_option('mxchat_options', array());
1592 -
1593 - if (isset($options['embedding_model']) && $options['embedding_model'] === 'gemini-embedding-exp-03-07') {
1594 - $options['embedding_model'] = 'gemini-embedding-001';
1595 - update_option('mxchat_options', $options);
1596 - }
1597 -}
1598 -
1599 957 // Register activation hook
1600 958 register_activation_hook(__FILE__, 'mxchat_activate');
1601 959
1602 960 // Add cron schedule
@@ -1608,67 +966,5 @@
1608 966 return $schedules;
1609 967 });
1610 968
1611 969 // Register deactivation hook
1612 -register_deactivation_hook(__FILE__, 'mxchat_deactivate');
1613 -
1614 -/**
1615 - * Per-session satisfaction rating: AJAX save handler (v3.2.6).
1616 - * Records one 👍/👎 + optional feedback per chat session. The UNIQUE KEY on
1617 - * session_id makes this naturally idempotent — only the first rating per
1618 - * session is stored; duplicate POSTs are silent no-ops.
1619 - */
1620 -function mxchat_save_session_rating() {
1621 - global $wpdb;
1622 -
1623 - $session_id = isset($_POST['session_id']) ? sanitize_text_field(wp_unslash($_POST['session_id'])) : '';
1624 - $bot_id = isset($_POST['bot_id']) ? sanitize_text_field(wp_unslash($_POST['bot_id'])) : 'default';
1625 - $rating_raw = isset($_POST['rating']) ? (int) $_POST['rating'] : 0;
1626 - $feedback = isset($_POST['feedback']) ? sanitize_textarea_field(wp_unslash($_POST['feedback'])) : '';
1627 -
1628 - if ($session_id === '' || ($rating_raw !== 1 && $rating_raw !== -1)) {
1629 - wp_send_json_error(array('message' => 'invalid_input'), 400);
1630 - }
1631 -
1632 - if (strlen($feedback) > 1000) {
1633 - $feedback = substr($feedback, 0, 1000);
1634 - }
1635 -
1636 - $table_name = $wpdb->prefix . 'mxchat_session_ratings';
1637 - $existing = $wpdb->get_var($wpdb->prepare(
1638 - "SELECT id FROM $table_name WHERE session_id = %s LIMIT 1",
1639 - $session_id
1640 - ));
1641 -
1642 - if ($existing) {
1643 - if ($feedback !== '') {
1644 - $wpdb->update(
1645 - $table_name,
1646 - array('rating_feedback' => $feedback),
1647 - array('id' => (int) $existing),
1648 - array('%s'),
1649 - array('%d')
1650 - );
1651 - }
1652 - wp_send_json_success(array('updated' => true));
1653 - }
1654 -
1655 - $inserted = $wpdb->insert(
1656 - $table_name,
1657 - array(
1658 - 'session_id' => $session_id,
1659 - 'bot_id' => $bot_id !== '' ? $bot_id : 'default',
1660 - 'rating_value' => $rating_raw,
1661 - 'rating_feedback' => $feedback !== '' ? $feedback : null,
1662 - 'created_at' => current_time('mysql'),
1663 - ),
1664 - array('%s', '%s', '%d', '%s', '%s')
1665 - );
1666 -
1667 - if ($inserted === false) {
1668 - wp_send_json_error(array('message' => 'db_insert_failed'), 500);
1669 - }
1670 -
1671 - wp_send_json_success(array('saved' => true));
1672 -}
1673 -add_action('wp_ajax_mxchat_save_rating', 'mxchat_save_session_rating');
1674 -add_action('wp_ajax_nopriv_mxchat_save_rating', 'mxchat_save_session_rating');
970 +register_deactivation_hook(__FILE__, 'mxchat_deactivate');