Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucasstark/57923dd7f22a7e2f9114b7b911111131 to your computer and use it in GitHub Desktop.
Save lucasstark/57923dd7f22a7e2f9114b7b911111131 to your computer and use it in GitHub Desktop.
<?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')
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment