Skip to content

Instantly share code, notes, and snippets.

@sidharrell
Created February 9, 2015 17:10
Show Gist options
  • Save sidharrell/72762edc047fee9e4bed to your computer and use it in GitHub Desktop.
Save sidharrell/72762edc047fee9e4bed to your computer and use it in GitHub Desktop.
Delete Expired EE4 Session Transients plugin
<?php
/*
Plugin Name: Event Espresso - Delete Expired EE4 Session Transients
Plugin URI: http://www.eventespresso.com/
Description: Deletes Expired EE4 Session Transients. Requires version of Event Espresso 4 and greater.
Version: 0.0.1
Author: Event Espresso
Author URI: http://www.eventespresso.com
Copyright (c) 2014 Event Espresso All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* delete_expired_ee4_session_transients
*/
function delete_expired_ee4_session_transients() {
if ( ! defined( 'DOING_AJAX') || ! DOING_AJAX ) {
global $wpdb;
$expiration = current_time( 'timestamp' );
$SQL = "
SELECT option_name
FROM {$wpdb->options}
WHERE option_name
LIKE '\_transient\_timeout\_ee\_ssn\_%'
AND option_value < {$expiration}
LIMIT 2;
";
$expired_sessions = $wpdb->get_col( $SQL );
$IN = array();
foreach( $expired_sessions as $expired_session ) {
$IN[] = "'" . $expired_session . "'";
$IN[] = "'" . str_replace( '_transient_timeout_', '', $expired_session ) . "'";
}
$IN = implode( ', ', $IN );
if ( ! empty( $IN )) {
$SQL = "
DELETE FROM {$wpdb->options}
WHERE option_name
IN ( $IN );
";
$results = $wpdb->query( $SQL );
if ( $results ) {
echo 'deleted ' . count( $results ) . ' expired session transients<br />';
delete_expired_ee4_session_transients();
}
}
}
}
add_action( 'shutdown', 'delete_expired_ee4_session_transients', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment