| @@ -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.4 | |
| 6 | + * Version: 2.5.4 | |
| 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,243 +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 | - * Exclude MxChat assets from caching plugin optimizations | |
| 51 | - * | |
| 52 | - * This prevents issues with WP Rocket, LiteSpeed Cache, Autoptimize, WP Super Cache, | |
| 53 | - * W3 Total Cache, SG Optimizer, and similar plugins that may break the chatbot by | |
| 54 | - * removing "unused" CSS, minifying/combining JS, or deferring/delaying jQuery. | |
| 55 | - * | |
| 56 | - * Both chat-script.js and floating-script.js depend on jQuery, so jQuery must also | |
| 57 | - * be excluded from any optimization that changes load order or timing. | |
| 58 | - */ | |
| 59 | - | |
| 60 | -// ── WP Rocket ──────────────────────────────────────────────────────────────── | |
| 61 | - | |
| 62 | -// Exclude from Remove Unused CSS (RUCSS) | |
| 63 | -add_filter('rocket_rucss_inline_atts_exclusions', function($exclusions) { | |
| 64 | - if (!is_array($exclusions)) $exclusions = array(); | |
| 65 | - $exclusions[] = 'mxchat'; | |
| 66 | - return $exclusions; | |
| 67 | -}); | |
| 68 | - | |
| 69 | -// Exclude CSS from minification/combination | |
| 70 | -add_filter('rocket_exclude_css', function($excluded) { | |
| 71 | - if (!is_array($excluded)) $excluded = array(); | |
| 72 | - $excluded[] = '/plugins/mxchat-basic/css/chat-style.css'; | |
| 73 | - return $excluded; | |
| 74 | -}); | |
| 75 | - | |
| 76 | -// Exclude JS from minification/combination | |
| 77 | -add_filter('rocket_exclude_js', function($excluded) { | |
| 78 | - if (!is_array($excluded)) $excluded = array(); | |
| 79 | - $excluded[] = '/plugins/mxchat-basic/js/chat-script.js'; | |
| 80 | - $excluded[] = '/plugins/mxchat-basic/js/floating-script.js'; | |
| 81 | - $excluded[] = '/jquery-core'; | |
| 82 | - $excluded[] = '/jquery.min.js'; | |
| 83 | - $excluded[] = '/jquery.js'; | |
| 84 | - $excluded[] = '/jquery-migrate'; | |
| 85 | - return $excluded; | |
| 86 | -}); | |
| 87 | - | |
| 88 | -// Exclude JS from defer | |
| 89 | -add_filter('rocket_exclude_defer_js', function($excluded) { | |
| 90 | - if (!is_array($excluded)) $excluded = array(); | |
| 91 | - $excluded[] = '/plugins/mxchat-basic/js/chat-script.js'; | |
| 92 | - $excluded[] = '/plugins/mxchat-basic/js/floating-script.js'; | |
| 93 | - $excluded[] = '/jquery-core'; | |
| 94 | - $excluded[] = '/jquery.min.js'; | |
| 95 | - $excluded[] = '/jquery.js'; | |
| 96 | - $excluded[] = '/jquery-migrate'; | |
| 97 | - return $excluded; | |
| 98 | -}); | |
| 99 | - | |
| 100 | -// Exclude from delay JS execution | |
| 101 | -add_filter('rocket_delay_js_exclusions', function($excluded) { | |
| 102 | - if (!is_array($excluded)) $excluded = array(); | |
| 103 | - $excluded[] = 'mxchat'; | |
| 104 | - $excluded[] = 'chat-script'; | |
| 105 | - $excluded[] = 'floating-script'; | |
| 106 | - $excluded[] = '/jquery-core'; | |
| 107 | - $excluded[] = '/jquery.min.js'; | |
| 108 | - $excluded[] = '/jquery.js'; | |
| 109 | - $excluded[] = '/jquery-migrate'; | |
| 110 | - return $excluded; | |
| 111 | -}); | |
| 112 | - | |
| 113 | -// ── LiteSpeed Cache ────────────────────────────────────────────────────────── | |
| 114 | - | |
| 115 | -// Exclude CSS from optimization | |
| 116 | -add_filter('litespeed_optimize_css_excludes', function($excluded) { | |
| 117 | - if (!is_array($excluded)) $excluded = array(); | |
| 118 | - $excluded[] = 'chat-style.css'; | |
| 119 | - $excluded[] = 'mxchat'; | |
| 120 | - return $excluded; | |
| 121 | -}); | |
| 122 | - | |
| 123 | -// Exclude from UCSS (Unique CSS) - prevents LiteSpeed from stripping "unused" MxChat CSS | |
| 124 | -add_filter('litespeed_ucss_whitelist', function($whitelist) { | |
| 125 | - if (!is_array($whitelist)) $whitelist = array(); | |
| 126 | - $whitelist[] = '.mxchat-chatbot-wrapper'; | |
| 127 | - $whitelist[] = '.floating-chatbot'; | |
| 128 | - $whitelist[] = '.floating-chatbot-button'; | |
| 129 | - $whitelist[] = '.chatbot-top-bar'; | |
| 130 | - $whitelist[] = '.mxchat-chatbot'; | |
| 131 | - $whitelist[] = '.chat-container'; | |
| 132 | - $whitelist[] = '.chat-box'; | |
| 133 | - $whitelist[] = '.bot-message'; | |
| 134 | - $whitelist[] = '.input-container'; | |
| 135 | - $whitelist[] = '.chat-input'; | |
| 136 | - $whitelist[] = '.send-button'; | |
| 137 | - $whitelist[] = '.pre-chat-message'; | |
| 138 | - $whitelist[] = '.mxchat-popular-questions'; | |
| 139 | - $whitelist[] = '.chat-toolbar'; | |
| 140 | - $whitelist[] = '.exit-chat'; | |
| 141 | - $whitelist[] = '.email-blocker'; | |
| 142 | - return $whitelist; | |
| 143 | -}); | |
| 144 | - | |
| 145 | -// Exclude CSS from CCSS (Critical CSS) generation | |
| 146 | -add_filter('litespeed_optm_ccss_exc', function($excluded) { | |
| 147 | - if (!is_array($excluded)) $excluded = array(); | |
| 148 | - $excluded[] = 'chat-style.css'; | |
| 149 | - $excluded[] = 'mxchat'; | |
| 150 | - return $excluded; | |
| 151 | -}); | |
| 152 | - | |
| 153 | -// Exclude JS from defer | |
| 154 | -add_filter('litespeed_optm_js_defer_exc', function($excluded) { | |
| 155 | - if (!is_array($excluded)) $excluded = array(); | |
| 156 | - $excluded[] = 'chat-script.js'; | |
| 157 | - $excluded[] = 'floating-script.js'; | |
| 158 | - $excluded[] = 'mxchat'; | |
| 159 | - $excluded[] = 'jquery.min.js'; | |
| 160 | - $excluded[] = 'jquery.js'; | |
| 161 | - return $excluded; | |
| 162 | -}); | |
| 163 | - | |
| 164 | -// Exclude JS from combining | |
| 165 | -add_filter('litespeed_optm_js_exc', function($excluded) { | |
| 166 | - if (!is_array($excluded)) $excluded = array(); | |
| 167 | - $excluded[] = 'chat-script.js'; | |
| 168 | - $excluded[] = 'floating-script.js'; | |
| 169 | - $excluded[] = 'mxchat'; | |
| 170 | - $excluded[] = 'jquery.min.js'; | |
| 171 | - $excluded[] = 'jquery.js'; | |
| 172 | - return $excluded; | |
| 173 | -}); | |
| 174 | - | |
| 175 | -// Exclude JS from delayed execution | |
| 176 | -add_filter('litespeed_optm_js_delay_exc', function($excluded) { | |
| 177 | - if (!is_array($excluded)) $excluded = array(); | |
| 178 | - $excluded[] = 'chat-script.js'; | |
| 179 | - $excluded[] = 'floating-script.js'; | |
| 180 | - $excluded[] = 'mxchat'; | |
| 181 | - return $excluded; | |
| 182 | -}); | |
| 183 | - | |
| 184 | -// Exclude from Guest Mode optimization | |
| 185 | -add_filter('litespeed_guest_optm_exc', function($excluded) { | |
| 186 | - if (!is_array($excluded)) $excluded = array(); | |
| 187 | - $excluded[] = 'mxchat'; | |
| 188 | - $excluded[] = 'chat-style'; | |
| 189 | - $excluded[] = 'chat-script'; | |
| 190 | - $excluded[] = 'floating-script'; | |
| 191 | - return $excluded; | |
| 192 | -}); | |
| 193 | - | |
| 194 | -// ── Autoptimize ────────────────────────────────────────────────────────────── | |
| 195 | - | |
| 196 | -// Exclude CSS from optimization (comma-separated strings) | |
| 197 | -add_filter('autoptimize_filter_css_exclude', function($excluded) { | |
| 198 | - if (!is_string($excluded)) $excluded = ''; | |
| 199 | - return $excluded . ', mxchat, chat-style.css'; | |
| 200 | -}); | |
| 201 | - | |
| 202 | -// Exclude JS from optimization (comma-separated strings) | |
| 203 | -add_filter('autoptimize_filter_js_exclude', function($excluded) { | |
| 204 | - if (!is_string($excluded)) $excluded = ''; | |
| 205 | - return $excluded . ', mxchat, chat-script.js, floating-script.js, jquery.min.js, jquery.js'; | |
| 206 | -}); | |
| 207 | - | |
| 208 | -// ── SG Optimizer (SiteGround) ──────────────────────────────────────────────── | |
| 209 | - | |
| 210 | -add_filter('sgo_js_minify_exclude', function($excluded) { | |
| 211 | - if (!is_array($excluded)) $excluded = array(); | |
| 212 | - $excluded[] = 'chat-script.js'; | |
| 213 | - $excluded[] = 'floating-script.js'; | |
| 214 | - $excluded[] = 'jquery.min.js'; | |
| 215 | - return $excluded; | |
| 216 | -}); | |
| 217 | - | |
| 218 | -add_filter('sgo_javascript_combine_exclude', function($excluded) { | |
| 219 | - if (!is_array($excluded)) $excluded = array(); | |
| 220 | - $excluded[] = 'chat-script.js'; | |
| 221 | - $excluded[] = 'floating-script.js'; | |
| 222 | - $excluded[] = 'jquery.min.js'; | |
| 223 | - return $excluded; | |
| 224 | -}); | |
| 225 | - | |
| 226 | -add_filter('sgo_js_async_exclude', function($excluded) { | |
| 227 | - if (!is_array($excluded)) $excluded = array(); | |
| 228 | - $excluded[] = 'chat-script.js'; | |
| 229 | - $excluded[] = 'floating-script.js'; | |
| 230 | - $excluded[] = 'jquery.min.js'; | |
| 231 | - return $excluded; | |
| 232 | -}); | |
| 233 | - | |
| 234 | -// ── W3 Total Cache ─────────────────────────────────────────────────────────── | |
| 235 | - | |
| 236 | -add_filter('w3tc_minify_js_do_tag_minification', function($do_minify, $script_tag, $file) { | |
| 237 | - if (strpos($file, 'chat-script.js') !== false || | |
| 238 | - strpos($file, 'floating-script.js') !== false || | |
| 239 | - strpos($file, 'jquery.min.js') !== false || | |
| 240 | - strpos($file, 'jquery.js') !== false) { | |
| 241 | - return false; | |
| 242 | - } | |
| 243 | - return $do_minify; | |
| 244 | -}, 10, 3); | |
| 245 | - | |
| 246 | -// ── WP Super Cache ────────────────────────────────────────────────────────── | |
| 247 | - | |
| 248 | -add_filter('wpsc_rejected_uri', function($rejected) { | |
| 249 | - if (!is_array($rejected)) $rejected = array(); | |
| 250 | - $rejected[] = 'wp-admin/admin-ajax.php'; | |
| 251 | - return $rejected; | |
| 252 | -}); | |
| 253 | - | |
| 254 | 31 | // Include classes with error handling |
| 255 | 32 | function mxchat_include_classes() { |
| 256 | 33 | $class_files = array( |
| 257 | 34 | 'includes/class-mxchat-integrator.php', |
| @@ -259,11 +36,10 @@ | ||
| 259 | 36 | 'includes/class-mxchat-public.php', |
| 260 | 37 | 'includes/class-mxchat-utils.php', |
| 261 | 38 | 'includes/class-mxchat-user.php', |
| 262 | 39 | 'includes/class-mxchat-meta-box.php', |
| 263 | - 'includes/class-mxchat-chunker.php', | |
| 40 | + 'includes/pdf-parser/alt_autoload.php', | |
| 264 | 41 | 'includes/class-mxchat-word-handler.php', |
| 265 | - 'includes/class-mxchat-content-generator.php', | |
| 266 | 42 | 'admin/class-ajax-handler.php', |
| 267 | 43 | 'admin/class-pinecone-manager.php', |
| 268 | 44 | 'admin/class-knowledge-manager.php' |
| 269 | 45 | ); |
| @@ -278,24 +54,8 @@ | ||
| 278 | 54 | } |
| 279 | 55 | } |
| 280 | 56 | |
| 281 | 57 | /** |
| 282 | - * Lazy-load the PDF parser library only when needed. | |
| 283 | - * Avoids loading 44 files on every page request. | |
| 284 | - */ | |
| 285 | -function mxchat_load_pdf_parser() { | |
| 286 | - if (class_exists('\Smalot\PdfParser\Parser')) { | |
| 287 | - return true; | |
| 288 | - } | |
| 289 | - $autoload_path = plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; | |
| 290 | - if (file_exists($autoload_path)) { | |
| 291 | - require_once $autoload_path; | |
| 292 | - return true; | |
| 293 | - } | |
| 294 | - return false; | |
| 295 | -} | |
| 296 | - | |
| 297 | -/** | |
| 298 | 58 | * Create URL click tracking table |
| 299 | 59 | */ |
| 300 | 60 | function mxchat_create_url_clicks_table() { |
| 301 | 61 | global $wpdb; |
| @@ -378,13 +138,12 @@ | ||
| 378 | 138 | |
| 379 | 139 | // Define all required columns and their types |
| 380 | 140 | $required_columns = [ |
| 381 | 141 | 'user_identifier' => 'VARCHAR(255) DEFAULT NULL', |
| 382 | - 'user_email' => 'VARCHAR(255) DEFAULT NULL', | |
| 142 | + 'user_email' => 'VARCHAR(255) DEFAULT NULL', | |
| 383 | 143 | 'user_name' => 'VARCHAR(100) DEFAULT NULL', |
| 384 | 144 | 'originating_page_url' => 'TEXT DEFAULT NULL', |
| 385 | - 'originating_page_title' => 'VARCHAR(500) DEFAULT NULL', | |
| 386 | - 'rag_context' => 'LONGTEXT DEFAULT NULL' | |
| 145 | + 'originating_page_title' => 'VARCHAR(500) DEFAULT NULL' | |
| 387 | 146 | ]; |
| 388 | 147 | |
| 389 | 148 | // Get existing columns |
| 390 | 149 | $existing_columns = $wpdb->get_results("SHOW COLUMNS FROM $table_name"); |
| @@ -504,137 +263,32 @@ | ||
| 504 | 263 | } |
| 505 | 264 | } |
| 506 | 265 | |
| 507 | 266 | /** |
| 508 | - * Create Pinecone role restrictions table with multi-bot support | |
| 267 | + * Create Pinecone role restrictions table | |
| 509 | 268 | */ |
| 510 | 269 | function mxchat_create_pinecone_roles_table() { |
| 511 | 270 | global $wpdb; |
| 512 | - | |
| 271 | + | |
| 513 | 272 | $table_name = $wpdb->prefix . 'mxchat_pinecone_roles'; |
| 514 | 273 | $charset_collate = $wpdb->get_charset_collate(); |
| 515 | - | |
| 274 | + | |
| 516 | 275 | $sql = "CREATE TABLE $table_name ( |
| 517 | 276 | id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 518 | 277 | vector_id varchar(255) NOT NULL, |
| 519 | - bot_id varchar(50) NOT NULL DEFAULT 'default', | |
| 520 | 278 | source_url text, |
| 521 | 279 | role_restriction varchar(50) DEFAULT 'public', |
| 522 | 280 | updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 523 | 281 | PRIMARY KEY (id), |
| 524 | - UNIQUE KEY vector_bot (vector_id, bot_id), | |
| 525 | - KEY role_restriction (role_restriction), | |
| 526 | - KEY bot_id (bot_id) | |
| 282 | + UNIQUE KEY vector_id (vector_id), | |
| 283 | + KEY role_restriction (role_restriction) | |
| 527 | 284 | ) $charset_collate;"; |
| 528 | - | |
| 285 | + | |
| 529 | 286 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 530 | 287 | dbDelta($sql); |
| 531 | 288 | } |
| 532 | 289 | |
| 533 | 290 | /** |
| 534 | - * Add bot_id column to mxchat_pinecone_roles table for multi-bot support | |
| 535 | - * This migration runs once to update existing installations | |
| 536 | - */ | |
| 537 | -function mxchat_migrate_pinecone_roles_add_bot_id() { | |
| 538 | - global $wpdb; | |
| 539 | - | |
| 540 | - // Check if migration already ran | |
| 541 | - $migration_version = get_option('mxchat_pinecone_roles_migration_version', '0'); | |
| 542 | - if (version_compare($migration_version, '2.5.2', '>=')) { | |
| 543 | - return; // Already migrated | |
| 544 | - } | |
| 545 | - | |
| 546 | - $table_name = $wpdb->prefix . 'mxchat_pinecone_roles'; | |
| 547 | - | |
| 548 | - // Check if table exists | |
| 549 | - if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name) { | |
| 550 | - return; // Table doesn't exist yet | |
| 551 | - } | |
| 552 | - | |
| 553 | - // Check if bot_id column already exists | |
| 554 | - $column_exists = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} LIKE 'bot_id'"); | |
| 555 | - | |
| 556 | - if (empty($column_exists)) { | |
| 557 | - // Add bot_id column | |
| 558 | - $wpdb->query("ALTER TABLE {$table_name} ADD COLUMN bot_id VARCHAR(50) NOT NULL DEFAULT 'default' AFTER vector_id"); | |
| 559 | - | |
| 560 | - // Update the unique key to include bot_id | |
| 561 | - $wpdb->query("ALTER TABLE {$table_name} DROP INDEX vector_id"); | |
| 562 | - $wpdb->query("ALTER TABLE {$table_name} ADD UNIQUE KEY vector_bot (vector_id, bot_id)"); | |
| 563 | - | |
| 564 | - // Add index for bot_id | |
| 565 | - $wpdb->query("ALTER TABLE {$table_name} ADD KEY bot_id (bot_id)"); | |
| 566 | - | |
| 567 | - //error_log('MxChat: Successfully added bot_id column to mxchat_pinecone_roles table'); | |
| 568 | - } | |
| 569 | - | |
| 570 | - // Mark migration as complete | |
| 571 | - update_option('mxchat_pinecone_roles_migration_version', '2.5.2'); | |
| 572 | -} | |
| 573 | - | |
| 574 | -/** | |
| 575 | - * 2.5.6: Add content_type column to mxchat_system_prompt_content table | |
| 576 | - * Enables filtering knowledge base by content type (posts, pages, PDFs, etc.) | |
| 577 | - */ | |
| 578 | -function mxchat_migrate_add_content_type_column() { | |
| 579 | - global $wpdb; | |
| 580 | - | |
| 581 | - // Check if migration already ran | |
| 582 | - $migration_version = get_option('mxchat_content_type_migration_version', '0'); | |
| 583 | - if (version_compare($migration_version, '2.5.6', '>=')) { | |
| 584 | - return; | |
| 585 | - } | |
| 586 | - | |
| 587 | - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content'; | |
| 588 | - | |
| 589 | - // Check if table exists | |
| 590 | - if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name) { | |
| 591 | - return; | |
| 592 | - } | |
| 593 | - | |
| 594 | - // Check if content_type column already exists | |
| 595 | - $column_exists = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} LIKE 'content_type'"); | |
| 596 | - | |
| 597 | - if (empty($column_exists)) { | |
| 598 | - // Add content_type column with default value 'content' for backwards compatibility | |
| 599 | - $wpdb->query("ALTER TABLE {$table_name} ADD COLUMN content_type VARCHAR(50) DEFAULT 'content' AFTER role_restriction"); | |
| 600 | - | |
| 601 | - // Add index for better query performance | |
| 602 | - $wpdb->query("ALTER TABLE {$table_name} ADD KEY content_type (content_type)"); | |
| 603 | - | |
| 604 | - //error_log('MxChat: Successfully added content_type column to mxchat_system_prompt_content table'); | |
| 605 | - } | |
| 606 | - | |
| 607 | - // Mark migration as complete | |
| 608 | - update_option('mxchat_content_type_migration_version', '2.5.6'); | |
| 609 | -} | |
| 610 | - | |
| 611 | -/** | |
| 612 | - * 3.2.4: Backfill the active embedding model option for installs that already | |
| 613 | - * have KB content but no stamped model. The mismatch warning compares this | |
| 614 | - * against the user's currently selected model — no per-row column needed. | |
| 615 | - */ | |
| 616 | -function mxchat_backfill_active_embedding_model() { | |
| 617 | - global $wpdb; | |
| 618 | - | |
| 619 | - if (get_option('mxchat_active_embedding_model', '') !== '') { | |
| 620 | - return; | |
| 621 | - } | |
| 622 | - | |
| 623 | - $kb_table = $wpdb->prefix . 'mxchat_system_prompt_content'; | |
| 624 | - if ($wpdb->get_var("SHOW TABLES LIKE '{$kb_table}'") !== $kb_table) { | |
| 625 | - return; | |
| 626 | - } | |
| 627 | - | |
| 628 | - $kb_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$kb_table}"); | |
| 629 | - if ($kb_count > 0) { | |
| 630 | - $options = get_option('mxchat_options', array()); | |
| 631 | - $current_model = $options['embedding_model'] ?? 'text-embedding-ada-002'; | |
| 632 | - update_option('mxchat_active_embedding_model', $current_model, false); | |
| 633 | - } | |
| 634 | -} | |
| 635 | - | |
| 636 | -/** | |
| 637 | 291 | * 2.5.2: Create queue processing tables for reliable background processing |
| 638 | 292 | */ |
| 639 | 293 | function mxchat_create_queue_tables() { |
| 640 | 294 | global $wpdb; |
| @@ -682,32 +336,8 @@ | ||
| 682 | 336 | //error_log("MxChat: Queue tables created/updated successfully"); |
| 683 | 337 | } |
| 684 | 338 | |
| 685 | 339 | /** |
| 686 | - * Create transcript translations table for persisting translations | |
| 687 | - */ | |
| 688 | -function mxchat_create_translations_table() { | |
| 689 | - global $wpdb; | |
| 690 | - $charset_collate = $wpdb->get_charset_collate(); | |
| 691 | - | |
| 692 | - $table_name = $wpdb->prefix . 'mxchat_transcript_translations'; | |
| 693 | - $sql = "CREATE TABLE $table_name ( | |
| 694 | - id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 695 | - session_id varchar(255) NOT NULL, | |
| 696 | - language_code varchar(10) NOT NULL, | |
| 697 | - translations longtext NOT NULL, | |
| 698 | - created_at datetime NOT NULL, | |
| 699 | - updated_at datetime NOT NULL, | |
| 700 | - PRIMARY KEY (id), | |
| 701 | - UNIQUE KEY session_lang (session_id, language_code), | |
| 702 | - KEY session_id (session_id) | |
| 703 | - ) $charset_collate;"; | |
| 704 | - | |
| 705 | - require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
| 706 | - dbDelta($sql); | |
| 707 | -} | |
| 708 | - | |
| 709 | -/** | |
| 710 | 340 | * 2.5.2: Fix URL column size to support long URLs (especially with UTF-8 encoding) |
| 711 | 341 | * This fixes "url, source_url. The supplied values may be too long" errors |
| 712 | 342 | */ |
| 713 | 343 | function mxchat_fix_url_column_size() { |
| @@ -730,80 +360,22 @@ | ||
| 730 | 360 | |
| 731 | 361 | /** |
| 732 | 362 | * Migrate deprecated AI models to their replacements |
| 733 | 363 | * Version 2.5.1: Migrate Claude 3.5 Sonnet (deprecated) to Claude 3.7 Sonnet |
| 734 | - * Version 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support | |
| 735 | - * Without utf8mb4, any bot response containing emojis silently fails to insert. | |
| 736 | 364 | */ |
| 737 | -function mxchat_migrate_transcripts_charset() { | |
| 738 | - global $wpdb; | |
| 739 | - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts'; | |
| 740 | - $wpdb->query("ALTER TABLE $table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); | |
| 741 | -} | |
| 742 | - | |
| 743 | -/** | |
| 744 | - * Version 3.0.55: Migrate GPT-4 series models (deprecated 2026-02-17) to GPT-5 series | |
| 745 | - */ | |
| 746 | 365 | function mxchat_migrate_deprecated_models() { |
| 747 | 366 | $options = get_option('mxchat_options', array()); |
| 748 | - $migrated = false; | |
| 749 | - $migration_message = ''; | |
| 750 | - | |
| 751 | - if (!isset($options['model'])) { | |
| 752 | - return; | |
| 753 | - } | |
| 754 | - | |
| 755 | - $current_model = $options['model']; | |
| 756 | - | |
| 757 | - // Migrate deprecated Claude models to Claude Opus 4.6 (recommended replacement per Anthropic) | |
| 758 | - $deprecated_claude_models = array( | |
| 759 | - 'claude-3-5-sonnet-20240620', // Retired Oct 28, 2025 | |
| 760 | - 'claude-3-5-sonnet-20241022', // Retired Oct 28, 2025 | |
| 761 | - 'claude-3-7-sonnet-20250219', // Retiring Feb 19, 2026 | |
| 762 | - 'claude-3-opus-20240229', // Retired Jan 5, 2026 | |
| 763 | - 'claude-3-sonnet-20240229', // Legacy | |
| 764 | - 'claude-3-haiku-20240307', // Legacy | |
| 765 | - ); | |
| 766 | - if (in_array($current_model, $deprecated_claude_models, true)) { | |
| 767 | - $options['model'] = 'claude-opus-4-6'; | |
| 768 | - $migrated = true; | |
| 769 | - $migration_message = sprintf( | |
| 770 | - 'Your chatbot model has been automatically updated from %s to Claude Opus 4.6 due to Anthropic deprecating older Claude models.', | |
| 771 | - $current_model | |
| 772 | - ); | |
| 773 | - } | |
| 774 | - | |
| 775 | - // Migrate deprecated Claude Haiku 3.5 to Claude Haiku 4.5 | |
| 776 | - if ($current_model === 'claude-3-5-haiku-20241022') { | |
| 777 | - $options['model'] = 'claude-haiku-4-5-20251001'; | |
| 778 | - $migrated = true; | |
| 779 | - $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.'; | |
| 780 | - } | |
| 781 | - | |
| 782 | - // Migrate deprecated GPT-4 series and GPT-3.5 Turbo to GPT-5.1 Chat Latest | |
| 783 | - if (in_array($current_model, array('gpt-4o', 'gpt-4.1-2025-04-14', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'), true)) { | |
| 784 | - $options['model'] = 'gpt-5.1-chat-latest'; | |
| 785 | - $migrated = true; | |
| 786 | - $migration_message = sprintf( | |
| 787 | - 'Your chatbot model has been automatically updated from %s to GPT-5.1 Chat Latest due to OpenAI deprecating older models.', | |
| 788 | - $current_model | |
| 789 | - ); | |
| 790 | - } | |
| 791 | - | |
| 792 | - // Migrate deprecated GPT-4o Mini and GPT-4.1 Mini to GPT-5 Mini | |
| 793 | - if (in_array($current_model, array('gpt-4o-mini', 'gpt-4.1-mini'), true)) { | |
| 794 | - $options['model'] = 'gpt-5-mini'; | |
| 795 | - $migrated = true; | |
| 796 | - $migration_message = sprintf( | |
| 797 | - 'Your chatbot model has been automatically updated from %s to GPT-5 Mini due to OpenAI deprecating GPT-4 series models.', | |
| 798 | - $current_model | |
| 799 | - ); | |
| 800 | - } | |
| 801 | - | |
| 802 | - if ($migrated) { | |
| 367 | + | |
| 368 | + // Check if model is set and is the deprecated Claude 3.5 Sonnet | |
| 369 | + if (isset($options['model']) && $options['model'] === 'claude-3-5-sonnet-20241022') { | |
| 370 | + // Update to Claude 3.7 Sonnet (the replacement model) | |
| 371 | + $options['model'] = 'claude-3-7-sonnet-20250219'; | |
| 803 | 372 | update_option('mxchat_options', $options); |
| 373 | + | |
| 374 | + // Set a flag to show admin notice | |
| 804 | 375 | update_option('mxchat_model_migrated_notice', true); |
| 805 | - update_option('mxchat_model_migration_message', $migration_message); | |
| 376 | + | |
| 377 | + //error_log('MxChat: Migrated deprecated Claude 3.5 Sonnet to Claude 3.7 Sonnet'); | |
| 806 | 378 | } |
| 807 | 379 | } |
| 808 | 380 | |
| 809 | 381 | /** |
| @@ -810,19 +382,17 @@ | ||
| 810 | 382 | * Show admin notice after model migration |
| 811 | 383 | */ |
| 812 | 384 | function mxchat_show_migration_notice() { |
| 813 | 385 | if (get_option('mxchat_model_migrated_notice')) { |
| 814 | - $migration_message = get_option('mxchat_model_migration_message', __('Your chatbot model has been automatically updated due to a model deprecation.', 'mxchat')); | |
| 815 | 386 | ?> |
| 816 | 387 | <div class="notice notice-info is-dismissible"> |
| 817 | 388 | <p> |
| 818 | 389 | <strong><?php esc_html_e('MxChat Model Updated', 'mxchat'); ?></strong><br> |
| 819 | - <?php echo esc_html($migration_message); ?> | |
| 390 | + <?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'); ?> | |
| 820 | 391 | </p> |
| 821 | 392 | </div> |
| 822 | 393 | <?php |
| 823 | 394 | delete_option('mxchat_model_migrated_notice'); |
| 824 | - delete_option('mxchat_model_migration_message'); | |
| 825 | 395 | } |
| 826 | 396 | } |
| 827 | 397 | |
| 828 | 398 | function mxchat_activate() { |
| @@ -842,12 +412,10 @@ | ||
| 842 | 412 | article_content LONGTEXT NOT NULL, |
| 843 | 413 | embedding_vector LONGTEXT, |
| 844 | 414 | source_url TEXT DEFAULT NULL, |
| 845 | 415 | role_restriction VARCHAR(50) DEFAULT 'public', |
| 846 | - content_type VARCHAR(50) DEFAULT 'content', | |
| 847 | 416 | timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 848 | - PRIMARY KEY (id), | |
| 849 | - KEY content_type (content_type) | |
| 417 | + PRIMARY KEY (id) | |
| 850 | 418 | ) $charset_collate;"; |
| 851 | 419 | |
| 852 | 420 | // Intents Table - NOW INCLUDES enabled_bots column from the start |
| 853 | 421 | $intents_table = $wpdb->prefix . 'mxchat_intents'; |
| @@ -862,26 +430,13 @@ | ||
| 862 | 430 | enabled_bots LONGTEXT DEFAULT NULL, |
| 863 | 431 | PRIMARY KEY (id) |
| 864 | 432 | ) $charset_collate;"; |
| 865 | 433 | |
| 866 | - // Individual Intent Phrases Table - each phrase gets its own embedding vector | |
| 867 | - $intent_phrases_table = $wpdb->prefix . 'mxchat_intent_phrases'; | |
| 868 | - $sql_intent_phrases_table = "CREATE TABLE $intent_phrases_table ( | |
| 869 | - id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, | |
| 870 | - intent_id BIGINT(20) UNSIGNED NOT NULL, | |
| 871 | - phrase TEXT NOT NULL, | |
| 872 | - embedding_vector LONGTEXT NOT NULL, | |
| 873 | - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | |
| 874 | - PRIMARY KEY (id), | |
| 875 | - KEY intent_id (intent_id) | |
| 876 | - ) $charset_collate;"; | |
| 877 | - | |
| 878 | 434 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 879 | 435 | |
| 880 | 436 | // Create other tables |
| 881 | 437 | dbDelta($sql_system_prompt); |
| 882 | 438 | dbDelta($sql_intents_table); |
| 883 | - dbDelta($sql_intent_phrases_table); | |
| 884 | 439 | |
| 885 | 440 | // Create URL click tracking table |
| 886 | 441 | mxchat_create_url_clicks_table(); |
| 887 | 442 | |
| @@ -890,11 +445,8 @@ | ||
| 890 | 445 | |
| 891 | 446 | // NEW 2.5.2: Create queue processing tables |
| 892 | 447 | mxchat_create_queue_tables(); |
| 893 | 448 | |
| 894 | - // Create transcript translations table | |
| 895 | - mxchat_create_translations_table(); | |
| 896 | - | |
| 897 | 449 | // Ensure additional columns in system prompt table |
| 898 | 450 | $existing_system_columns = $wpdb->get_results("SHOW COLUMNS FROM $system_prompt_table"); |
| 899 | 451 | if (!empty($existing_system_columns)) { |
| 900 | 452 | $existing_system_column_names = array_column($existing_system_columns, 'Field'); |
| @@ -934,20 +486,14 @@ | ||
| 934 | 486 | )); |
| 935 | 487 | } |
| 936 | 488 | } |
| 937 | 489 | |
| 938 | - // Run migration for existing installations | |
| 939 | - mxchat_migrate_pinecone_roles_add_bot_id(); | |
| 940 | - | |
| 941 | - // 3.2.4: Backfill active embedding model option (replaces 3.2.3 column-based tracking) | |
| 942 | - mxchat_backfill_active_embedding_model(); | |
| 943 | - | |
| 944 | 490 | // Setup cron jobs |
| 945 | 491 | mxchat_setup_cron_jobs(); |
| 946 | 492 | |
| 947 | 493 | // Update version |
| 948 | 494 | update_option('mxchat_plugin_version', MXCHAT_VERSION); |
| 949 | - | |
| 495 | + | |
| 950 | 496 | //error_log("MxChat: Activation function completed"); |
| 951 | 497 | } |
| 952 | 498 | |
| 953 | 499 | /** |
| @@ -975,21 +521,8 @@ | ||
| 975 | 521 | } else { |
| 976 | 522 | // Clear fallback flags if cron scheduling succeeded |
| 977 | 523 | delete_option('mxchat_use_fallback_rate_limits'); |
| 978 | 524 | } |
| 979 | - | |
| 980 | - // Schedule transcript cleanup if configured | |
| 981 | - $transcript_options = get_option('mxchat_transcripts_options', array()); | |
| 982 | - $cleanup_interval = isset($transcript_options['mxchat_auto_delete_transcripts']) ? $transcript_options['mxchat_auto_delete_transcripts'] : 'never'; | |
| 983 | - | |
| 984 | - if ($cleanup_interval !== 'never') { | |
| 985 | - // Check if not already scheduled | |
| 986 | - if (!wp_next_scheduled('mxchat_cleanup_old_transcripts')) { | |
| 987 | - // Schedule to run daily at 3 AM | |
| 988 | - $next_run = strtotime('tomorrow 3:00 AM'); | |
| 989 | - wp_schedule_event($next_run, 'daily', 'mxchat_cleanup_old_transcripts'); | |
| 990 | - } | |
| 991 | - } | |
| 992 | 525 | } |
| 993 | 526 | |
| 994 | 527 | /** |
| 995 | 528 | * Clean up on plugin deactivation |
| @@ -996,10 +529,8 @@ | ||
| 996 | 529 | */ |
| 997 | 530 | function mxchat_deactivate() { |
| 998 | 531 | // Clear scheduled cron jobs |
| 999 | 532 | wp_clear_scheduled_hook('mxchat_reset_rate_limits'); |
| 1000 | - wp_clear_scheduled_hook('mxchat_cleanup_old_transcripts'); | |
| 1001 | - wp_clear_scheduled_hook('mxchat_send_delayed_transcript'); | |
| 1002 | 533 | |
| 1003 | 534 | // Clear fallback options |
| 1004 | 535 | delete_option('mxchat_use_fallback_rate_limits'); |
| 1005 | 536 | delete_option('mxchat_next_rate_limit_check'); |
| @@ -1059,14 +590,11 @@ | ||
| 1059 | 590 | |
| 1060 | 591 | // Version-specific migrations |
| 1061 | 592 | if ($current_version !== $plugin_version) { |
| 1062 | 593 | //error_log("MxChat: Version change detected: $current_version -> $plugin_version"); |
| 1063 | - | |
| 594 | + | |
| 1064 | 595 | // Run live agent update BEFORE updating the stored version |
| 1065 | 596 | mxchat_handle_live_agent_update(); |
| 1066 | - | |
| 1067 | - // Run theme migration notice for 3.0.1 (AI theme CSS structure changes) | |
| 1068 | - mxchat_handle_theme_migration_notice(); | |
| 1069 | 597 | |
| 1070 | 598 | // Run role restriction migration for 2.4.1 |
| 1071 | 599 | if (version_compare($current_version, '2.4.1', '<')) { |
| 1072 | 600 | mxchat_add_role_restriction_column(); |
| @@ -1087,43 +615,9 @@ | ||
| 1087 | 615 | mxchat_create_queue_tables(); |
| 1088 | 616 | mxchat_fix_url_column_size(); // NEW: Fix URL column size for long URLs |
| 1089 | 617 | //error_log("MxChat: Queue tables created and URL columns updated for upgrade to 2.5.2"); |
| 1090 | 618 | } |
| 1091 | - | |
| 1092 | - // 2.6.0: Ensure rag_context column exists for retrieved documents feature | |
| 1093 | - if (version_compare($current_version, '2.6.0', '<')) { | |
| 1094 | - $chat_table = $wpdb->prefix . 'mxchat_chat_transcripts'; | |
| 1095 | - mxchat_ensure_all_columns($chat_table); | |
| 1096 | - //error_log("MxChat: rag_context column migration for 2.6.0"); | |
| 1097 | - } | |
| 1098 | - | |
| 1099 | - // 3.0.5: Migrate deprecated Gemini embedding model | |
| 1100 | - if (version_compare($current_version, '3.0.5', '<')) { | |
| 1101 | - mxchat_migrate_gemini_embedding_model(); | |
| 1102 | - } | |
| 1103 | - | |
| 1104 | - // 3.0.6: Migrate deprecated OpenAI and Claude models | |
| 1105 | - if (version_compare($current_version, '3.0.6', '<')) { | |
| 1106 | - mxchat_migrate_deprecated_models(); | |
| 1107 | - } | |
| 1108 | - | |
| 1109 | - // 3.1.2: Convert chat transcripts table to utf8mb4 for emoji support | |
| 1110 | - if (version_compare($current_version, '3.1.2', '<')) { | |
| 1111 | - mxchat_migrate_transcripts_charset(); | |
| 1112 | - } | |
| 1113 | - | |
| 1114 | - // 3.1.7: Clean up stale shared session email/name entries | |
| 1115 | - if (version_compare($current_version, '3.1.7', '<')) { | |
| 1116 | - delete_option('mxchat_email_null'); | |
| 1117 | - delete_option('mxchat_name_null'); | |
| 1118 | - } | |
| 1119 | - | |
| 1120 | - // 3.2.4: Backfill active embedding model option for the warning UI | |
| 1121 | - // (replaces the per-row column tracking from 3.2.3, which was reverted) | |
| 1122 | - if (version_compare($current_version, '3.2.4', '<')) { | |
| 1123 | - mxchat_backfill_active_embedding_model(); | |
| 1124 | - } | |
| 1125 | - | |
| 619 | + | |
| 1126 | 620 | // Run full activation to ensure everything is up to date |
| 1127 | 621 | mxchat_activate(); |
| 1128 | 622 | |
| 1129 | 623 | // Run migration functions |
| @@ -1255,59 +749,31 @@ | ||
| 1255 | 749 | function mxchat_handle_live_agent_update() { |
| 1256 | 750 | // Get the CURRENT stored version (before it gets updated) |
| 1257 | 751 | $current_version = get_option('mxchat_plugin_version', '0.0.0'); |
| 1258 | 752 | $new_version = '2.2.2'; |
| 1259 | - | |
| 753 | + | |
| 1260 | 754 | // Only run this once for the update to 2.2.2 |
| 1261 | 755 | $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false); |
| 1262 | - | |
| 756 | + | |
| 1263 | 757 | // Check if we're upgrading TO 2.2.2 and haven't handled this yet |
| 1264 | 758 | if (version_compare($current_version, $new_version, '<') && !$update_handled) { |
| 1265 | 759 | $options = get_option('mxchat_options', array()); |
| 1266 | - | |
| 760 | + | |
| 1267 | 761 | // Check if live agent was previously enabled |
| 1268 | 762 | if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') { |
| 1269 | 763 | // Disable live agent |
| 1270 | 764 | $options['live_agent_status'] = 'off'; |
| 1271 | 765 | update_option('mxchat_options', $options); |
| 1272 | - | |
| 766 | + | |
| 1273 | 767 | // Set flag to show the notification banner |
| 1274 | 768 | update_option('mxchat_show_live_agent_disabled_notice', true); |
| 1275 | 769 | } |
| 1276 | - | |
| 770 | + | |
| 1277 | 771 | // Mark this update as handled |
| 1278 | 772 | update_option('mxchat_live_agent_update_2_2_2_handled', true); |
| 1279 | 773 | } |
| 1280 | 774 | } |
| 1281 | 775 | |
| 1282 | -/** | |
| 1283 | - * Handle theme migration notice for version 3.0.1 | |
| 1284 | - * Shows a dismissible notice to Pro users about migrating AI-generated themes | |
| 1285 | - */ | |
| 1286 | -function mxchat_handle_theme_migration_notice() { | |
| 1287 | - // Get the CURRENT stored version (before it gets updated) | |
| 1288 | - $current_version = get_option('mxchat_plugin_version', '0.0.0'); | |
| 1289 | - $target_version = '3.0.1'; | |
| 1290 | - | |
| 1291 | - // Only run this once for the update to 3.0.1 | |
| 1292 | - $update_handled = get_option('mxchat_theme_migration_update_3_0_1_handled', false); | |
| 1293 | - | |
| 1294 | - // Check if we're upgrading TO 3.0.1 and haven't handled this yet | |
| 1295 | - if (version_compare($current_version, $target_version, '<') && !$update_handled) { | |
| 1296 | - // Check if Pro is activated - only show to Pro users | |
| 1297 | - $license_status = get_option('mxchat_license_status', 'inactive'); | |
| 1298 | - $is_pro = ($license_status === 'active'); | |
| 1299 | - | |
| 1300 | - if ($is_pro) { | |
| 1301 | - // Set flag to show the theme migration notification banner | |
| 1302 | - update_option('mxchat_show_theme_migration_notice', true); | |
| 1303 | - } | |
| 1304 | - | |
| 1305 | - // Mark this update as handled (whether Pro or not) | |
| 1306 | - update_option('mxchat_theme_migration_update_3_0_1_handled', true); | |
| 1307 | - } | |
| 1308 | -} | |
| 1309 | - | |
| 1310 | 776 | // Initialize plugin safely |
| 1311 | 777 | function mxchat_init() { |
| 1312 | 778 | // Include all class files first |
| 1313 | 779 | mxchat_include_classes(); |
| @@ -1339,17 +805,10 @@ | ||
| 1339 | 805 | // Initialize meta box class |
| 1340 | 806 | if (class_exists('MxChat_Meta_Box')) { |
| 1341 | 807 | new MxChat_Meta_Box(); |
| 1342 | 808 | } |
| 1343 | - | |
| 1344 | 809 | } |
| 1345 | - | |
| 1346 | - // Initialize content generator globally — it registers wp_head hook | |
| 1347 | - // for frontend CSS injection, plus wp_ajax_ hooks for admin. | |
| 1348 | - if (class_exists('MxChat_Content_Generator')) { | |
| 1349 | - new MxChat_Content_Generator(); | |
| 1350 | - } | |
| 1351 | - | |
| 810 | + | |
| 1352 | 811 | // Initialize public classes |
| 1353 | 812 | if (class_exists('MxChat_Public')) { |
| 1354 | 813 | $mxchat_public = new MxChat_Public(); |
| 1355 | 814 | } |
| @@ -1375,60 +834,8 @@ | ||
| 1375 | 834 | } |
| 1376 | 835 | |
| 1377 | 836 | // Run initialization on plugins_loaded |
| 1378 | 837 | add_action('plugins_loaded', 'mxchat_init'); |
| 1379 | - | |
| 1380 | -// Run migration check on admin init (for auto-updates without reactivation) | |
| 1381 | -add_action('admin_init', 'mxchat_check_and_run_migrations'); | |
| 1382 | - | |
| 1383 | -/** | |
| 1384 | - * Check and run migrations on admin init | |
| 1385 | - * This ensures migrations run even when plugin is auto-updated | |
| 1386 | - */ | |
| 1387 | -function mxchat_check_and_run_migrations() { | |
| 1388 | - // Only run in admin and not on every request | |
| 1389 | - static $checked = false; | |
| 1390 | - if ($checked) { | |
| 1391 | - return; | |
| 1392 | - } | |
| 1393 | - $checked = true; | |
| 1394 | - | |
| 1395 | - mxchat_migrate_pinecone_roles_add_bot_id(); | |
| 1396 | - mxchat_migrate_add_content_type_column(); | |
| 1397 | - mxchat_migrate_add_translations_table(); | |
| 1398 | -} | |
| 1399 | - | |
| 1400 | -/** | |
| 1401 | - * Migration: Create transcript translations table (v3.0.4) | |
| 1402 | - * For users upgrading from versions before 3.0.4 | |
| 1403 | - */ | |
| 1404 | -function mxchat_migrate_add_translations_table() { | |
| 1405 | - $migration_key = 'mxchat_translations_table_created'; | |
| 1406 | - | |
| 1407 | - // Check if migration already ran | |
| 1408 | - if (get_option($migration_key)) { | |
| 1409 | - return; | |
| 1410 | - } | |
| 1411 | - | |
| 1412 | - // Create the translations table | |
| 1413 | - mxchat_create_translations_table(); | |
| 1414 | - | |
| 1415 | - // Mark migration as complete | |
| 1416 | - update_option($migration_key, '3.0.4'); | |
| 1417 | -} | |
| 1418 | - | |
| 1419 | -/** | |
| 1420 | - * Migration: Update deprecated Gemini embedding model (v3.0.5) | |
| 1421 | - * Updates gemini-embedding-exp-03-07 to gemini-embedding-001 for users who had it selected | |
| 1422 | - */ | |
| 1423 | -function mxchat_migrate_gemini_embedding_model() { | |
| 1424 | - $options = get_option('mxchat_options', array()); | |
| 1425 | - | |
| 1426 | - if (isset($options['embedding_model']) && $options['embedding_model'] === 'gemini-embedding-exp-03-07') { | |
| 1427 | - $options['embedding_model'] = 'gemini-embedding-001'; | |
| 1428 | - update_option('mxchat_options', $options); | |
| 1429 | - } | |
| 1430 | -} | |
| 1431 | 838 | |
| 1432 | 839 | // Register activation hook |
| 1433 | 840 | register_activation_hook(__FILE__, 'mxchat_activate'); |
| 1434 | 841 | |