Skip to content

Commit

Permalink
Merge pull request #51 from dartiss/develop
Browse files Browse the repository at this point in the history
Version 1.7
  • Loading branch information
dartiss authored Jun 9, 2024
2 parents 82b68f1 + e4bbd5c commit 88b9357
Show file tree
Hide file tree
Showing 18 changed files with 1,050 additions and 784 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Transient Cleaner

<img src="https://ps.w.org/artiss-transient-cleaner/assets/icon-128x128.png" align="left">Remove expired transients from your options table. The original and best!
<img src="https://ps.w.org/artiss-transient-cleaner/assets/icon.svg" width=128px align="left" style="padding: 0 20px 20px 0;">Remove expired transients from your options table. The original and best!

**This plugin is designed only for WordPress 5.8 or below, as transient cleaning is part of core functionality after that point.**

Expand Down
24 changes: 14 additions & 10 deletions artiss-transient-cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* Transient Cleaner
*
* @package Artiss-Transient-Cleaner
* @package artiss-transient-cleaner
* @author David Artiss
* @license GPL-2.0-or-later
*
* Plugin Name: Transient Cleaner
* Plugin URI: https://wordpress.org/plugins/artiss-transient-cleaner/
* Description: 🧼 Clear expired transients from your options table.
* Version: 1.6
* Description: Clear expired transients from your options table.
* Version: 1.7
* Requires at least: 4.4
* Requires PHP: 7.4
* Author: David Artiss
Expand All @@ -26,22 +26,26 @@
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

// Exit if accessed directly.

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

// Define global to hold the plugin base file name.

if ( ! defined( 'TRANSIENT_CLEANER_PLUGIN_BASE' ) ) {
define( 'TRANSIENT_CLEANER_PLUGIN_BASE', plugin_basename( __FILE__ ) );
}

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

// Include all the various functions.

require_once $functions_dir . 'clean-transients.php'; // General configuration set-up.
require_once plugin_dir_path( __FILE__ ) . 'inc/scheduler.php';

require_once $functions_dir . 'shared-functions.php'; // Assorted shared functions.
require_once plugin_dir_path( __FILE__ ) . 'inc/clean-transients.php';

if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'inc/settings.php';

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

}
require_once plugin_dir_path( __FILE__ ) . 'inc/shared.php';
30 changes: 30 additions & 0 deletions assets/blueprints/blueprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"landingPage": "\/wp-admin\/tools.php?page=transient-options",
"preferredVersions": {
"php": "8.0",
"wp": "latest"
},
"phpExtensionBundles": [
"kitchen-sink"
],
"features": {
"networking": true
},
"steps": [
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "url",
"url": "https:\/\/downloads.wordpress.org\/plugin\/artiss-transient-cleaner.zip"
},
"options": {
"activate": true
}
},
{
"step": "login",
"username": "admin",
"password": "password"
}
]
}
Binary file modified assets/icon-128x128.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 modified assets/icon-256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icon.svg
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.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 removed assets/screenshot-1.png
Binary file not shown.
128 changes: 128 additions & 0 deletions inc/clean-transients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Clean Transients
*
* Functions to clear down transient data
*
* @package artiss-transient-cleaner
*/

// Exit if accessed directly.

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Delete Transients
*
* Shared function that will clear down requested transients
*
* @param string $clear_all TRUE or FALSE, whether to clear all transients or not.
* @return string Number of removed transients
*/
function transient_cleaner_transient_delete( $clear_all ) {

$cleaned = 0;

global $_wp_using_ext_object_cache;

if ( ! $_wp_using_ext_object_cache ) {

$options = transient_cleaner_get_options();

global $wpdb;
$records = 0;

// Build and execute required SQL.

if ( $clear_all ) {

// Clean from options table.

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

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

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

// Delete transients from options table.

$clean = $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, CHAR_LENGTH('_transient_') + 1 ) )
AND b.option_value < %d",
$wpdb->esc_like( '_transient_' ) . '%',
$wpdb->esc_like( '_transient_timeout_' ) . '%',
time()
)
);
$records .= $clean;

// Delete transients from multisite, if configured as such.

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

$clean = $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
WHERE a.meta_key LIKE %s
AND a.meta_key NOT LIKE %s
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, CHAR_LENGTH('_site_transient_') + 1 ) )
AND b.meta_value < %d",
$wpdb->esc_like( '_site_transient_' ) . '%',
$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
time()
)
);
$records .= $clean;
}
}

// Save options field with number & timestamp.

$results = array();

$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 ) ) || ( ( $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;
}
119 changes: 119 additions & 0 deletions inc/scheduler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Scheduler
*
* Set up scheduler and hooks for performing the cleaning process.
*
* @package artiss-transient-cleaner
*/

// Exit if accessed directly.

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Clean Expired Transients
*
* Hook into scheduled deletions and clear down expired transients
*
* @return string Number of transients removed
*/
function transient_cleaner_clean_transients() {

$cleaned = 0;

// Only perform clean if enabled.

$options = transient_cleaner_get_options();

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

// Return number of cleaned transients.

return $cleaned;
}

add_action( 'housekeep_transients', 'transient_cleaner_clean_transients' );

/**
* Set cleaning schedule
*
* Set up scheduler for transient cleaning
*/
function transient_cleaner_set_up_scheduler() {

global $_wp_using_ext_object_cache;

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

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

// Set up schedule, if required.

if ( $schedule ) {
$options = transient_cleaner_get_options();
transient_cleaner_set_schedule( $options['schedule'] );
}
}

add_action( 'init', 'transient_cleaner_set_up_scheduler' );

/**
* Set scheduler
*
* Set up scheduler.
*
* @param string $hour The hour to be scheduled.
* @return string The hour parameter that was used
*/
function transient_cleaner_set_schedule( $hour ) {

// If the hour to be set is before now, setting it will cause the schedule to run immediately
// Therefore, in this case it's set to that time for tomorrow.

$hour .= ':00';
if ( $hour <= gmdate( 'H' ) ) {
$hour .= ' tomorrow';
}

// Now create the scheduled event.

wp_schedule_event( strtotime( $hour ), 'daily', 'housekeep_transients' );

return $hour;
}

/**
* Clear All Transients
*
* Hook into database upgrade and clear transients
*
* @return string Number of transients removed
*/
function transient_cleaner_clear_transients() {

$cleared = 0;

// Only perform clear if enabled.

$options = transient_cleaner_get_options();

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

// Return number of cleared transients.

return $cleared;
}

add_action( 'after_db_upgrade', 'transient_cleaner_clear_transients' );
add_action( 'clear_all_transients', 'transient_cleaner_clear_transients' );
Loading

0 comments on commit 88b9357

Please sign in to comment.