| @@ -1,15 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 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.0.3 | |
| 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.2 | |
| 6 | 6 | * Author: MxChat |
| 7 | 7 | * Author URI: https://mxchat.ai |
| 8 | 8 | * License: GPLv2 or later |
| 9 | 9 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | - * Text Domain: mxchat | |
| 11 | - * Domain Path: /languages | |
| 12 | 10 | */ |
| 13 | 11 | |
| 14 | 12 | if (!defined('ABSPATH')) { |
| 15 | 13 | exit; // Exit if accessed directly. |
| @@ -14,14 +12,8 @@ | ||
| 14 | 12 | if (!defined('ABSPATH')) { |
| 15 | 13 | exit; // Exit if accessed directly. |
| 16 | 14 | } |
| 17 | 15 | |
| 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 | 16 | // Include classes |
| 25 | 17 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php'; |
| 26 | 18 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php'; |
| 27 | 19 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php'; |
| @@ -27,147 +19,105 @@ | ||
| 27 | 19 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php'; |
| 28 | 20 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php'; |
| 29 | 21 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php'; |
| 30 | 22 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-woocommerce.php'; |
| 31 | -require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; | |
| 32 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php'; | |
| 33 | 23 | |
| 34 | 24 | function mxchat_activate() { |
| 35 | 25 | global $wpdb; |
| 36 | - $charset_collate = $wpdb->get_charset_collate(); | |
| 37 | 26 | |
| 38 | 27 | // Chat Transcripts Table |
| 39 | 28 | $chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts'; |
| 40 | 29 | $sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table ( |
| 41 | - id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, | |
| 42 | - user_id MEDIUMINT(9) DEFAULT 0, | |
| 43 | - session_id VARCHAR(255) NOT NULL, | |
| 44 | - role VARCHAR(255) NOT NULL, | |
| 45 | - message TEXT NOT NULL, | |
| 46 | - user_email VARCHAR(255) DEFAULT NULL, | |
| 47 | - timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | |
| 48 | - PRIMARY KEY (id) | |
| 49 | - ) $charset_collate;"; | |
| 30 | + id mediumint(9) NOT NULL AUTO_INCREMENT, | |
| 31 | + user_id mediumint(9) DEFAULT 0, | |
| 32 | + session_id varchar(255) NOT NULL, | |
| 33 | + role varchar(255) NOT NULL, | |
| 34 | + message text NOT NULL, | |
| 35 | + timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, | |
| 36 | + PRIMARY KEY (id) | |
| 37 | + );"; | |
| 50 | 38 | |
| 51 | 39 | // System Prompt Content Table |
| 52 | 40 | $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content'; |
| 53 | 41 | $sql_system_prompt = "CREATE TABLE $system_prompt_table ( |
| 54 | - id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, | |
| 55 | - url VARCHAR(255) NOT NULL, | |
| 56 | - article_content LONGTEXT NOT NULL, | |
| 57 | - embedding_vector LONGTEXT, | |
| 58 | - source_url VARCHAR(255) DEFAULT NULL, | |
| 59 | - timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | |
| 60 | - PRIMARY KEY (id) | |
| 61 | - ) $charset_collate;"; | |
| 42 | + id mediumint(9) NOT NULL AUTO_INCREMENT, | |
| 43 | + url varchar(255) NOT NULL, | |
| 44 | + article_content longtext NOT NULL, | |
| 45 | + embedding_vector longtext, | |
| 46 | + timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, | |
| 47 | + PRIMARY KEY (id) | |
| 48 | + );"; | |
| 62 | 49 | |
| 63 | - // Intents Table | |
| 64 | - $intents_table = $wpdb->prefix . 'mxchat_intents'; | |
| 65 | - $sql_intents_table = "CREATE TABLE $intents_table ( | |
| 66 | - id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, | |
| 67 | - intent_label VARCHAR(255) NOT NULL, | |
| 68 | - phrases TEXT NOT NULL, | |
| 69 | - embedding_vector LONGTEXT NOT NULL, | |
| 70 | - callback_function VARCHAR(255) NOT NULL, | |
| 71 | - similarity_threshold FLOAT DEFAULT 0.85, | |
| 72 | - PRIMARY KEY (id) | |
| 73 | - ) $charset_collate;"; | |
| 50 | + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
| 74 | 51 | |
| 75 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
| 52 | + // Create or update the chat transcripts table | |
| 53 | + dbDelta($sql_chat_transcripts); | |
| 76 | 54 | |
| 77 | - // Create or update tables | |
| 78 | - dbDelta($sql_chat_transcripts); | |
| 55 | + // Create or update the system prompt content table | |
| 79 | 56 | dbDelta($sql_system_prompt); |
| 80 | - dbDelta($sql_intents_table); | |
| 81 | 57 | |
| 82 | - // Ensure additional columns in `mxchat_chat_transcripts` | |
| 83 | - mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)'); | |
| 84 | - mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255) DEFAULT NULL'); | |
| 58 | + // Ensure 'embedding_vector' column exists in the 'system_prompt_content' table | |
| 59 | + $column_exists = $wpdb->get_results($wpdb->prepare( | |
| 60 | + "SHOW COLUMNS FROM {$system_prompt_table} LIKE %s", | |
| 61 | + 'embedding_vector' | |
| 62 | + )); | |
| 63 | + if (empty($column_exists)) { | |
| 64 | + $wpdb->query( | |
| 65 | + "ALTER TABLE {$system_prompt_table} ADD COLUMN embedding_vector longtext" | |
| 66 | + ); | |
| 67 | + } | |
| 85 | 68 | |
| 86 | - // Ensure additional columns in `mxchat_system_prompt_content` | |
| 87 | - mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT'); | |
| 88 | - mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); | |
| 69 | + // Ensure 'source_url' column exists in the 'system_prompt_content' table | |
| 70 | + $source_url_exists = $wpdb->get_results($wpdb->prepare( | |
| 71 | + "SHOW COLUMNS FROM {$system_prompt_table} LIKE %s", | |
| 72 | + 'source_url' | |
| 73 | + )); | |
| 74 | + if (empty($source_url_exists)) { | |
| 75 | + $wpdb->query( | |
| 76 | + "ALTER TABLE {$system_prompt_table} ADD COLUMN source_url VARCHAR(255) DEFAULT NULL" | |
| 77 | + ); | |
| 78 | + } | |
| 89 | 79 | |
| 90 | - // Set default thresholds for existing intents | |
| 91 | - $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); | |
| 80 | + // Ensure 'user_identifier' and 'user_email' columns exist in the 'chat_transcripts' table | |
| 81 | + $user_identifier_exists = $wpdb->get_results($wpdb->prepare( | |
| 82 | + "SHOW COLUMNS FROM {$chat_transcripts_table} LIKE %s", | |
| 83 | + 'user_identifier' | |
| 84 | + )); | |
| 85 | + if (empty($user_identifier_exists)) { | |
| 86 | + $wpdb->query( | |
| 87 | + "ALTER TABLE {$chat_transcripts_table} ADD COLUMN user_identifier VARCHAR(255)" | |
| 88 | + ); | |
| 89 | + } | |
| 92 | 90 | |
| 93 | - // Update plugin version in the database | |
| 94 | - update_option('mxchat_plugin_version', '2.0.3'); | |
| 95 | -} | |
| 91 | + $user_email_exists = $wpdb->get_results($wpdb->prepare( | |
| 92 | + "SHOW COLUMNS FROM {$chat_transcripts_table} LIKE %s", | |
| 93 | + 'user_email' | |
| 94 | + )); | |
| 95 | + if (empty($user_email_exists)) { | |
| 96 | + $wpdb->query( | |
| 97 | + "ALTER TABLE {$chat_transcripts_table} ADD COLUMN user_email VARCHAR(255)" | |
| 98 | + ); | |
| 99 | + } | |
| 96 | 100 | |
| 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 | - } | |
| 101 | + // Update the version number in the database | |
| 102 | + update_option('mxchat_plugin_version', '1.2'); | |
| 104 | 103 | } |
| 105 | 104 | |
| 106 | 105 | function mxchat_check_for_update() { |
| 107 | 106 | $current_version = get_option('mxchat_plugin_version'); |
| 108 | - $plugin_version = '2.0.3'; // Update with your latest version | |
| 107 | + $plugin_version = '1.2'; | |
| 109 | 108 | |
| 110 | 109 | if ($current_version !== $plugin_version) { |
| 111 | - mxchat_activate(); // Run the activation script to apply schema changes | |
| 112 | - mxchat_migrate_live_agent_status(); // Add migration for live agent status | |
| 113 | - update_option('mxchat_plugin_version', $plugin_version); // Update the version | |
| 110 | + mxchat_activate(); // Run the activation script which includes database migrations | |
| 111 | + update_option('mxchat_plugin_version', $plugin_version); // Update to the latest version | |
| 114 | 112 | } |
| 115 | 113 | } |
| 116 | 114 | |
| 117 | -function mxchat_migrate_live_agent_status() { | |
| 118 | - $options = get_option('mxchat_options', []); | |
| 119 | - | |
| 120 | - // Check if live_agent_status exists | |
| 121 | - if (isset($options['live_agent_status'])) { | |
| 122 | - $current_status = $options['live_agent_status']; | |
| 123 | - $needs_update = false; | |
| 124 | - | |
| 125 | - // Convert to new format if needed | |
| 126 | - if ($current_status === 'online') { | |
| 127 | - $options['live_agent_status'] = 'on'; | |
| 128 | - $needs_update = true; | |
| 129 | - } else if ($current_status === 'offline') { | |
| 130 | - $options['live_agent_status'] = 'off'; | |
| 131 | - $needs_update = true; | |
| 132 | - } else if (!in_array($current_status, ['on', 'off'])) { | |
| 133 | - // Default to off for any unexpected values | |
| 134 | - $options['live_agent_status'] = 'off'; | |
| 135 | - $needs_update = true; | |
| 136 | - } | |
| 137 | - | |
| 138 | - // Only update if needed | |
| 139 | - if ($needs_update) { | |
| 140 | - update_option('mxchat_options', $options); | |
| 141 | - } | |
| 142 | - } else { | |
| 143 | - // If status doesn't exist, set default to off | |
| 144 | - $options['live_agent_status'] = 'off'; | |
| 145 | - update_option('mxchat_options', $options); | |
| 146 | - } | |
| 147 | -} | |
| 148 | - | |
| 149 | - | |
| 150 | 115 | // Run the update check function on every request |
| 151 | 116 | add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 152 | 117 | |
| 153 | 118 | // Register activation hook |
| 154 | 119 | register_activation_hook(__FILE__, 'mxchat_activate'); |
| 155 | - | |
| 156 | - | |
| 157 | -add_filter('cron_schedules', function($schedules) { | |
| 158 | - $schedules['one_minute'] = array( | |
| 159 | - 'interval' => 60, | |
| 160 | - 'display' => 'Every Minute' | |
| 161 | - ); | |
| 162 | - return $schedules; | |
| 163 | -}); | |
| 164 | - | |
| 165 | -add_action('init', function() { | |
| 166 | - add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5); | |
| 167 | - add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5); | |
| 168 | -}); | |
| 169 | - | |
| 170 | 120 | |
| 171 | 121 | // Instantiate classes |
| 172 | 122 | if (is_admin()) { |
| 173 | 123 | $mxchat_admin = new MxChat_Admin(); |