Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dartiss committed Nov 12, 2017
1 parent d55d6ad commit 1df1bc4
Show file tree
Hide file tree
Showing 12 changed files with 896 additions and 0 deletions.
34 changes: 34 additions & 0 deletions artiss-transient-cleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
Plugin Name: Transient Cleaner
Plugin URI: https://wordpress.org/plugins/artiss-transient-cleaner/
Description: Housekeep expired transients from your options table.
Version: 1.5.3
Author: David Artiss
Author URI: https://artiss.blog
Text Domain: artiss-transient-cleaner
*/

/**
* Artiss Transient Cleaner
*
* Main code - include various functions
*
* @package Artiss-Transient-Cleaner
* @since 1.2
*/

$functions_dir = plugin_dir_path( __FILE__ ) . 'includes/';

// Include all the various functions

include_once( $functions_dir . 'clean-transients.php' ); // General configuration set-up

include_once( $functions_dir . 'shared-functions.php' ); // Assorted shared functions

if ( is_admin() ) {

include_once( $functions_dir . 'set-admin-config.php' ); // Administration configuration

}
?>
Binary file added assets/banner-1544×500.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/banner-772x250.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon-128x128.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon-256x256.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
203 changes: 203 additions & 0 deletions includes/clean-transients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php
/**
* Clean Transients
*
* Functions to clear down transient data
*
* @package Artiss-Transient-Cleaner
*/

/**
* Clean Expired Transients
*
* Hook into scheduled deletions and clear down expired transients
*
* @since 1.0
*
* @return string Number of transients removed
*/

function tc_clean_transients() {

$cleaned = 0;

// Only perform clean if enabled

$options = tc_get_options();

if ( $options[ 'clean_enable' ] ) { $cleaned = tc_transient_delete( false ); }

// Return number of cleaned transients

return $cleaned;
}

add_action( 'housekeep_transients', 'tc_clean_transients' );

/**
* Set housekeeping schedule
*
* Set up scheduler for housekeeping
*
* @since 1.4
*/

function tc_set_up_scheduler() {

// Check for conditions under which the scheduler requires settings up

if ( !wp_next_scheduled( 'housekeep_transients' ) && !wp_installing() ) { $schedule = true; } else { $schedule = false; }

// Set up schedule, if required

if ( $schedule ) {
$options = tc_get_options();
tc_set_schedule( $options[ 'schedule' ] );
}
}

add_action( 'init', 'tc_set_up_scheduler' );

/**
* Clear All Transients
*
* Hook into database upgrade and clear transients
*
* @since 1.0
*
* @return string Number of transients removed
*/

function tc_clear_transients() {

$cleared = 0;

// Only perform clear if enabled

$options = tc_get_options();

if ( $options[ 'upgrade_enable' ] ) { $cleared = tc_transient_delete( true ); }

// Return number of cleared transients

return $cleared;

}

add_action( 'after_db_upgrade', 'tc_clear_transients' );
add_action( 'clear_all_transients', 'tc_clear_transients' );

/**
* Delete Transients
*
* Shared function that will clear down requested transients
*
* @since 1.0
*
* @param string $expired TRUE or FALSE, whether to clear all transients or not
* @return string Number of removed transients
*/

function tc_transient_delete( $clear_all ) {

$cleaned = 0;

global $_wp_using_ext_object_cache;

if ( !$_wp_using_ext_object_cache ) {

$options = tc_get_options();

global $wpdb;
$records = 0;

// Build and execute required SQL

if ( $clear_all ) {

// Clean from options table

$sql = "DELETE FROM $wpdb->options WHERE option_name LIKE '%_transient_%'";
$clean = $wpdb -> query( $sql );
$records .= $clean;

// If multisite, and the main network, also clear the sitemeta table

if ( is_multisite() && is_main_network() ) {
$sql = "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '_site_transient_%'";
$clean = $wpdb -> query( $sql );
$records .= $clean;
}

} else {

// Delete transients from options table

$sql = "
DELETE
a, b
FROM
{$wpdb->options} a, {$wpdb->options} b
WHERE
a.option_name LIKE '%_transient_%' AND
a.option_name NOT LIKE '%_transient_timeout_%' AND
b.option_name = CONCAT(
'_transient_timeout_',
SUBSTRING(
a.option_name,
CHAR_LENGTH('_transient_') + 1
)
)
AND b.option_value < UNIX_TIMESTAMP()
";

$clean = $wpdb -> query( $sql );
$records .= $clean;

// Delete transients from multisite, if configured as such

if ( is_multisite() && is_main_network() ) {

$sql = "
DELETE
a, b
FROM
{$wpdb->sitemeta} a, {$wpdb->sitemeta} b
WHERE
a.meta_key LIKE '_site_transient_%' AND
a.meta_key NOT LIKE '_site_transient_timeout_%' AND
b.meta_key = CONCAT(
'_site_transient_timeout_',
SUBSTRING(
a.meta_key,
CHAR_LENGTH('_site_transient_') + 1
)
)
AND b.meta_value < UNIX_TIMESTAMP()
";

$clean = $wpdb -> query( $sql );
$records .= $clean;
}
}

// Save options field with number & timestamp

$results[ 'timestamp' ] = time() + ( get_option( 'gmt_offset' ) * 3600 );
$results[ 'records' ] = $records;

$option_name = 'transient_clean_';
if ( $clear_all ) { $option_name .= 'all'; } else { $option_name .= 'expired'; }
update_option( $option_name, $results );

// Optimize the table after the deletions

if ( ( ( $options[ 'upgrade_optimize' ] ) && ( $clear_all ) ) or ( ( $options[ 'clean_optimize' ] ) && ( !$clear_all ) ) ) {
$wpdb -> query( "OPTIMIZE TABLE $wpdb->options" );
if ( is_multisite() && is_main_network() ) { $wpdb -> query( "OPTIMIZE TABLE $wpdb->sitemeta" ); }
}
}

return $cleaned;
}
?>
Loading

0 comments on commit 1df1bc4

Please sign in to comment.