| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: MxChat |
| 4 |
* Description: AI Chatbot for WordPress with support for OpenAI, X.AI, and Claude models. Includes features like custom knowledge submission, chat transcripts, and seamless integration with WooCommerce. Perfect for blogs, business sites, e-commerce platforms, and more, offering real-time intelligent interactions with advanced AI models. |
| 5 |
* Version: 1.3 |
| 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 |
|
| 24 |
function mxchat_activate() { |
| 25 |
global $wpdb; |
| 26 |
$charset_collate = $wpdb->get_charset_collate(); |
| 27 |
|
| 28 |
// Chat Transcripts Table |
| 29 |
$chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 30 |
$sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table ( |
| 31 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 32 |
user_id mediumint(9) DEFAULT 0, |
| 33 |
session_id varchar(255) NOT NULL, |
| 34 |
role varchar(255) NOT NULL, |
| 35 |
message text NOT NULL, |
| 36 |
timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 37 |
PRIMARY KEY (id) |
| 38 |
);"; |
| 39 |
|
| 40 |
// System Prompt Content Table |
| 41 |
$system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 42 |
$sql_system_prompt = "CREATE TABLE $system_prompt_table ( |
| 43 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 44 |
url varchar(255) NOT NULL, |
| 45 |
article_content longtext NOT NULL, |
| 46 |
embedding_vector longtext, |
| 47 |
timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 48 |
PRIMARY KEY (id) |
| 49 |
);"; |
| 50 |
|
| 51 |
// Intents Table |
| 52 |
$intents_table = $wpdb->prefix . 'mxchat_intents'; |
| 53 |
$sql_intents_table = "CREATE TABLE $intents_table ( |
| 54 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 55 |
intent_label VARCHAR(255) NOT NULL, |
| 56 |
phrases TEXT NOT NULL, |
| 57 |
embedding_vector LONGTEXT NOT NULL, |
| 58 |
callback_function VARCHAR(255) NOT NULL, |
| 59 |
PRIMARY KEY (id) |
| 60 |
) $charset_collate;"; |
| 61 |
|
| 62 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 63 |
|
| 64 |
// Create or update the chat transcripts table |
| 65 |
dbDelta($sql_chat_transcripts); |
| 66 |
|
| 67 |
// Create or update the system prompt content table |
| 68 |
dbDelta($sql_system_prompt); |
| 69 |
|
| 70 |
// Create or update the intents table |
| 71 |
dbDelta($sql_intents_table); |
| 72 |
|
| 73 |
// Ensure 'embedding_vector' column exists in the 'system_prompt_content' table |
| 74 |
$column_exists = $wpdb->get_results($wpdb->prepare( |
| 75 |
"SHOW COLUMNS FROM {$system_prompt_table} LIKE %s", |
| 76 |
'embedding_vector' |
| 77 |
)); |
| 78 |
if (empty($column_exists)) { |
| 79 |
$wpdb->query( |
| 80 |
"ALTER TABLE {$system_prompt_table} ADD COLUMN embedding_vector longtext" |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
// Ensure 'source_url' column exists in the 'system_prompt_content' table |
| 85 |
$source_url_exists = $wpdb->get_results($wpdb->prepare( |
| 86 |
"SHOW COLUMNS FROM {$system_prompt_table} LIKE %s", |
| 87 |
'source_url' |
| 88 |
)); |
| 89 |
if (empty($source_url_exists)) { |
| 90 |
$wpdb->query( |
| 91 |
"ALTER TABLE {$system_prompt_table} ADD COLUMN source_url VARCHAR(255) DEFAULT NULL" |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
// Ensure 'user_identifier' and 'user_email' columns exist in the 'chat_transcripts' table |
| 96 |
$user_identifier_exists = $wpdb->get_results($wpdb->prepare( |
| 97 |
"SHOW COLUMNS FROM {$chat_transcripts_table} LIKE %s", |
| 98 |
'user_identifier' |
| 99 |
)); |
| 100 |
if (empty($user_identifier_exists)) { |
| 101 |
$wpdb->query( |
| 102 |
"ALTER TABLE {$chat_transcripts_table} ADD COLUMN user_identifier VARCHAR(255)" |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
$user_email_exists = $wpdb->get_results($wpdb->prepare( |
| 107 |
"SHOW COLUMNS FROM {$chat_transcripts_table} LIKE %s", |
| 108 |
'user_email' |
| 109 |
)); |
| 110 |
if (empty($user_email_exists)) { |
| 111 |
$wpdb->query( |
| 112 |
"ALTER TABLE {$chat_transcripts_table} ADD COLUMN user_email VARCHAR(255)" |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
// Update the version number in the database |
| 117 |
update_option('mxchat_plugin_version', '1.3'); |
| 118 |
} |
| 119 |
|
| 120 |
function mxchat_check_for_update() { |
| 121 |
$current_version = get_option('mxchat_plugin_version'); |
| 122 |
$plugin_version = '1.3'; |
| 123 |
|
| 124 |
if ($current_version !== $plugin_version) { |
| 125 |
mxchat_activate(); // Run the activation script which includes database migrations |
| 126 |
update_option('mxchat_plugin_version', $plugin_version); // Update to the latest version |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Run the update check function on every request |
| 131 |
add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 132 |
|
| 133 |
// Register activation hook |
| 134 |
register_activation_hook(__FILE__, 'mxchat_activate'); |
| 135 |
|
| 136 |
// Instantiate classes |
| 137 |
if (is_admin()) { |
| 138 |
$mxchat_admin = new MxChat_Admin(); |
| 139 |
} |
| 140 |
$mxchat_public = new MxChat_Public(); |
| 141 |
$mxchat_integrator = new MxChat_Integrator(); |
| 142 |
|