Created
June 28, 2025 10:24
-
-
Save lucasstark/57923dd7f22a7e2f9114b7b911111131 to your computer and use it in GitHub Desktop.
Revisions
-
lucasstark created this gist
Jun 28, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ <?php /** * Plugin Name: Recommendation Engine Session Cleanup * Description: Deletes session history entries older than 30 days from the woocommerce_session_activity table. * Version: 1.0 * Author: Element Stark */ // Schedule daily cleanup task on plugin activation register_activation_hook(__FILE__, function () { if (!wp_next_scheduled('recommender_cleanup_old_sessions')) { wp_schedule_event(time(), 'daily', 'recommender_cleanup_old_sessions'); } }); // Clear scheduled task on plugin deactivation register_deactivation_hook(__FILE__, function () { wp_clear_scheduled_hook('recommender_cleanup_old_sessions'); }); // Perform the cleanup add_action('recommender_cleanup_old_sessions', function () { global $wpdb; $table_name = $wpdb->prefix . 'woocommerce_session_activity'; // Check if the table exists first if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) { return; } // Delete rows older than 30 days $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE session_time < %d", strtotime('-30 days') ) ); });