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
mxchat-basic / mxchat-basic.php

mxchat-basic.php in MxChat – AI Chatbot & Content Generation for WordPress 2.3.1, at mxchat-basic.php

319 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: MxChat
4 * Description: AI chatbot for WordPress with OpenAI, Claude, xAI, DeepSeek, live agent, PDF uploads, WooCommerce, and training on website data.
5 * Version: 2.3.1
6 * Author: MxChat
7 * Author URI: https://mxchat.ai
8 * License: GPLv2 or later
9 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
10 * Text Domain: mxchat
11 * Domain Path: /languages
12 */
13
14 if (!defined('ABSPATH')) {
15 exit; // Exit if accessed directly.
16 }
17
18 // Define plugin version constant for asset versioning
19 define('MXCHAT_VERSION', '2.3.1');
20
21 function mxchat_load_textdomain() {
22 load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages');
23 }
24 add_action('plugins_loaded', 'mxchat_load_textdomain');
25
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 );
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
51 function mxchat_activate() {
52 global $wpdb;
53 $charset_collate = $wpdb->get_charset_collate();
54
55 // Chat Transcripts Table
56 $chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts';
57 $sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table (
58 id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
59 user_id MEDIUMINT(9) DEFAULT 0,
60 session_id VARCHAR(255) NOT NULL,
61 role VARCHAR(255) NOT NULL,
62 message TEXT NOT NULL,
63 user_email VARCHAR(255) DEFAULT NULL,
64 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
65 PRIMARY KEY (id)
66 ) $charset_collate;";
67
68 // System Prompt Content Table
69 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
70 $sql_system_prompt = "CREATE TABLE $system_prompt_table (
71 id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
72 url VARCHAR(255) NOT NULL,
73 article_content LONGTEXT NOT NULL,
74 embedding_vector LONGTEXT,
75 source_url VARCHAR(255) DEFAULT NULL,
76 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
77 PRIMARY KEY (id)
78 ) $charset_collate;";
79
80 // Intents Table
81 $intents_table = $wpdb->prefix . 'mxchat_intents';
82 $sql_intents_table = "CREATE TABLE $intents_table (
83 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
84 intent_label VARCHAR(255) NOT NULL,
85 phrases TEXT NOT NULL,
86 embedding_vector LONGTEXT NOT NULL,
87 callback_function VARCHAR(255) NOT NULL,
88 similarity_threshold FLOAT DEFAULT 0.85,
89 PRIMARY KEY (id)
90 ) $charset_collate;";
91
92 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
93
94 // Create or update tables
95 dbDelta($sql_chat_transcripts);
96 dbDelta($sql_system_prompt);
97 dbDelta($sql_intents_table);
98
99 // Ensure additional columns in `mxchat_chat_transcripts`
100 mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)');
101 mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255) DEFAULT NULL');
102
103 // Ensure additional columns in `mxchat_system_prompt_content`
104 mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT');
105 mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL');
106
107 // Set default thresholds for existing intents
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');
110
111 // Use the constant instead of hardcoded version
112 update_option('mxchat_plugin_version', MXCHAT_VERSION);
113 }
114
115 function mxchat_add_missing_columns($table, $column_name, $column_type) {
116 global $wpdb;
117 $column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name));
118 if (empty($column_exists)) {
119 $wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type");
120 }
121 }
122
123 function mxchat_check_for_update() {
124 try {
125 $current_version = get_option('mxchat_plugin_version');
126 $plugin_version = MXCHAT_VERSION;
127
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
147 }
148 }
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
192 function mxchat_migrate_live_agent_status() {
193 $options = get_option('mxchat_options', []);
194
195 // Check if live_agent_status exists
196 if (isset($options['live_agent_status'])) {
197 $current_status = $options['live_agent_status'];
198 $needs_update = false;
199
200 // Convert to new format if needed
201 if ($current_status === 'online') {
202 $options['live_agent_status'] = 'on';
203 $needs_update = true;
204 } else if ($current_status === 'offline') {
205 $options['live_agent_status'] = 'off';
206 $needs_update = true;
207 } else if (!in_array($current_status, ['on', 'off'])) {
208 // Default to off for any unexpected values
209 $options['live_agent_status'] = 'off';
210 $needs_update = true;
211 }
212
213 // Only update if needed
214 if ($needs_update) {
215 update_option('mxchat_options', $options);
216 }
217 } else {
218 // If status doesn't exist, set default to off
219 $options['live_agent_status'] = 'off';
220 update_option('mxchat_options', $options);
221 }
222 }
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 }
250
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 }
295
296 // Run initialization on plugins_loaded
297 add_action('plugins_loaded', 'mxchat_init');
298
299 // Register activation hook
300 register_activation_hook(__FILE__, 'mxchat_activate');
301
302 // Add cron schedule
303 add_filter('cron_schedules', function($schedules) {
304 $schedules['one_minute'] = array(
305 'interval' => 60,
306 'display' => 'Every Minute'
307 );
308 return $schedules;
309 });
310
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 });