| @@ -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.6 | |
| 5 | + * Version: 2.2.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 |
| @@ -14,8 +14,10 @@ | ||
| 14 | 14 | if (!defined('ABSPATH')) { |
| 15 | 15 | exit; // Exit if accessed directly. |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | +// ADD THIS: Define plugin version constant for asset versioning | |
| 19 | +define('MXCHAT_VERSION', '2.2.2'); | |
| 18 | 20 | |
| 19 | 21 | function mxchat_load_textdomain() { |
| 20 | 22 | load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 21 | 23 | } |
| @@ -28,9 +30,13 @@ | ||
| 28 | 30 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php'; |
| 29 | 31 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php'; |
| 30 | 32 | require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php'; |
| 31 | 33 | require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php'; |
| 34 | +require_once plugin_dir_path(__FILE__) . 'admin/class-ajax-handler.php'; | |
| 35 | +require_once plugin_dir_path(__FILE__) . 'admin/class-pinecone-manager.php'; | |
| 36 | +require_once plugin_dir_path(__FILE__) . 'admin/class-knowledge-manager.php'; | |
| 32 | 37 | |
| 38 | + | |
| 33 | 39 | function mxchat_activate() { |
| 34 | 40 | global $wpdb; |
| 35 | 41 | $charset_collate = $wpdb->get_charset_collate(); |
| 36 | 42 | |
| @@ -87,11 +93,12 @@ | ||
| 87 | 93 | mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL'); |
| 88 | 94 | |
| 89 | 95 | // Set default thresholds for existing intents |
| 90 | 96 | $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL"); |
| 97 | + mxchat_add_missing_columns($intents_table, 'enabled', 'TINYINT(1) NOT NULL DEFAULT 1'); | |
| 91 | 98 | |
| 92 | - // Update plugin version in the database | |
| 93 | - update_option('mxchat_plugin_version', '2.0.6'); | |
| 99 | + // CHANGE THIS: Use the constant instead of hardcoded version | |
| 100 | + update_option('mxchat_plugin_version', MXCHAT_VERSION); | |
| 94 | 101 | } |
| 95 | 102 | |
| 96 | 103 | |
| 97 | 104 | function mxchat_add_missing_columns($table, $column_name, $column_type) { |
| @@ -102,24 +109,90 @@ | ||
| 102 | 109 | } |
| 103 | 110 | } |
| 104 | 111 | |
| 105 | 112 | function mxchat_check_for_update() { |
| 113 | + //error_log('=== mxchat_check_for_update called ==='); | |
| 114 | + | |
| 106 | 115 | $current_version = get_option('mxchat_plugin_version'); |
| 107 | - $plugin_version = '2.0.6'; // Update with your latest version | |
| 116 | + $plugin_version = MXCHAT_VERSION; | |
| 117 | + | |
| 118 | + //error_log('Current stored version: ' . $current_version); | |
| 119 | + //error_log('Plugin version constant: ' . $plugin_version); | |
| 108 | 120 | |
| 109 | 121 | if ($current_version !== $plugin_version) { |
| 110 | - mxchat_activate(); // Run the activation script to apply schema changes | |
| 111 | - mxchat_migrate_live_agent_status(); // Add migration for live agent status | |
| 112 | - update_option('mxchat_plugin_version', $plugin_version); // Update the version | |
| 122 | + //error_log('Version mismatch detected, running updates'); | |
| 123 | + | |
| 124 | + // IMPORTANT: Run live agent update BEFORE updating the stored version | |
| 125 | + mxchat_handle_live_agent_update(); | |
| 126 | + | |
| 127 | + // Run your existing update processes | |
| 128 | + mxchat_activate(); | |
| 129 | + mxchat_migrate_live_agent_status(); | |
| 130 | + | |
| 131 | + // Add the new cleanup function for version 2.1.8 | |
| 132 | + if (version_compare($current_version, '2.1.8', '<')) { | |
| 133 | + $deleted = mxchat_cleanup_orphaned_chat_history(); | |
| 134 | + //error_log(sprintf('MxChat cleanup: Removed %d orphaned chat history entries', $deleted)); | |
| 135 | + } | |
| 136 | + | |
| 137 | + // Update version LAST so the live agent update logic can work | |
| 138 | + update_option('mxchat_plugin_version', $plugin_version); | |
| 139 | + //error_log('Version updated to: ' . $plugin_version); | |
| 140 | + } else { | |
| 141 | + //error_log('Versions match, no update needed'); | |
| 113 | 142 | } |
| 114 | 143 | } |
| 115 | 144 | |
| 145 | +/** | |
| 146 | + * Clean up orphaned chat history options from the wp_options table | |
| 147 | + * @return int Number of options deleted | |
| 148 | + */ | |
| 149 | +function mxchat_cleanup_orphaned_chat_history() { | |
| 150 | + global $wpdb; | |
| 151 | + $count = 0; | |
| 152 | + | |
| 153 | + // Get all option keys that match our pattern | |
| 154 | + $history_options = $wpdb->get_results( | |
| 155 | + "SELECT option_name FROM {$wpdb->options} | |
| 156 | + WHERE option_name LIKE 'mxchat_history_%'" | |
| 157 | + ); | |
| 158 | + | |
| 159 | + if (!empty($history_options)) { | |
| 160 | + foreach ($history_options as $option) { | |
| 161 | + // Extract the session ID from the option name | |
| 162 | + $session_id = str_replace('mxchat_history_', '', $option->option_name); | |
| 163 | + | |
| 164 | + // Check if this session still exists in the custom table | |
| 165 | + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts'; | |
| 166 | + $exists = $wpdb->get_var( | |
| 167 | + $wpdb->prepare( | |
| 168 | + "SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", | |
| 169 | + $session_id | |
| 170 | + ) | |
| 171 | + ); | |
| 172 | + | |
| 173 | + // If session doesn't exist in the main table, delete the option | |
| 174 | + if ($exists == 0) { | |
| 175 | + delete_option($option->option_name); | |
| 176 | + // Also delete related metadata | |
| 177 | + delete_option("mxchat_email_{$session_id}"); | |
| 178 | + delete_option("mxchat_agent_name_{$session_id}"); | |
| 179 | + $count++; | |
| 180 | + } | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + return $count; | |
| 185 | +} | |
| 186 | + | |
| 116 | 187 | function mxchat_migrate_live_agent_status() { |
| 188 | + //error_log('=== mxchat_migrate_live_agent_status called ==='); | |
| 117 | 189 | $options = get_option('mxchat_options', []); |
| 118 | 190 | |
| 119 | 191 | // Check if live_agent_status exists |
| 120 | 192 | if (isset($options['live_agent_status'])) { |
| 121 | 193 | $current_status = $options['live_agent_status']; |
| 194 | + //error_log('Current live agent status: ' . $current_status); | |
| 122 | 195 | $needs_update = false; |
| 123 | 196 | |
| 124 | 197 | // Convert to new format if needed |
| 125 | 198 | if ($current_status === 'online') { |
| @@ -124,28 +197,84 @@ | ||
| 124 | 197 | // Convert to new format if needed |
| 125 | 198 | if ($current_status === 'online') { |
| 126 | 199 | $options['live_agent_status'] = 'on'; |
| 127 | 200 | $needs_update = true; |
| 201 | + //error_log('Converting online -> on'); | |
| 128 | 202 | } else if ($current_status === 'offline') { |
| 129 | 203 | $options['live_agent_status'] = 'off'; |
| 130 | 204 | $needs_update = true; |
| 205 | + //error_log('Converting offline -> off'); | |
| 131 | 206 | } else if (!in_array($current_status, ['on', 'off'])) { |
| 132 | 207 | // Default to off for any unexpected values |
| 133 | 208 | $options['live_agent_status'] = 'off'; |
| 134 | 209 | $needs_update = true; |
| 210 | + //error_log('Unexpected value, setting to off'); | |
| 135 | 211 | } |
| 136 | 212 | |
| 137 | 213 | // Only update if needed |
| 138 | 214 | if ($needs_update) { |
| 139 | 215 | update_option('mxchat_options', $options); |
| 216 | + //error_log('Live agent status updated'); | |
| 140 | 217 | } |
| 141 | 218 | } else { |
| 142 | 219 | // If status doesn't exist, set default to off |
| 143 | 220 | $options['live_agent_status'] = 'off'; |
| 144 | 221 | update_option('mxchat_options', $options); |
| 222 | + //error_log('Live agent status not set, defaulting to off'); | |
| 145 | 223 | } |
| 146 | 224 | } |
| 147 | 225 | |
| 226 | +// FIXED: This function had the wrong logic | |
| 227 | +function mxchat_handle_live_agent_update() { | |
| 228 | + //error_log('=== mxchat_handle_live_agent_update called ==='); | |
| 229 | + | |
| 230 | + // Get the CURRENT stored version (before it gets updated) | |
| 231 | + $current_version = get_option('mxchat_plugin_version', '0.0.0'); | |
| 232 | + $new_version = '2.2.2'; | |
| 233 | + | |
| 234 | + //error_log('Live Agent Update - Current stored version: ' . $current_version); | |
| 235 | + //error_log('Live Agent Update - Target version: ' . $new_version); | |
| 236 | + | |
| 237 | + // Only run this once for the update to 2.2.2 | |
| 238 | + $update_handled = get_option('mxchat_live_agent_update_2_2_2_handled', false); | |
| 239 | + //error_log('Live Agent Update - Already handled: ' . ($update_handled ? 'yes' : 'no')); | |
| 240 | + | |
| 241 | + // FIXED: Check if we're upgrading TO 2.2.2 and haven't handled this yet | |
| 242 | + if (version_compare($current_version, $new_version, '<') && !$update_handled) { | |
| 243 | + //error_log('Live Agent Update - Conditions met, checking live agent status'); | |
| 244 | + | |
| 245 | + $options = get_option('mxchat_options', array()); | |
| 246 | + $live_agent_status = isset($options['live_agent_status']) ? $options['live_agent_status'] : 'not set'; | |
| 247 | + //error_log('Live Agent Update - Current status: ' . $live_agent_status); | |
| 248 | + | |
| 249 | + // Check if live agent was previously enabled | |
| 250 | + if (isset($options['live_agent_status']) && $options['live_agent_status'] === 'on') { | |
| 251 | + //error_log('Live Agent Update - Live agent was ON, disabling it'); | |
| 252 | + | |
| 253 | + // Disable live agent | |
| 254 | + $options['live_agent_status'] = 'off'; | |
| 255 | + update_option('mxchat_options', $options); | |
| 256 | + | |
| 257 | + // Set flag to show the notification banner | |
| 258 | + update_option('mxchat_show_live_agent_disabled_notice', true); | |
| 259 | + //error_log('Live Agent Update - Live agent disabled and banner flag set'); | |
| 260 | + } else { | |
| 261 | + //error_log('Live Agent Update - Live agent was not ON (status: ' . $live_agent_status . '), no action taken'); | |
| 262 | + } | |
| 263 | + | |
| 264 | + // Mark this update as handled | |
| 265 | + update_option('mxchat_live_agent_update_2_2_2_handled', true); | |
| 266 | + //error_log('Live Agent Update - Marked as handled'); | |
| 267 | + } else { | |
| 268 | + //error_log('Live Agent Update - Conditions not met'); | |
| 269 | + if (version_compare($current_version, $new_version, '>=')) { | |
| 270 | + //error_log('Live Agent Update - Already on version ' . $new_version . ' or higher'); | |
| 271 | + } | |
| 272 | + if ($update_handled) { | |
| 273 | + //error_log('Live Agent Update - Already handled this update'); | |
| 274 | + } | |
| 275 | + } | |
| 276 | +} | |
| 148 | 277 | |
| 149 | 278 | // Run the update check function on every request |
| 150 | 279 | add_action('plugins_loaded', 'mxchat_check_for_update'); |
| 151 | 280 | |
| @@ -160,16 +289,18 @@ | ||
| 160 | 289 | ); |
| 161 | 290 | return $schedules; |
| 162 | 291 | }); |
| 163 | 292 | |
| 164 | -add_action('init', function() { | |
| 165 | - add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5); | |
| 166 | - add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5); | |
| 167 | -}); | |
| 168 | 293 | |
| 169 | - | |
| 170 | 294 | // Instantiate classes |
| 171 | 295 | if (is_admin()) { |
| 172 | - $mxchat_admin = new MxChat_Admin(); | |
| 296 | + // Create the knowledge manager instance first | |
| 297 | + $mxchat_knowledge_manager = new MxChat_Knowledge_Manager(); | |
| 298 | + | |
| 299 | + // Pass it to your admin class | |
| 300 | + $mxchat_admin = new MxChat_Admin($mxchat_knowledge_manager); | |
| 173 | 301 | } |
| 174 | 302 | $mxchat_public = new MxChat_Public(); |
| 175 | 303 | $mxchat_integrator = new MxChat_Integrator(); |
| 304 | + | |
| 305 | +// ADD THIS LINE HERE: | |
| 306 | +register_deactivation_hook(__FILE__, array($mxchat_integrator, 'cleanup_cron_events')); | |