| 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.0 |
| 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 |
|
| 19 |
function mxchat_load_textdomain() { |
| 20 |
load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 21 |
} |
| 22 |
add_action('plugins_loaded', 'mxchat_load_textdomain'); |
| 23 |
|
| 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'; |
| 32 |
|
| 33 |
function mxchat_activate() { |
| 34 |
global $wpdb; |
| 35 |
$charset_collate = $wpdb->get_charset_collate(); |
| 36 |
|
| 37 |
// Chat Transcripts Table |
| 38 |
$chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 39 |
$sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table ( |
| 40 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 41 |
user_id MEDIUMINT(9) DEFAULT 0, |
| 42 |
session_id VARCHAR(255) NOT NULL, |
| 43 |
role VARCHAR(255) NOT NULL, |
| 44 |
message TEXT NOT NULL, |
| 45 |
user_email VARCHAR(255) DEFAULT NULL, |
| 46 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 47 |
PRIMARY KEY (id) |
| 48 |
) $charset_collate;"; |
| 49 |
|
| 50 |
// System Prompt Content Table |
| 51 |
$system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 52 |
$sql_system_prompt = "CREATE TABLE $system_prompt_table ( |
| 53 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 54 |
url VARCHAR(255) NOT NULL, |
| 55 |
article_content LONGTEXT NOT NULL, |
| 56 |
embedding_vector LONGTEXT, |
| 57 |
source_url VARCHAR(255) DEFAULT NULL, |
| 58 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 59 |
PRIMARY KEY (id) |
| 60 |
) $charset_collate;"; |
| 61 |
|
| 62 |
// Intents Table |
| 63 |
$intents_table = $wpdb->prefix . 'mxchat_intents'; |
| 64 |
$sql_intents_table = "CREATE TABLE $intents_table ( |
| 65 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 66 |
intent_label VARCHAR(255) NOT NULL, |
| 67 |
phrases TEXT NOT NULL, |
| 68 |
embedding_vector LONGTEXT NOT NULL, |
| 69 |
callback_function VARCHAR(255) NOT NULL, |
| 70 |
similarity_threshold FLOAT DEFAULT 0.85, |
| 71 |
PRIMARY KEY (id) |
| 72 |
) $charset_collate;"; |
| 73 |
|
| 74 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 75 |
|
| 76 |
// Create or update tables |
| 77 |
dbDelta($sql_chat_transcripts); |
| 78 |
dbDelta($sql_system_prompt); |
| 79 |
dbDelta($sql_intents_table); |
| 80 |
|
| 81 |
// Ensure additional columns in `mxchat_chat_transcripts` |
| 82 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)'); |
| 83 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255) DEFAULT NULL'); |
| 84 |
|
| 85 |
// Ensure additional columns in `mxchat_system_prompt_content` |
| 86 |
mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT'); |
| 87 |
mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 88 |
|
| 89 |
// Set default thresholds for existing intents |
| 90 |
$wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); |
| 91 |
mxchat_add_missing_columns($intents_table, 'enabled', 'TINYINT(1) NOT NULL DEFAULT 1'); |
| 92 |
|
| 93 |
// Update plugin version in the database |
| 94 |
update_option('mxchat_plugin_version', '2.2.0'); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| 99 |
global $wpdb; |
| 100 |
$column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name)); |
| 101 |
if (empty($column_exists)) { |
| 102 |
$wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type"); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
function mxchat_check_for_update() { |
| 107 |
$current_version = get_option('mxchat_plugin_version'); |
| 108 |
$plugin_version = '2.2.0'; // Increment to your next version |
| 109 |
|
| 110 |
if ($current_version !== $plugin_version) { |
| 111 |
// Run your existing update processes |
| 112 |
mxchat_activate(); // Run the activation script to apply schema changes |
| 113 |
mxchat_migrate_live_agent_status(); // Add migration for live agent status |
| 114 |
|
| 115 |
// Add the new cleanup function for version 2.1.8 |
| 116 |
if (version_compare($current_version, '2.1.8', '<')) { |
| 117 |
// Call the cleanup function |
| 118 |
$deleted = mxchat_cleanup_orphaned_chat_history(); |
| 119 |
// Optionally log the results |
| 120 |
error_log(sprintf('MxChat cleanup: Removed %d orphaned chat history entries', $deleted)); |
| 121 |
} |
| 122 |
|
| 123 |
// Update the stored version number |
| 124 |
update_option('mxchat_plugin_version', $plugin_version); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Clean up orphaned chat history options from the wp_options table |
| 130 |
* @return int Number of options deleted |
| 131 |
*/ |
| 132 |
function mxchat_cleanup_orphaned_chat_history() { |
| 133 |
global $wpdb; |
| 134 |
$count = 0; |
| 135 |
|
| 136 |
// Get all option keys that match our pattern |
| 137 |
$history_options = $wpdb->get_results( |
| 138 |
"SELECT option_name FROM {$wpdb->options} |
| 139 |
WHERE option_name LIKE 'mxchat_history_%'" |
| 140 |
); |
| 141 |
|
| 142 |
if (!empty($history_options)) { |
| 143 |
foreach ($history_options as $option) { |
| 144 |
// Extract the session ID from the option name |
| 145 |
$session_id = str_replace('mxchat_history_', '', $option->option_name); |
| 146 |
|
| 147 |
// Check if this session still exists in the custom table |
| 148 |
$table_name = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 149 |
$exists = $wpdb->get_var( |
| 150 |
$wpdb->prepare( |
| 151 |
"SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", |
| 152 |
$session_id |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
// If session doesn't exist in the main table, delete the option |
| 157 |
if ($exists == 0) { |
| 158 |
delete_option($option->option_name); |
| 159 |
// Also delete related metadata |
| 160 |
delete_option("mxchat_email_{$session_id}"); |
| 161 |
delete_option("mxchat_agent_name_{$session_id}"); |
| 162 |
$count++; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $count; |
| 168 |
} |
| 169 |
|
| 170 |
function mxchat_migrate_live_agent_status() { |
| 171 |
$options = get_option('mxchat_options', []); |
| 172 |
|
| 173 |
// Check if live_agent_status exists |
| 174 |
if (isset($options['live_agent_status'])) { |
| 175 |
$current_status = $options['live_agent_status']; |
| 176 |
$needs_update = false; |
| 177 |
|
| 178 |
// Convert to new format if needed |
| 179 |
if ($current_status === 'online') { |
| 180 |
$options['live_agent_status'] = 'on'; |
| 181 |
$needs_update = true; |
| 182 |
} else if ($current_status === 'offline') { |
| 183 |
$options['live_agent_status'] = 'off'; |
| 184 |
$needs_update = true; |
| 185 |
} else if (!in_array($current_status, ['on', 'off'])) { |
| 186 |
// Default to off for any unexpected values |
| 187 |
$options['live_agent_status'] = 'off'; |
| 188 |
$needs_update = true; |
| 189 |
} |
| 190 |
|
| 191 |
// Only update if needed |
| 192 |
if ($needs_update) { |
| 193 |
update_option('mxchat_options', $options); |
| 194 |
} |
| 195 |
} else { |
| 196 |
// If status doesn't exist, set default to off |
| 197 |
$options['live_agent_status'] = 'off'; |
| 198 |
update_option('mxchat_options', $options); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
// Run the update check function on every request |
| 204 |
add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 205 |
|
| 206 |
// Register activation hook |
| 207 |
register_activation_hook(__FILE__, 'mxchat_activate'); |
| 208 |
|
| 209 |
|
| 210 |
add_filter('cron_schedules', function($schedules) { |
| 211 |
$schedules['one_minute'] = array( |
| 212 |
'interval' => 60, |
| 213 |
'display' => 'Every Minute' |
| 214 |
); |
| 215 |
return $schedules; |
| 216 |
}); |
| 217 |
|
| 218 |
add_action('init', function() { |
| 219 |
add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5); |
| 220 |
add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5); |
| 221 |
}); |
| 222 |
|
| 223 |
|
| 224 |
// Instantiate classes |
| 225 |
if (is_admin()) { |
| 226 |
$mxchat_admin = new MxChat_Admin(); |
| 227 |
} |
| 228 |
$mxchat_public = new MxChat_Public(); |
| 229 |
$mxchat_integrator = new MxChat_Integrator(); |
| 230 |
|