Last active
November 20, 2021 15:28
-
-
Save kjbenk/168624095b9ad162e04e to your computer and use it in GitHub Desktop.
WordPress - Purge All Expired Transients
This file contains 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 characters
<?php | |
// Schedule the CRON Job to purge all expired transients | |
if (!wp_next_scheduled('purge_popup_transients_cron')) { | |
/** | |
* wp_schedule_event | |
* | |
* @param - When to start the CRON job | |
* @param - The interval in for each subsequent run | |
* @param - The name of the CRON JOB | |
*/ | |
wp_schedule_event( time(), 'daily', 'purge_popup_transients_cron'); | |
} | |
add_action( 'purg_transients_cron', 'purge_transients', 10 ,2); | |
/** | |
* Deletes all transients that have expired | |
* | |
* @access public | |
* @static | |
* @return void | |
*/ | |
static function purge_popup_transients($older_than = '1 day', $safemode = true) { | |
global $wpdb; | |
$older_than_time = strtotime('-' . $older_than); | |
/** | |
* Only check if the transients are older than the specified time | |
*/ | |
if ( $older_than_time > time() || $older_than_time < 1 ) { | |
return false; | |
} | |
/** | |
* Get all the expired transients | |
* | |
* @var mixed | |
* @access public | |
*/ | |
$transients = $wpdb->get_col( | |
$wpdb->prepare( " | |
SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name | |
FROM {$wpdb->options} | |
WHERE option_name LIKE '\_transient\_timeout\__%%' | |
AND option_value < %s | |
", $older_than_time) | |
); | |
/** | |
* If safemode is ON just use the default WordPress get_transient() function | |
* to delete the expired transients | |
*/ | |
if ( $safemode ) { | |
foreach( $transients as $transient ) { | |
get_transient($transient); | |
} | |
} | |
/** | |
* If safemode is OFF the just manually delete all the transient rows in the database | |
*/ | |
else { | |
$options_names = array(); | |
foreach($transients as $transient) { | |
$options_names[] = '_transient_' . $transient; | |
$options_names[] = '_transient_timeout_' . $transient; | |
} | |
if ($options_names) { | |
$options_names = array_map(array($wpdb, 'escape'), $options_names); | |
$options_names = "'". implode("','", $options_names) ."'"; | |
$result = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name IN ({$options_names})" ); | |
if (!$result) { | |
return false; | |
} | |
} | |
} | |
return $transients; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`<?php
// Schedule the CRON Job to purge all expired transients
if (!wp_next_scheduled('purge_popup_transients_cron')) {
/**
* wp_schedule_event
*
* @param - When to start the CRON job
* @param - The interval in for each subsequent run
* @param - The name of the CRON JOB
/
wp_schedule_event( time(), 'daily', 'purge_popup_transients_cron');
}
add_action( 'purge_popup_transients_cron', 'purge_transients', 10 ,2);
/*
/
function purge_transients($older_than = '1 day', $safemode = true) {
global $wpdb;
$older_than_time = strtotime('-' . $older_than);
/*
/
if ( $older_than_time > time() || $older_than_time < 1 ) {
return false;
}
/*
/
$transients = $wpdb->get_col(
$wpdb->prepare( "
SELECT REPLACE(option_name, 'transient_timeout', '') AS transient_name
FROM {$wpdb->options}
WHERE option_name LIKE '_transient_timeout__%%'
AND option_value < %s
", $older_than_time)
);
/*
/
if ( $safemode ) {
foreach( $transients as $transient ) {
get_transient($transient);
}
}
/*
*/
else {
$options_names = array();
foreach($transients as $transient) {
$options_names[] = 'transient' . $transient;
$options_names[] = 'transient_timeout' . $transient;
}
if ($options_names) {
$options_names = array_map(array($wpdb, 'escape'), $options_names);
$options_names = "'". implode("','", $options_names) ."'";
$result = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name IN ({$options_names})" );
if (!$result) {
return false;
}
}
}
return $transients;
}`