| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: MxChat |
| 4 |
* Description: AI chatbot for WordPress with OpenAI, Claude, xAI, live agent transfer, PDF uploads, WooCommerce, and training on website data. |
| 5 |
* Version: 1.5.5 |
| 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 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
// Include classes |
| 17 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php'; |
| 18 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php'; |
| 19 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php'; |
| 20 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php'; |
| 21 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php'; |
| 22 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-woocommerce.php'; |
| 23 |
require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; |
| 24 |
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php'; |
| 25 |
|
| 26 |
function mxchat_activate() { |
| 27 |
global $wpdb; |
| 28 |
$charset_collate = $wpdb->get_charset_collate(); |
| 29 |
|
| 30 |
// Chat Transcripts Table |
| 31 |
$chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 32 |
$sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table ( |
| 33 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 34 |
user_id MEDIUMINT(9) DEFAULT 0, |
| 35 |
session_id VARCHAR(255) NOT NULL, |
| 36 |
role VARCHAR(255) NOT NULL, |
| 37 |
message TEXT NOT NULL, |
| 38 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 39 |
PRIMARY KEY (id) |
| 40 |
) $charset_collate;"; |
| 41 |
|
| 42 |
// System Prompt Content Table |
| 43 |
$system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 44 |
$sql_system_prompt = "CREATE TABLE $system_prompt_table ( |
| 45 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 46 |
url VARCHAR(255) NOT NULL, |
| 47 |
article_content LONGTEXT NOT NULL, |
| 48 |
embedding_vector LONGTEXT, |
| 49 |
source_url VARCHAR(255) DEFAULT NULL, |
| 50 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 51 |
PRIMARY KEY (id) |
| 52 |
) $charset_collate;"; |
| 53 |
|
| 54 |
// Intents Table |
| 55 |
$intents_table = $wpdb->prefix . 'mxchat_intents'; |
| 56 |
$sql_intents_table = "CREATE TABLE $intents_table ( |
| 57 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 58 |
intent_label VARCHAR(255) NOT NULL, |
| 59 |
phrases TEXT NOT NULL, |
| 60 |
embedding_vector LONGTEXT NOT NULL, |
| 61 |
callback_function VARCHAR(255) NOT NULL, |
| 62 |
similarity_threshold FLOAT DEFAULT 0.85, |
| 63 |
PRIMARY KEY (id) |
| 64 |
) $charset_collate;"; |
| 65 |
|
| 66 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 67 |
|
| 68 |
// Create or update tables |
| 69 |
dbDelta($sql_chat_transcripts); |
| 70 |
dbDelta($sql_system_prompt); |
| 71 |
dbDelta($sql_intents_table); |
| 72 |
|
| 73 |
// Ensure additional columns in `mxchat_system_prompt_content` and `mxchat_chat_transcripts` |
| 74 |
mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT'); |
| 75 |
mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 76 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)'); |
| 77 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255)'); |
| 78 |
|
| 79 |
// Set default thresholds for existing intents |
| 80 |
$wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); |
| 81 |
|
| 82 |
// Update plugin version in the database |
| 83 |
update_option('mxchat_plugin_version', '1.5.5'); |
| 84 |
} |
| 85 |
|
| 86 |
function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| 87 |
global $wpdb; |
| 88 |
$column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name)); |
| 89 |
if (empty($column_exists)) { |
| 90 |
$wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type"); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
function mxchat_check_for_update() { |
| 95 |
$current_version = get_option('mxchat_plugin_version'); |
| 96 |
$plugin_version = '1.5.5'; |
| 97 |
|
| 98 |
if ($current_version !== $plugin_version) { |
| 99 |
mxchat_activate(); // Run the activation script which includes database migrations |
| 100 |
update_option('mxchat_plugin_version', $plugin_version); // Update to the latest version |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Run the update check function on every request |
| 105 |
add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 106 |
|
| 107 |
// Register activation hook |
| 108 |
register_activation_hook(__FILE__, 'mxchat_activate'); |
| 109 |
|
| 110 |
add_action('init', function() { |
| 111 |
add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5); |
| 112 |
add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5); |
| 113 |
}); |
| 114 |
|
| 115 |
// Instantiate classes |
| 116 |
if (is_admin()) { |
| 117 |
$mxchat_admin = new MxChat_Admin(); |
| 118 |
} |
| 119 |
$mxchat_public = new MxChat_Public(); |
| 120 |
$mxchat_integrator = new MxChat_Integrator(); |
| 121 |
|