PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.3.1
MxChat – AI Chatbot & Content Generation for WordPress v2.3.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 +177 -33 2.0.62.3.1 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 3 * Plugin Name: MxChat
4 4 * Description: AI chatbot for WordPress with OpenAI, Claude, xAI, DeepSeek, live agent, PDF uploads, WooCommerce, and training on website data.
5 - * Version: 2.0.6
5 + * Version: 2.3.1
6 6 * Author: MxChat
7 7 * Author URI: https://mxchat.ai
8 8 * License: GPLv2 or later
9 9 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
@@ -14,8 +14,10 @@
14 14 if (!defined('ABSPATH')) {
15 15 exit; // Exit if accessed directly.
16 16 }
17 17
18 +// Define plugin version constant for asset versioning
19 +define('MXCHAT_VERSION', '2.3.1');
18 20
19 21 function mxchat_load_textdomain() {
20 22 load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages');
21 23 }
@@ -20,17 +22,33 @@
20 22 load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages');
21 23 }
22 24 add_action('plugins_loaded', 'mxchat_load_textdomain');
23 25
24 -// Include classes
25 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php';
26 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php';
27 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php';
28 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php';
29 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php';
30 -require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php';
31 -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php';
26 +// Include classes with error handling
27 +function mxchat_include_classes() {
28 + $class_files = array(
29 + 'includes/class-mxchat-integrator.php',
30 + 'includes/class-mxchat-admin.php',
31 + 'includes/class-mxchat-public.php',
32 + 'includes/class-mxchat-utils.php',
33 + 'includes/class-mxchat-user.php',
34 + 'includes/pdf-parser/alt_autoload.php',
35 + 'includes/class-mxchat-word-handler.php',
36 + 'admin/class-ajax-handler.php',
37 + 'admin/class-pinecone-manager.php',
38 + 'admin/class-knowledge-manager.php'
39 + );
32 40
41 + foreach ($class_files as $file) {
42 + $file_path = plugin_dir_path(__FILE__) . $file;
43 + if (file_exists($file_path)) {
44 + require_once $file_path;
45 + } else {
46 + error_log('MxChat: Missing class file - ' . $file);
47 + }
48 + }
49 +}
50 +
33 51 function mxchat_activate() {
34 52 global $wpdb;
35 53 $charset_collate = $wpdb->get_charset_collate();
36 54
@@ -87,14 +105,14 @@
87 105 mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL');
88 106
89 107 // Set default thresholds for existing intents
90 108 $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL");
109 + mxchat_add_missing_columns($intents_table, 'enabled', 'TINYINT(1) NOT NULL DEFAULT 1');
91 110
92 - // Update plugin version in the database
93 - update_option('mxchat_plugin_version', '2.0.6');
111 + // Use the constant instead of hardcoded version
112 + update_option('mxchat_plugin_version', MXCHAT_VERSION);
94 113 }
95 114
96 -
97 115 function mxchat_add_missing_columns($table, $column_name, $column_type) {
98 116 global $wpdb;
99 117 $column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name));
100 118 if (empty($column_exists)) {
@@ -102,18 +120,76 @@
102 120 }
103 121 }
104 122
105 123 function mxchat_check_for_update() {
106 - $current_version = get_option('mxchat_plugin_version');
107 - $plugin_version = '2.0.6'; // Update with your latest version
124 + try {
125 + $current_version = get_option('mxchat_plugin_version');
126 + $plugin_version = MXCHAT_VERSION;
108 127
109 - if ($current_version !== $plugin_version) {
110 - mxchat_activate(); // Run the activation script to apply schema changes
111 - mxchat_migrate_live_agent_status(); // Add migration for live agent status
112 - update_option('mxchat_plugin_version', $plugin_version); // Update the version
128 + if ($current_version !== $plugin_version) {
129 + // Run live agent update BEFORE updating the stored version
130 + mxchat_handle_live_agent_update();
131 +
132 + // Run existing update processes
133 + mxchat_activate();
134 + mxchat_migrate_live_agent_status();
135 +
136 + // Add the new cleanup function for version 2.1.8
137 + if (version_compare($current_version, '2.1.8', '<')) {
138 + $deleted = mxchat_cleanup_orphaned_chat_history();
139 + }
140 +
141 + // Update version LAST so the live agent update logic can work
142 + update_option('mxchat_plugin_version', $plugin_version);
143 + }
144 + } catch (Exception $e) {
145 + error_log('MxChat update error: ' . $e->getMessage());
146 + // Don't update version if there was an error
113 147 }
114 148 }
115 149
150 +/**
151 + * Clean up orphaned chat history options from the wp_options table
152 + * @return int Number of options deleted
153 + */
154 +function mxchat_cleanup_orphaned_chat_history() {
155 + global $wpdb;
156 + $count = 0;
157 +
158 + // Get all option keys that match our pattern
159 + $history_options = $wpdb->get_results(
160 + "SELECT option_name FROM {$wpdb->options}
161 + WHERE option_name LIKE 'mxchat_history_%'"
162 + );
163 +
164 + if (!empty($history_options)) {
165 + foreach ($history_options as $option) {
166 + // Extract the session ID from the option name
167 + $session_id = str_replace('mxchat_history_', '', $option->option_name);
168 +
169 + // Check if this session still exists in the custom table
170 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
171 + $exists = $wpdb->get_var(
172 + $wpdb->prepare(
173 + "SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s",
174 + $session_id
175 + )
176 + );
177 +
178 + // If session doesn't exist in the main table, delete the option
179 + if ($exists == 0) {
180 + delete_option($option->option_name);
181 + // Also delete related metadata
182 + delete_option("mxchat_email_{$session_id}");
183 + delete_option("mxchat_agent_name_{$session_id}");
184 + $count++;
185 + }
186 + }
187 + }
188 +
189 + return $count;
190 +}
191 +
116 192 function mxchat_migrate_live_agent_status() {
117 193 $options = get_option('mxchat_options', []);
118 194
119 195 // Check if live_agent_status exists
@@ -144,16 +220,87 @@
144 220 update_option('mxchat_options', $options);
145 221 }
146 222 }
147 223
224 +function mxchat_handle_live_agent_update() {
225 + // Get the CURRENT stored version (before it gets updated)
226 + $current_version = get_option('mxchat_plugin_version', '0.0.0');
227 + $new_version = '2.2.2';
228 +
229 + // Only run this once for the update to 2.2.2
230 + $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false);
231 +
232 + // Check if we're upgrading TO 2.2.2 and haven't handled this yet
233 + if (version_compare($current_version, $new_version, '<') && !$update_handled) {
234 + $options = get_option('mxchat_options', array());
235 +
236 + // Check if live agent was previously enabled
237 + if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') {
238 + // Disable live agent
239 + $options['live_agent_status'] = 'off';
240 + update_option('mxchat_options', $options);
241 +
242 + // Set flag to show the notification banner
243 + update_option('mxchat_show_live_agent_disabled_notice', true);
244 + }
245 +
246 + // Mark this update as handled
247 + update_option('mxchat_live_agent_update_2_2_2_handled', true);
248 + }
249 +}
148 250
149 -// Run the update check function on every request
150 -add_action('plugins_loaded', 'mxchat_check_for_update');
251 +// Initialize plugin safely
252 +function mxchat_init() {
253 + // Include all class files first
254 + mxchat_include_classes();
255 +
256 + // Run update check
257 + mxchat_check_for_update();
258 +
259 + // Initialize classes with error handling
260 + try {
261 + // Initialize admin classes
262 + if (is_admin()) {
263 + if (class_exists('MxChat_Knowledge_Manager')) {
264 + $mxchat_knowledge_manager = new MxChat_Knowledge_Manager();
265 +
266 + if (class_exists('MxChat_Admin')) {
267 + $mxchat_admin = new MxChat_Admin($mxchat_knowledge_manager);
268 + }
269 + }
270 + }
271 +
272 + // Initialize public classes
273 + if (class_exists('MxChat_Public')) {
274 + $mxchat_public = new MxChat_Public();
275 + }
276 +
277 + if (class_exists('MxChat_Integrator')) {
278 + $mxchat_integrator = new MxChat_Integrator();
279 + }
280 +
281 + } catch (Exception $e) {
282 + error_log('MxChat initialization error: ' . $e->getMessage());
283 +
284 + // Show admin notice if there's an error
285 + if (is_admin()) {
286 + add_action('admin_notices', function() use ($e) {
287 + echo '<div class="notice notice-error"><p>';
288 + echo '<strong>MxChat Error:</strong> Plugin initialization failed. ';
289 + echo 'Please check error logs or contact support. Error: ' . esc_html($e->getMessage());
290 + echo '</p></div>';
291 + });
292 + }
293 + }
294 +}
151 295
296 +// Run initialization on plugins_loaded
297 +add_action('plugins_loaded', 'mxchat_init');
298 +
152 299 // Register activation hook
153 300 register_activation_hook(__FILE__, 'mxchat_activate');
154 301
155 -
302 +// Add cron schedule
156 303 add_filter('cron_schedules', function($schedules) {
157 304 $schedules['one_minute'] = array(
158 305 'interval' => 60,
159 306 'display' => 'Every Minute'
@@ -160,16 +307,13 @@
160 307 );
161 308 return $schedules;
162 309 });
163 310
164 -add_action('init', function() {
165 - add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5);
166 - add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5);
167 -});
168 -
169 -
170 -// Instantiate classes
171 -if (is_admin()) {
172 - $mxchat_admin = new MxChat_Admin();
173 -}
174 -$mxchat_public = new MxChat_Public();
175 -$mxchat_integrator = new MxChat_Integrator();
311 +// Register deactivation hook - wrap in a callback to ensure the integrator exists
312 +register_deactivation_hook(__FILE__, function() {
313 + if (class_exists('MxChat_Integrator')) {
314 + $integrator = new MxChat_Integrator();
315 + if (method_exists($integrator, 'cleanup_cron_events')) {
316 + $integrator->cleanup_cron_events();
317 + }
318 + }
319 +});