Skip to content

Instantly share code, notes, and snippets.

@brianleejackson
Last active September 20, 2023 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianleejackson/ee569a239860da5c7eaa3099db6473cb to your computer and use it in GitHub Desktop.
Save brianleejackson/ee569a239860da5c7eaa3099db6473cb to your computer and use it in GitHub Desktop.
<?php
add_action( 'shutdown', function() {
// If the event is scheduled, don't run anything.
if ( wp_next_scheduled( 'prefix_remove_license_activation_logs' ) ) {
return;
}
if ( ! function_exists( 'edd_has_upgrade_completed' ) ) {
require_once EDD_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
}
if ( edd_has_upgrade_completed( 'prefix_remove_license_activation_logs' ) ) {
return;
}
// schedule a one time event to begin removing the license activation logs.
wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'prefix_remove_license_activation_logs' );
} );
add_action( 'prefix_remove_license_activation_logs', 'prefix_remove_license_activation_logs' );
/**
* Removes the license activation logs.
*/
function prefix_remove_license_activation_logs() {
// If the upgrade is already complete, don't run it again.
if ( edd_has_upgrade_completed( 'prefix_remove_license_activation_logs' ) ) {
return;
}
// If the event is still scheduled, don't run it again.
if ( wp_next_scheduled( 'prefix_remove_license_activation_logs' ) ) {
return;
}
// If the upgrade function doesn't exist, require the file that has it.
if ( ! function_exists( 'edd_set_upgrade_complete' ) ) {
require_once EDD_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
}
global $wpdb;
$query = $wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_type = %s AND ( post_title LIKE %s OR post_title LIKE %s )",
'edd_license_log',
'%' . __( 'License Activated', 'edd_sl' ) . '%',
'%' . __( 'License Deactivated', 'edd_sl' ) . '%'
);
// Check for one log. If it doesn't exist, the upgrade is complete.
$logs = $wpdb->get_results( $query . " LIMIT 1" );
if ( empty( $logs ) ) {
edd_set_upgrade_complete( 'prefix_remove_license_activation_logs' );
}
// Get the first 100 logs and delete them.
$logs = $wpdb->get_results( $query . " LIMIT 100" );
foreach ( $logs as $log ) {
$wpdb->delete( $wpdb->posts, array( 'ID' => $log->ID ) );
$wpdb->delete( $wpdb->postmeta, array( 'post_id' => $log->ID ) );
}
// Schedule another event to remove more logs.
wp_schedule_single_event( time() + ( MINUTE_IN_SECONDS * 10 ), 'prefix_remove_license_activation_logs' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment