| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: MxChat |
| 4 | 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.5 | |
| 5 | + * Version: 2.2.1 | |
| 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 |
| @@ -26,9 +26,8 @@ | ||
| 26 | 26 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php'; |
| 27 | 27 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php'; |
| 28 | 28 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php'; |
| 29 | 29 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php'; |
| 30 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-woocommerce.php'; | |
| 31 | 30 | require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; |
| 32 | 31 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php'; |
| 33 | 32 | |
| 34 | 33 | function mxchat_activate() { |
| @@ -88,11 +87,12 @@ | ||
| 88 | 87 | mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 89 | 88 | |
| 90 | 89 | // Set default thresholds for existing intents |
| 91 | 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 | 92 | |
| 93 | 93 | // Update plugin version in the database |
| 94 | - update_option('mxchat_plugin_version', '2.0.5'); | |
| 94 | + update_option('mxchat_plugin_version', '2.2.1'); | |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | |
| 98 | 98 | function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| @@ -104,15 +104,68 @@ | ||
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | function mxchat_check_for_update() { |
| 107 | 107 | $current_version = get_option('mxchat_plugin_version'); |
| 108 | - $plugin_version = '2.0.5'; // Update with your latest version | |
| 109 | - | |
| 108 | + $plugin_version = '2.2.1'; // Increment to your next version | |
| 109 | + | |
| 110 | 110 | if ($current_version !== $plugin_version) { |
| 111 | + // Run your existing update processes | |
| 111 | 112 | mxchat_activate(); // Run the activation script to apply schema changes |
| 112 | 113 | mxchat_migrate_live_agent_status(); // Add migration for live agent status |
| 113 | - update_option('mxchat_plugin_version', $plugin_version); // Update the version | |
| 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); | |
| 114 | 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; | |
| 115 | 168 | } |
| 116 | 169 | |
| 117 | 170 | function mxchat_migrate_live_agent_status() { |
| 118 | 171 | $options = get_option('mxchat_options', []); |