prefix}" . ABMSENSE_PREFIX . 'temp_data'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, page_title text NOT NULL, time_spent int NOT NULL, page_view int NOT NULL, visitor_ip varchar(100) NOT NULL, header_ip varchar(100), visitor_city varchar(100), visitor_country varchar(100), visitor_company varchar(255), account_id varchar(100) NOT NULL, last_update date NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); // Set a transient to indicate that the plugin has just been activated set_transient(ABMSENSE_PREFIX . 'activation', true, 30); } // Function to set the plugin settings to default values function abmsense_complete_activation() { // Check if the activation flag is set if (!get_transient(ABMSENSE_PREFIX . 'activation')) { return; } delete_transient(ABMSENSE_PREFIX . 'activation'); abmsense_update_plugin_data(); } // Function to deactivate the plugin function abmsense_deactivate() { delete_transient(ABMSENSE_PREFIX . 'activation'); delete_transient(ABMSENSE_PREFIX . 'upgrade_completed'); } // Function to handle plugin upgrades function abmsense_upgrade_completed() { // Check if we're in the middle of an upgrade if (get_transient(ABMSENSE_PREFIX . 'upgrade_in_progress')) { return; } // Check if upgrade is needed $current_version = abmsense_get_version(); $stored_version = get_option(ABMSENSE_PREFIX . 'version_installed'); if (abmsense_version_check()) { // Set a longer-lived transient to prevent multiple upgrade attempts set_transient(ABMSENSE_PREFIX . 'upgrade_completed', true, DAY_IN_SECONDS); // Set a short-lived transient to prevent concurrent upgrades set_transient(ABMSENSE_PREFIX . 'upgrade_in_progress', true, 60); try { // Get the current version $current_version = abmsense_get_version(); // Update plugin data with the new version $api_result = abmsense_update_plugin_data(); // Only update the local version if the API call succeeded if ($api_result) { update_option(ABMSENSE_PREFIX . 'version_installed', $current_version); } // Run any additional upgrade tasks abmsense_run_version_updates(); } finally { // Always clear the in-progress flag delete_transient(ABMSENSE_PREFIX . 'upgrade_in_progress'); } } } // Function to update plugin data function abmsense_update_plugin_data() { $current_user = wp_get_current_user(); $server_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; $admin_email = $current_user->user_email ?: 'no-reply@' . $server_name; $report_email = get_option(ABMSENSE_PREFIX . 'report_email') ?: $admin_email; $report_frequency = get_option(ABMSENSE_PREFIX . 'report_frequency', 'weekly'); $is_consent = get_option(ABMSENSE_PREFIX . 'is_consent') ?: 0; $date_registered = get_option(ABMSENSE_PREFIX . 'date_registered') ?: gmdate('Y-m-d'); $version_installed = abmsense_get_version(); $full_name = $current_user->display_name ?: ''; update_option(ABMSENSE_PREFIX . 'full_name', $full_name); // Use the same values as the settings page $consent_update = true; // Match settings page logic for remote update $sku = 'ABM-1000-ID-MONTHLY-FREE-TIER'; $real_time_alert = 'No'; // Call the same function as the settings page $result = abmsense_save_data( $server_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed, $full_name, $consent_update, $sku, $real_time_alert ); // If save was successful, ensure we have a user entry if ($result) { // Clear any cached user data to force a fresh fetch $cache_key = 'abmsense_users_' . md5($admin_email); delete_transient($cache_key); // Clear the consent cache $consent_cache_key = 'abmsense_consent_' . md5($server_name); delete_transient($consent_cache_key); } return $result; } function abmsense_run_version_updates() { // Add any version-specific upgrade tasks here // Clear any caches that might affect the plugin wp_cache_flush(); delete_transient(ABMSENSE_PREFIX . 'upgrade_completed'); } add_action('admin_init', 'abmsense_complete_activation'); add_action('admin_init', 'abmsense_upgrade_completed'); register_deactivation_hook(__FILE__, 'abmsense_deactivate'); function abmsense_check_version_on_plugins_page($upgrader_object, $options) { if ($options['action'] === 'update' && $options['type'] === 'plugin') { // Check if our plugin was updated $our_plugin = plugin_basename(__FILE__); if (isset($options['plugins']) && in_array($our_plugin, $options['plugins'])) { delete_option(ABMSENSE_PREFIX . 'version_installed'); } } } add_action('upgrader_process_complete', 'abmsense_check_version_on_plugins_page', 10, 2);