| 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.4.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 |
require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; |
| 24 |
|
| 25 |
function mxchat_activate() { |
| 26 |
global $wpdb; |
| 27 |
$charset_collate = $wpdb->get_charset_collate(); |
| 28 |
|
| 29 |
// Chat Transcripts Table |
| 30 |
$chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 31 |
$sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table ( |
| 32 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 33 |
user_id MEDIUMINT(9) DEFAULT 0, |
| 34 |
session_id VARCHAR(255) NOT NULL, |
| 35 |
role VARCHAR(255) NOT NULL, |
| 36 |
message TEXT NOT NULL, |
| 37 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 38 |
PRIMARY KEY (id) |
| 39 |
) $charset_collate;"; |
| 40 |
|
| 41 |
// System Prompt Content Table |
| 42 |
$system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 43 |
$sql_system_prompt = "CREATE TABLE $system_prompt_table ( |
| 44 |
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, |
| 45 |
url VARCHAR(255) NOT NULL, |
| 46 |
article_content LONGTEXT NOT NULL, |
| 47 |
embedding_vector LONGTEXT, |
| 48 |
source_url VARCHAR(255) DEFAULT NULL, |
| 49 |
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 50 |
PRIMARY KEY (id) |
| 51 |
) $charset_collate;"; |
| 52 |
|
| 53 |
// Intents Table |
| 54 |
$intents_table = $wpdb->prefix . 'mxchat_intents'; |
| 55 |
$sql_intents_table = "CREATE TABLE $intents_table ( |
| 56 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 57 |
intent_label VARCHAR(255) NOT NULL, |
| 58 |
phrases TEXT NOT NULL, |
| 59 |
embedding_vector LONGTEXT NOT NULL, |
| 60 |
callback_function VARCHAR(255) NOT NULL, |
| 61 |
similarity_threshold FLOAT DEFAULT 0.85, |
| 62 |
PRIMARY KEY (id) |
| 63 |
) $charset_collate;"; |
| 64 |
|
| 65 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 66 |
|
| 67 |
// Create or update tables |
| 68 |
dbDelta($sql_chat_transcripts); |
| 69 |
dbDelta($sql_system_prompt); |
| 70 |
dbDelta($sql_intents_table); |
| 71 |
|
| 72 |
// Ensure additional columns in `mxchat_system_prompt_content` and `mxchat_chat_transcripts` |
| 73 |
mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT'); |
| 74 |
mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 75 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)'); |
| 76 |
mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255)'); |
| 77 |
|
| 78 |
// Set default thresholds for existing intents |
| 79 |
$wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); |
| 80 |
|
| 81 |
// Update plugin version in the database |
| 82 |
update_option('mxchat_plugin_version', '1.4.3'); |
| 83 |
} |
| 84 |
|
| 85 |
function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| 86 |
global $wpdb; |
| 87 |
$column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name)); |
| 88 |
if (empty($column_exists)) { |
| 89 |
$wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
function mxchat_check_for_update() { |
| 94 |
$current_version = get_option('mxchat_plugin_version'); |
| 95 |
$plugin_version = '1.4.3'; |
| 96 |
|
| 97 |
if ($current_version !== $plugin_version) { |
| 98 |
mxchat_activate(); // Run the activation script which includes database migrations |
| 99 |
update_option('mxchat_plugin_version', $plugin_version); // Update to the latest version |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Run the update check function on every request |
| 104 |
add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 105 |
|
| 106 |
// Register activation hook |
| 107 |
register_activation_hook(__FILE__, 'mxchat_activate'); |
| 108 |
|
| 109 |
// Instantiate classes |
| 110 |
if (is_admin()) { |
| 111 |
$mxchat_admin = new MxChat_Admin(); |
| 112 |
} |
| 113 |
$mxchat_public = new MxChat_Public(); |
| 114 |
$mxchat_integrator = new MxChat_Integrator(); |
| 115 |
|
| 116 |
|
| 117 |
// Define admin_post actions for form submission handling |
| 118 |
add_action('admin_post_mxchat_add_intent', [ $mxchat_admin, 'mxchat_handle_add_intent' ]); |
| 119 |
add_action('admin_post_mxchat_delete_intent', [ $mxchat_admin, 'mxchat_handle_delete_intent' ]); |
| 120 |
add_action('admin_post_mxchat_update_intent_threshold', [ $mxchat_admin, 'mxchat_handle_update_intent_threshold' ]); |
| 121 |
|