Created
February 9, 2015 17:08
-
-
Save sidharrell/6f799954297a96cb3ead to your computer and use it in GitHub Desktop.
Delete orphaned session 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
/** | |
* espresso_delete_orphaned_session_transients | |
* @param int $expired_session_transient_delete_query_limit | |
*/ | |
function espresso_delete_orphaned_session_transients( $expired_session_transient_delete_query_limit ) { | |
global $wpdb; | |
// DELETE ORPHANED SESSION TRANSIENTS | |
$SQL = " | |
SELECT option_name | |
FROM wp_options | |
WHERE option_name LIKE '%\_ee\_ssn\_%' | |
AND option_name NOT LIKE '%timeout%' | |
AND option_name NOT IN ( | |
SELECT t2.option_name | |
FROM wp_options t1 | |
JOIN wp_options t2 ON t2.option_name = REPLACE( t1.option_name, '_timeout', '' ) | |
WHERE ( t1.option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' ) | |
) | |
LIMIT 0, {$expired_session_transient_delete_query_limit} | |
"; | |
$possibly_orphaned_sessions = $wpdb->get_col( $SQL ); | |
// valid results? | |
if ( ! $possibly_orphaned_sessions instanceof WP_Error && ! empty( $possibly_orphaned_sessions )) { | |
// format array of results into something usable within the actual DELETE query's IN clause | |
$orphans = array(); | |
foreach( $possibly_orphaned_sessions as $possibly_orphaned_session ) { | |
$orphans[] = "'" . $possibly_orphaned_session . "'"; | |
} | |
$orphans = implode( ', ', $orphans ); | |
$SQL = " | |
DELETE | |
FROM {$wpdb->options} | |
WHERE option_name | |
IN ( $orphans ) | |
"; | |
$results = $wpdb->query( $SQL ); | |
// if something went wrong, then notify the admin | |
if ( $results instanceof WP_Error && is_admin() ) { | |
EE_Error::add_error( $results->get_error_message(), __FILE__, __FUNCTION__, __LINE__ ); | |
} | |
} | |
} | |
add_action( 'FHEE__EE_Session__garbage_collection___end', 'espresso_delete_orphaned_session_transients', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment