| @@ -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.9 | |
| 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 |
| @@ -14,8 +14,10 @@ | ||
| 14 | 14 | if (!defined('ABSPATH')) { |
| 15 | 15 | exit; // Exit if accessed directly. |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | +// Define plugin version constant for asset versioning | |
| 19 | +define('MXCHAT_VERSION', '2.2.9'); | |
| 18 | 20 | |
| 19 | 21 | function mxchat_load_textdomain() { |
| 20 | 22 | load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 21 | 23 | } |
| @@ -20,18 +22,33 @@ | ||
| 20 | 22 | load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 21 | 23 | } |
| 22 | 24 | add_action('plugins_loaded', 'mxchat_load_textdomain'); |
| 23 | 25 | |
| 24 | -// Include classes | |
| 25 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php'; | |
| 26 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php'; | |
| 27 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php'; | |
| 28 | -require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php'; | |
| 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 | -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'; | |
| 26 | +// Include classes with error handling | |
| 27 | +function mxchat_include_classes() { | |
| 28 | + $class_files = array( | |
| 29 | + 'includes/class-mxchat-integrator.php', | |
| 30 | + 'includes/class-mxchat-admin.php', | |
| 31 | + 'includes/class-mxchat-public.php', | |
| 32 | + 'includes/class-mxchat-utils.php', | |
| 33 | + 'includes/class-mxchat-user.php', | |
| 34 | + 'includes/pdf-parser/alt_autoload.php', | |
| 35 | + 'includes/class-mxchat-word-handler.php', | |
| 36 | + 'admin/class-ajax-handler.php', | |
| 37 | + 'admin/class-pinecone-manager.php', | |
| 38 | + 'admin/class-knowledge-manager.php' | |
| 39 | + ); | |
| 33 | 40 | |
| 41 | + foreach ($class_files as $file) { | |
| 42 | + $file_path = plugin_dir_path(__FILE__) . $file; | |
| 43 | + if (file_exists($file_path)) { | |
| 44 | + require_once $file_path; | |
| 45 | + } else { | |
| 46 | + error_log('MxChat: Missing class file - ' . $file); | |
| 47 | + } | |
| 48 | + } | |
| 49 | +} | |
| 50 | + | |
| 34 | 51 | function mxchat_activate() { |
| 35 | 52 | global $wpdb; |
| 36 | 53 | $charset_collate = $wpdb->get_charset_collate(); |
| 37 | 54 | |
| @@ -88,14 +105,14 @@ | ||
| 88 | 105 | mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 89 | 106 | |
| 90 | 107 | // Set default thresholds for existing intents |
| 91 | 108 | $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); |
| 109 | + mxchat_add_missing_columns($intents_table, 'enabled', 'TINYINT(1) NOT NULL DEFAULT 1'); | |
| 92 | 110 | |
| 93 | - // Update plugin version in the database | |
| 94 | - update_option('mxchat_plugin_version', '2.0.5'); | |
| 111 | + // Use the constant instead of hardcoded version | |
| 112 | + update_option('mxchat_plugin_version', MXCHAT_VERSION); | |
| 95 | 113 | } |
| 96 | 114 | |
| 97 | - | |
| 98 | 115 | function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| 99 | 116 | global $wpdb; |
| 100 | 117 | $column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name)); |
| 101 | 118 | if (empty($column_exists)) { |
| @@ -103,18 +120,76 @@ | ||
| 103 | 120 | } |
| 104 | 121 | } |
| 105 | 122 | |
| 106 | 123 | function mxchat_check_for_update() { |
| 107 | - $current_version = get_option('mxchat_plugin_version'); | |
| 108 | - $plugin_version = '2.0.5'; // Update with your latest version | |
| 124 | + try { | |
| 125 | + $current_version = get_option('mxchat_plugin_version'); | |
| 126 | + $plugin_version = MXCHAT_VERSION; | |
| 109 | 127 | |
| 110 | - 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 | |
| 128 | + if ($current_version !== $plugin_version) { | |
| 129 | + // Run live agent update BEFORE updating the stored version | |
| 130 | + mxchat_handle_live_agent_update(); | |
| 131 | + | |
| 132 | + // Run existing update processes | |
| 133 | + mxchat_activate(); | |
| 134 | + mxchat_migrate_live_agent_status(); | |
| 135 | + | |
| 136 | + // Add the new cleanup function for version 2.1.8 | |
| 137 | + if (version_compare($current_version, '2.1.8', '<')) { | |
| 138 | + $deleted = mxchat_cleanup_orphaned_chat_history(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + // Update version LAST so the live agent update logic can work | |
| 142 | + update_option('mxchat_plugin_version', $plugin_version); | |
| 143 | + } | |
| 144 | + } catch (Exception $e) { | |
| 145 | + error_log('MxChat update error: ' . $e->getMessage()); | |
| 146 | + // Don't update version if there was an error | |
| 114 | 147 | } |
| 115 | 148 | } |
| 116 | 149 | |
| 150 | +/** | |
| 151 | + * Clean up orphaned chat history options from the wp_options table | |
| 152 | + * @return int Number of options deleted | |
| 153 | + */ | |
| 154 | +function mxchat_cleanup_orphaned_chat_history() { | |
| 155 | + global $wpdb; | |
| 156 | + $count = 0; | |
| 157 | + | |
| 158 | + // Get all option keys that match our pattern | |
| 159 | + $history_options = $wpdb->get_results( | |
| 160 | + "SELECT option_name FROM {$wpdb->options} | |
| 161 | + WHERE option_name LIKE 'mxchat_history_%'" | |
| 162 | + ); | |
| 163 | + | |
| 164 | + if (!empty($history_options)) { | |
| 165 | + foreach ($history_options as $option) { | |
| 166 | + // Extract the session ID from the option name | |
| 167 | + $session_id = str_replace('mxchat_history_', '', $option->option_name); | |
| 168 | + | |
| 169 | + // Check if this session still exists in the custom table | |
| 170 | + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts'; | |
| 171 | + $exists = $wpdb->get_var( | |
| 172 | + $wpdb->prepare( | |
| 173 | + "SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", | |
| 174 | + $session_id | |
| 175 | + ) | |
| 176 | + ); | |
| 177 | + | |
| 178 | + // If session doesn't exist in the main table, delete the option | |
| 179 | + if ($exists == 0) { | |
| 180 | + delete_option($option->option_name); | |
| 181 | + // Also delete related metadata | |
| 182 | + delete_option("mxchat_email_{$session_id}"); | |
| 183 | + delete_option("mxchat_agent_name_{$session_id}"); | |
| 184 | + $count++; | |
| 185 | + } | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 189 | + return $count; | |
| 190 | +} | |
| 191 | + | |
| 117 | 192 | function mxchat_migrate_live_agent_status() { |
| 118 | 193 | $options = get_option('mxchat_options', []); |
| 119 | 194 | |
| 120 | 195 | // Check if live_agent_status exists |
| @@ -145,16 +220,87 @@ | ||
| 145 | 220 | update_option('mxchat_options', $options); |
| 146 | 221 | } |
| 147 | 222 | } |
| 148 | 223 | |
| 224 | +function mxchat_handle_live_agent_update() { | |
| 225 | + // Get the CURRENT stored version (before it gets updated) | |
| 226 | + $current_version = get_option('mxchat_plugin_version', '0.0.0'); | |
| 227 | + $new_version = '2.2.2'; | |
| 228 | + | |
| 229 | + // Only run this once for the update to 2.2.2 | |
| 230 | + $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false); | |
| 231 | + | |
| 232 | + // Check if we're upgrading TO 2.2.2 and haven't handled this yet | |
| 233 | + if (version_compare($current_version, $new_version, '<') && !$update_handled) { | |
| 234 | + $options = get_option('mxchat_options', array()); | |
| 235 | + | |
| 236 | + // Check if live agent was previously enabled | |
| 237 | + if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') { | |
| 238 | + // Disable live agent | |
| 239 | + $options['live_agent_status'] = 'off'; | |
| 240 | + update_option('mxchat_options', $options); | |
| 241 | + | |
| 242 | + // Set flag to show the notification banner | |
| 243 | + update_option('mxchat_show_live_agent_disabled_notice', true); | |
| 244 | + } | |
| 245 | + | |
| 246 | + // Mark this update as handled | |
| 247 | + update_option('mxchat_live_agent_update_2_2_2_handled', true); | |
| 248 | + } | |
| 249 | +} | |
| 149 | 250 | |
| 150 | -// Run the update check function on every request | |
| 151 | -add_action('plugins_loaded', 'mxchat_check_for_update'); | |
| 251 | +// Initialize plugin safely | |
| 252 | +function mxchat_init() { | |
| 253 | + // Include all class files first | |
| 254 | + mxchat_include_classes(); | |
| 255 | + | |
| 256 | + // Run update check | |
| 257 | + mxchat_check_for_update(); | |
| 258 | + | |
| 259 | + // Initialize classes with error handling | |
| 260 | + try { | |
| 261 | + // Initialize admin classes | |
| 262 | + if (is_admin()) { | |
| 263 | + if (class_exists('MxChat_Knowledge_Manager')) { | |
| 264 | + $mxchat_knowledge_manager = new MxChat_Knowledge_Manager(); | |
| 265 | + | |
| 266 | + if (class_exists('MxChat_Admin')) { | |
| 267 | + $mxchat_admin = new MxChat_Admin($mxchat_knowledge_manager); | |
| 268 | + } | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 272 | + // Initialize public classes | |
| 273 | + if (class_exists('MxChat_Public')) { | |
| 274 | + $mxchat_public = new MxChat_Public(); | |
| 275 | + } | |
| 276 | + | |
| 277 | + if (class_exists('MxChat_Integrator')) { | |
| 278 | + $mxchat_integrator = new MxChat_Integrator(); | |
| 279 | + } | |
| 280 | + | |
| 281 | + } catch (Exception $e) { | |
| 282 | + error_log('MxChat initialization error: ' . $e->getMessage()); | |
| 283 | + | |
| 284 | + // Show admin notice if there's an error | |
| 285 | + if (is_admin()) { | |
| 286 | + add_action('admin_notices', function() use ($e) { | |
| 287 | + echo '<div class="notice notice-error"><p>'; | |
| 288 | + echo '<strong>MxChat Error:</strong> Plugin initialization failed. '; | |
| 289 | + echo 'Please check error logs or contact support. Error: ' . esc_html($e->getMessage()); | |
| 290 | + echo '</p></div>'; | |
| 291 | + }); | |
| 292 | + } | |
| 293 | + } | |
| 294 | +} | |
| 152 | 295 | |
| 296 | +// Run initialization on plugins_loaded | |
| 297 | +add_action('plugins_loaded', 'mxchat_init'); | |
| 298 | + | |
| 153 | 299 | // Register activation hook |
| 154 | 300 | register_activation_hook(__FILE__, 'mxchat_activate'); |
| 155 | 301 | |
| 156 | - | |
| 302 | +// Add cron schedule | |
| 157 | 303 | add_filter('cron_schedules', function($schedules) { |
| 158 | 304 | $schedules['one_minute'] = array( |
| 159 | 305 | 'interval' => 60, |
| 160 | 306 | 'display' => 'Every Minute' |
| @@ -161,16 +307,13 @@ | ||
| 161 | 307 | ); |
| 162 | 308 | return $schedules; |
| 163 | 309 | }); |
| 164 | 310 | |
| 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 | - | |
| 171 | -// Instantiate classes | |
| 172 | -if (is_admin()) { | |
| 173 | - $mxchat_admin = new MxChat_Admin(); | |
| 174 | -} | |
| 175 | -$mxchat_public = new MxChat_Public(); | |
| 176 | -$mxchat_integrator = new MxChat_Integrator(); | |
| 311 | +// Register deactivation hook - wrap in a callback to ensure the integrator exists | |
| 312 | +register_deactivation_hook(__FILE__, function() { | |
| 313 | + if (class_exists('MxChat_Integrator')) { | |
| 314 | + $integrator = new MxChat_Integrator(); | |
| 315 | + if (method_exists($integrator, 'cleanup_cron_events')) { | |
| 316 | + $integrator->cleanup_cron_events(); | |
| 317 | + } | |
| 318 | + } | |
| 319 | +}); | |