| 1 |
<?php |
| 2 |
|
| 3 |
namespace Upress\EzCache; |
| 4 |
|
| 5 |
/** |
| 6 |
* One-click database cleanup, similar to WP Rocket's Database tab. |
| 7 |
* |
| 8 |
* Available cleanup tasks: |
| 9 |
* - Revisions, auto-drafts, trashed posts |
| 10 |
* - Spam and trashed comments |
| 11 |
* - Expired transients |
| 12 |
* - Optimize MyISAM/InnoDB tables (`OPTIMIZE TABLE`) |
| 13 |
* |
| 14 |
* Each task is opt-in via the corresponding setting flag and `clean()` returns |
| 15 |
* a per-task summary so the UI can show what was cleaned up. |
| 16 |
*/ |
| 17 |
class DatabaseOptimizer { |
| 18 |
|
| 19 |
/** |
| 20 |
* Run the cleanup tasks enabled by the user. |
| 21 |
* |
| 22 |
* @param array $tasks Optional explicit list of tasks; defaults to all enabled in settings. |
| 23 |
* @return array Per task counts. |
| 24 |
*/ |
| 25 |
public function clean( $tasks = null ) { |
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
$settings = Settings::get_settings(); |
| 29 |
$results = []; |
| 30 |
|
| 31 |
$enabled = function ( $key ) use ( $tasks, $settings ) { |
| 32 |
if ( is_array( $tasks ) ) { |
| 33 |
return in_array( $key, $tasks, true ); |
| 34 |
} |
| 35 |
return ! empty( $settings->{ $key } ); |
| 36 |
}; |
| 37 |
|
| 38 |
if ( $enabled( 'db_cleanup_revisions' ) ) { |
| 39 |
$results['revisions'] = (int) $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'revision'" ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( $enabled( 'db_cleanup_auto_drafts' ) ) { |
| 43 |
$results['auto_drafts'] = (int) $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_status = 'auto-draft'" ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( $enabled( 'db_cleanup_trashed_posts' ) ) { |
| 47 |
$results['trashed_posts'] = (int) $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_status = 'trash'" ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( $enabled( 'db_cleanup_spam_comments' ) ) { |
| 51 |
$results['spam_comments'] = (int) $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_approved = 'spam'" ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( $enabled( 'db_cleanup_trashed_comments' ) ) { |
| 55 |
$results['trashed_comments'] = (int) $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_approved = 'trash' OR comment_approved = 'post-trashed'" ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( $enabled( 'db_cleanup_expired_transients' ) ) { |
| 59 |
$now = time(); |
| 60 |
$results['expired_transients'] = (int) $wpdb->query( |
| 61 |
$wpdb->prepare( |
| 62 |
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b |
| 63 |
WHERE a.option_name LIKE %s |
| 64 |
AND a.option_name NOT LIKE %s |
| 65 |
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) |
| 66 |
AND b.option_value < %d", |
| 67 |
$wpdb->esc_like( '_transient_' ) . '%', |
| 68 |
$wpdb->esc_like( '_transient_timeout_' ) . '%', |
| 69 |
$now |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
if ( $enabled( 'db_cleanup_orphan_postmeta' ) ) { |
| 75 |
$results['orphan_postmeta'] = (int) $wpdb->query( "DELETE pm FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.ID IS NULL" ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( $enabled( 'db_optimize_tables' ) ) { |
| 79 |
$tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}%'" ); |
| 80 |
$optimized = 0; |
| 81 |
foreach ( $tables as $table ) { |
| 82 |
if ( false !== $wpdb->query( "OPTIMIZE TABLE `{$table}`" ) ) { |
| 83 |
$optimized++; |
| 84 |
} |
| 85 |
} |
| 86 |
$results['optimized_tables'] = $optimized; |
| 87 |
} |
| 88 |
|
| 89 |
// Clear post cache so updates are reflected immediately. |
| 90 |
Cache::instance()->clear_cache(); |
| 91 |
|
| 92 |
return $results; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Schedule a recurring cleanup based on the settings. |
| 97 |
*/ |
| 98 |
public function schedule() { |
| 99 |
$settings = Settings::get_settings(); |
| 100 |
|
| 101 |
$timestamp = wp_next_scheduled( 'ezcache_db_cleanup' ); |
| 102 |
if ( empty( $settings->db_cleanup_schedule ) || 'never' === $settings->db_cleanup_schedule ) { |
| 103 |
if ( $timestamp ) { |
| 104 |
wp_unschedule_event( $timestamp, 'ezcache_db_cleanup' ); |
| 105 |
} |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! $timestamp ) { |
| 110 |
wp_schedule_event( time() + DAY_IN_SECONDS, $settings->db_cleanup_schedule, 'ezcache_db_cleanup' ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|