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); // Active for 30 seconds } // 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'); $current_user = wp_get_current_user(); $admin_email = $current_user->user_email ?: 'no-reply@' . sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'] ?? '')); $report_email = get_option(ABMSENSE_PREFIX . 'report_email') ?: $admin_email; $report_frequency = 'daily'; $is_consent = 0; $date_registered = gmdate('Y-m-d'); $version_installed = abmsense_get_version(); $server_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; // Save data using WordPress functions abmsense_save_data($server_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed); } function abmsense_version_check() { $current_version = abmsense_get_version(); $stored_version = get_option(ABMSENSE_PREFIX . 'version_installed'); if ($current_version !== $stored_version) { $server_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; $report_email = get_option(ABMSENSE_PREFIX . 'report_email'); $report_frequency = get_option(ABMSENSE_PREFIX . 'report_frequency'); $is_consent = get_option(ABMSENSE_PREFIX . 'is_consent'); $date_registered = gmdate('Y-m-d'); // Update the options with the new version update_option(ABMSENSE_PREFIX . 'server_name', $server_name); update_option(ABMSENSE_PREFIX . 'report_email', $report_email); update_option(ABMSENSE_PREFIX . 'report_frequency', $report_frequency); update_option(ABMSENSE_PREFIX . 'is_consent', $is_consent); update_option(ABMSENSE_PREFIX . 'date_registered', $date_registered); update_option(ABMSENSE_PREFIX . 'version_installed', $current_version); return true; } return false; } // Function to deactivate the plugin function abmsense_deactivate() { // Clear any related caches or transients delete_transient(ABMSENSE_PREFIX . 'activation'); // Optionally, you can add more cleanup code here if needed } add_action('admin_init', 'abmsense_complete_activation'); add_action('admin_init', 'abmsense_version_check'); register_deactivation_hook(__FILE__, 'abmsense_deactivate'); function abmsense_check_version_and_run_updates() { if (abmsense_version_check()) { abmsense_run_version_updates(); } } function abmsense_run_version_updates() { // Run any necessary updates for the new version // For example, you might need to update database schemas, clear caches, etc. $server_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; $report_email = get_option(ABMSENSE_PREFIX . 'report_email'); $report_frequency = get_option(ABMSENSE_PREFIX . 'report_frequency'); $is_consent = get_option(ABMSENSE_PREFIX . 'is_consent'); $date_registered = get_option(ABMSENSE_PREFIX . 'date_registered'); $version_installed = abmsense_get_version(); abmsense_save_data($server_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed); } // Add this new action hook add_action('admin_init', 'abmsense_check_version_and_run_updates');