PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.2.2
MxChat – AI Chatbot & Content Generation for WordPress v2.2.2
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.2.2, at mxchat-basic.php

306 lines 12.2 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.2.2
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 // ADD THIS: Define plugin version constant for asset versioning
19 define('MXCHAT_VERSION', '2.2.2');
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
27 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php';
28 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php';
29 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php';
30 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php';
31 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php';
32 require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php';
33 require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php';
34 require_once plugin_dir_path(__FILE__) . 'admin/class-ajax-handler.php';
35 require_once plugin_dir_path(__FILE__) . 'admin/class-pinecone-manager.php';
36 require_once plugin_dir_path(__FILE__) . 'admin/class-knowledge-manager.php';
37
38
39 function mxchat_activate() {
40 global $wpdb;
41 $charset_collate = $wpdb->get_charset_collate();
42
43 // Chat Transcripts Table
44 $chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts';
45 $sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table (
46 id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
47 user_id MEDIUMINT(9) DEFAULT 0,
48 session_id VARCHAR(255) NOT NULL,
49 role VARCHAR(255) NOT NULL,
50 message TEXT NOT NULL,
51 user_email VARCHAR(255) DEFAULT NULL,
52 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
53 PRIMARY KEY (id)
54 ) $charset_collate;";
55
56 // System Prompt Content Table
57 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
58 $sql_system_prompt = "CREATE TABLE $system_prompt_table (
59 id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
60 url VARCHAR(255) NOT NULL,
61 article_content LONGTEXT NOT NULL,
62 embedding_vector LONGTEXT,
63 source_url VARCHAR(255) DEFAULT NULL,
64 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
65 PRIMARY KEY (id)
66 ) $charset_collate;";
67
68 // Intents Table
69 $intents_table = $wpdb->prefix . 'mxchat_intents';
70 $sql_intents_table = "CREATE TABLE $intents_table (
71 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
72 intent_label VARCHAR(255) NOT NULL,
73 phrases TEXT NOT NULL,
74 embedding_vector LONGTEXT NOT NULL,
75 callback_function VARCHAR(255) NOT NULL,
76 similarity_threshold FLOAT DEFAULT 0.85,
77 PRIMARY KEY (id)
78 ) $charset_collate;";
79
80 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
81
82 // Create or update tables
83 dbDelta($sql_chat_transcripts);
84 dbDelta($sql_system_prompt);
85 dbDelta($sql_intents_table);
86
87 // Ensure additional columns in `mxchat_chat_transcripts`
88 mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)');
89 mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255) DEFAULT NULL');
90
91 // Ensure additional columns in `mxchat_system_prompt_content`
92 mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT');
93 mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL');
94
95 // Set default thresholds for existing intents
96 $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL");
97 mxchat_add_missing_columns($intents_table, 'enabled', 'TINYINT(1) NOT NULL DEFAULT 1');
98
99 // CHANGE THIS: Use the constant instead of hardcoded version
100 update_option('mxchat_plugin_version', MXCHAT_VERSION);
101 }
102
103
104 function mxchat_add_missing_columns($table, $column_name, $column_type) {
105 global $wpdb;
106 $column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name));
107 if (empty($column_exists)) {
108 $wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type");
109 }
110 }
111
112 function mxchat_check_for_update() {
113 //error_log('=== mxchat_check_for_update called ===');
114
115 $current_version = get_option('mxchat_plugin_version');
116 $plugin_version = MXCHAT_VERSION;
117
118 //error_log('Current stored version: ' . $current_version);
119 //error_log('Plugin version constant: ' . $plugin_version);
120
121 if ($current_version !== $plugin_version) {
122 //error_log('Version mismatch detected, running updates');
123
124 // IMPORTANT: Run live agent update BEFORE updating the stored version
125 mxchat_handle_live_agent_update();
126
127 // Run your existing update processes
128 mxchat_activate();
129 mxchat_migrate_live_agent_status();
130
131 // Add the new cleanup function for version 2.1.8
132 if (version_compare($current_version, '2.1.8', '<')) {
133 $deleted = mxchat_cleanup_orphaned_chat_history();
134 //error_log(sprintf('MxChat cleanup: Removed %d orphaned chat history entries', $deleted));
135 }
136
137 // Update version LAST so the live agent update logic can work
138 update_option('mxchat_plugin_version', $plugin_version);
139 //error_log('Version updated to: ' . $plugin_version);
140 } else {
141 //error_log('Versions match, no update needed');
142 }
143 }
144
145 /**
146 * Clean up orphaned chat history options from the wp_options table
147 * @return int Number of options deleted
148 */
149 function mxchat_cleanup_orphaned_chat_history() {
150 global $wpdb;
151 $count = 0;
152
153 // Get all option keys that match our pattern
154 $history_options = $wpdb->get_results(
155 "SELECT option_name FROM {$wpdb->options}
156 WHERE option_name LIKE 'mxchat_history_%'"
157 );
158
159 if (!empty($history_options)) {
160 foreach ($history_options as $option) {
161 // Extract the session ID from the option name
162 $session_id = str_replace('mxchat_history_', '', $option->option_name);
163
164 // Check if this session still exists in the custom table
165 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
166 $exists = $wpdb->get_var(
167 $wpdb->prepare(
168 "SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s",
169 $session_id
170 )
171 );
172
173 // If session doesn't exist in the main table, delete the option
174 if ($exists == 0) {
175 delete_option($option->option_name);
176 // Also delete related metadata
177 delete_option("mxchat_email_{$session_id}");
178 delete_option("mxchat_agent_name_{$session_id}");
179 $count++;
180 }
181 }
182 }
183
184 return $count;
185 }
186
187 function mxchat_migrate_live_agent_status() {
188 //error_log('=== mxchat_migrate_live_agent_status called ===');
189 $options = get_option('mxchat_options', []);
190
191 // Check if live_agent_status exists
192 if (isset($options['live_agent_status'])) {
193 $current_status = $options['live_agent_status'];
194 //error_log('Current live agent status: ' . $current_status);
195 $needs_update = false;
196
197 // Convert to new format if needed
198 if ($current_status === 'online') {
199 $options['live_agent_status'] = 'on';
200 $needs_update = true;
201 //error_log('Converting online -> on');
202 } else if ($current_status === 'offline') {
203 $options['live_agent_status'] = 'off';
204 $needs_update = true;
205 //error_log('Converting offline -> off');
206 } else if (!in_array($current_status, ['on', 'off'])) {
207 // Default to off for any unexpected values
208 $options['live_agent_status'] = 'off';
209 $needs_update = true;
210 //error_log('Unexpected value, setting to off');
211 }
212
213 // Only update if needed
214 if ($needs_update) {
215 update_option('mxchat_options', $options);
216 //error_log('Live agent status updated');
217 }
218 } else {
219 // If status doesn't exist, set default to off
220 $options['live_agent_status'] = 'off';
221 update_option('mxchat_options', $options);
222 //error_log('Live agent status not set, defaulting to off');
223 }
224 }
225
226 // FIXED: This function had the wrong logic
227 function mxchat_handle_live_agent_update() {
228 //error_log('=== mxchat_handle_live_agent_update called ===');
229
230 // Get the CURRENT stored version (before it gets updated)
231 $current_version = get_option('mxchat_plugin_version', '0.0.0');
232 $new_version = '2.2.2';
233
234 //error_log('Live Agent Update - Current stored version: ' . $current_version);
235 //error_log('Live Agent Update - Target version: ' . $new_version);
236
237 // Only run this once for the update to 2.2.2
238 $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false);
239 //error_log('Live Agent Update - Already handled: ' . ($update_handled ? 'yes' : 'no'));
240
241 // FIXED: Check if we're upgrading TO 2.2.2 and haven't handled this yet
242 if (version_compare($current_version, $new_version, '<') && !$update_handled) {
243 //error_log('Live Agent Update - Conditions met, checking live agent status');
244
245 $options = get_option('mxchat_options', array());
246 $live_agent_status = isset($options['live_agent_status']) ? $options['live_agent_status'] : 'not set';
247 //error_log('Live Agent Update - Current status: ' . $live_agent_status);
248
249 // Check if live agent was previously enabled
250 if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') {
251 //error_log('Live Agent Update - Live agent was ON, disabling it');
252
253 // Disable live agent
254 $options['live_agent_status'] = 'off';
255 update_option('mxchat_options', $options);
256
257 // Set flag to show the notification banner
258 update_option('mxchat_show_live_agent_disabled_notice', true);
259 //error_log('Live Agent Update - Live agent disabled and banner flag set');
260 } else {
261 //error_log('Live Agent Update - Live agent was not ON (status: ' . $live_agent_status . '), no action taken');
262 }
263
264 // Mark this update as handled
265 update_option('mxchat_live_agent_update_2_2_2_handled', true);
266 //error_log('Live Agent Update - Marked as handled');
267 } else {
268 //error_log('Live Agent Update - Conditions not met');
269 if (version_compare($current_version, $new_version, '>=')) {
270 //error_log('Live Agent Update - Already on version ' . $new_version . ' or higher');
271 }
272 if ($update_handled) {
273 //error_log('Live Agent Update - Already handled this update');
274 }
275 }
276 }
277
278 // Run the update check function on every request
279 add_action('plugins_loaded', 'mxchat_check_for_update');
280
281 // Register activation hook
282 register_activation_hook(__FILE__, 'mxchat_activate');
283
284
285 add_filter('cron_schedules', function($schedules) {
286 $schedules['one_minute'] = array(
287 'interval' => 60,
288 'display' => 'Every Minute'
289 );
290 return $schedules;
291 });
292
293
294 // Instantiate classes
295 if (is_admin()) {
296 // Create the knowledge manager instance first
297 $mxchat_knowledge_manager = new MxChat_Knowledge_Manager();
298
299 // Pass it to your admin class
300 $mxchat_admin = new MxChat_Admin($mxchat_knowledge_manager);
301 }
302 $mxchat_public = new MxChat_Public();
303 $mxchat_integrator = new MxChat_Integrator();
304
305 // ADD THIS LINE HERE:
306 register_deactivation_hook(__FILE__, array($mxchat_integrator, 'cleanup_cron_events'));