| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cron based functionalities |
| 4 |
* |
| 5 |
* @package PoweredCache |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache; |
| 9 |
|
| 10 |
use const PoweredCache\Constants\PURGE_CACHE_CRON_NAME; |
| 11 |
use const PoweredCache\Constants\PURGE_FO_CRON_NAME; |
| 12 |
use function PoweredCache\Utils\get_expired_files; |
| 13 |
use function PoweredCache\Utils\is_dir_empty; |
| 14 |
use function PoweredCache\Utils\site_cache_dir; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Cron |
| 22 |
*/ |
| 23 |
class Cron { |
| 24 |
|
| 25 |
/** |
| 26 |
* Placeholder constructor |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Setup actions and filters |
| 34 |
* |
| 35 |
* @since 1.0 |
| 36 |
*/ |
| 37 |
private function setup() { |
| 38 |
add_action( PURGE_CACHE_CRON_NAME, array( $this, 'purge_expired_page_cache' ) ); |
| 39 |
add_action( PURGE_FO_CRON_NAME, array( $this, 'purge_expired_minify_files' ) ); |
| 40 |
add_action( 'init', array( $this, 'schedule_page_cache_events' ) ); |
| 41 |
add_action( 'init', array( $this, 'schedule_fo_cache_events' ) ); |
| 42 |
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add custom cron schedule |
| 47 |
* |
| 48 |
* @param array $schedules registered cron schedules |
| 49 |
* |
| 50 |
* @return array $schedules |
| 51 |
* @since 1.0 |
| 52 |
*/ |
| 53 |
public function cron_schedules( $schedules ) { |
| 54 |
$settings = \PoweredCache\Utils\get_settings(); |
| 55 |
|
| 56 |
$interval = $settings['cache_timeout'] * 60; // in seconds |
| 57 |
|
| 58 |
$schedules['powered_cache'] = [ |
| 59 |
/** |
| 60 |
* Filters page cache purge interval |
| 61 |
* |
| 62 |
* @hook powered_cache_cache_purge_interval |
| 63 |
* |
| 64 |
* @param {int} TTL in seconds. |
| 65 |
* |
| 66 |
* @return {int} New value. |
| 67 |
* @since 2.0 |
| 68 |
*/ |
| 69 |
'interval' => apply_filters( 'powered_cache_cache_purge_interval', $interval ), |
| 70 |
'display' => esc_html__( 'Powered Cache Purge Interval', 'powered-cache' ), |
| 71 |
]; |
| 72 |
|
| 73 |
return $schedules; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Unschedule events |
| 78 |
* |
| 79 |
* @since 1.0 |
| 80 |
*/ |
| 81 |
public function unschedule_events() { |
| 82 |
$timestamp = wp_next_scheduled( PURGE_CACHE_CRON_NAME ); |
| 83 |
wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME ); |
| 84 |
|
| 85 |
$timestamp = wp_next_scheduled( PURGE_FO_CRON_NAME ); |
| 86 |
wp_unschedule_event( $timestamp, PURGE_FO_CRON_NAME ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Setup cron jobs |
| 91 |
* |
| 92 |
* @since 1.0 |
| 93 |
*/ |
| 94 |
public function schedule_page_cache_events() { |
| 95 |
$settings = \PoweredCache\Utils\get_settings(); |
| 96 |
|
| 97 |
$timestamp = wp_next_scheduled( PURGE_CACHE_CRON_NAME ); |
| 98 |
|
| 99 |
// we don't need when page cache off |
| 100 |
if ( true !== $settings['enable_page_cache'] ) { |
| 101 |
wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME ); |
| 102 |
|
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
// Expire cache never |
| 107 |
if ( intval( $settings['cache_timeout'] ) === 0 ) { |
| 108 |
wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME ); |
| 109 |
|
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! $timestamp ) { |
| 114 |
/** |
| 115 |
* Only use `powered_cache` interval when TTL under the an hour |
| 116 |
*/ |
| 117 |
if ( intval( $settings['cache_timeout'] ) < 60 ) { |
| 118 |
return wp_schedule_event( time(), 'powered_cache', PURGE_CACHE_CRON_NAME ); |
| 119 |
} |
| 120 |
|
| 121 |
return wp_schedule_event( time(), 'hourly', PURGE_CACHE_CRON_NAME ); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Schedule a cron event keep only most recent min. files in cache |
| 129 |
* |
| 130 |
* @return bool|void|\WP_Error |
| 131 |
*/ |
| 132 |
public function schedule_fo_cache_events() { |
| 133 |
$timestamp = wp_next_scheduled( PURGE_FO_CRON_NAME ); |
| 134 |
|
| 135 |
if ( ! file_exists( POWERED_CACHE_FO_CACHE_DIR ) || is_dir_empty( POWERED_CACHE_FO_CACHE_DIR ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! $timestamp ) { |
| 140 |
return wp_schedule_event( time(), 'hourly', PURGE_FO_CRON_NAME ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Initiate a cache purge |
| 146 |
* |
| 147 |
* @since 1.0 |
| 148 |
*/ |
| 149 |
public function purge_expired_page_cache() { |
| 150 |
$settings = \PoweredCache\Utils\get_settings(); |
| 151 |
|
| 152 |
// Do nothing, caching is turned off |
| 153 |
if ( true !== $settings['enable_page_cache'] ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$lifespan = $settings['cache_timeout'] * 60; // TTL in seconds |
| 158 |
|
| 159 |
$expired_files = get_expired_files( site_cache_dir(), $lifespan ); |
| 160 |
|
| 161 |
foreach ( $expired_files as $file_path ) { |
| 162 |
\PoweredCache\Utils\log( sprintf( 'Cleanup via cron: %s', $file_path ) ); |
| 163 |
@unlink( $file_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 164 |
} |
| 165 |
|
| 166 |
// cleanup dirs.. |
| 167 |
foreach ( $expired_files as $file_path ) { |
| 168 |
$dir = dirname( $file_path ); |
| 169 |
if ( file_exists( $dir ) && is_dir_empty( $dir ) ) { |
| 170 |
@rmdir( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Fire when expired files deleted |
| 176 |
* |
| 177 |
* @since 2.0 added $expired_files $arg |
| 178 |
* @since 1.0 |
| 179 |
*/ |
| 180 |
do_action( 'powered_cache_expired_files_deleted', $expired_files ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Remove minify files |
| 185 |
*/ |
| 186 |
public function purge_expired_minify_files() { |
| 187 |
/** |
| 188 |
* Filters default TTL for minified assets. |
| 189 |
* |
| 190 |
* @hook powered_cache_fo_cache_ttl |
| 191 |
* |
| 192 |
* @param {int} TTL in seconds. |
| 193 |
* |
| 194 |
* @return {int} New value. |
| 195 |
* @since 2.0 |
| 196 |
*/ |
| 197 |
$lifespan = apply_filters( 'powered_cache_fo_cache_ttl', 60 * 60 ); // in seconds |
| 198 |
$expired_files = get_expired_files( POWERED_CACHE_FO_CACHE_DIR, $lifespan ); |
| 199 |
foreach ( $expired_files as $file_path ) { |
| 200 |
\PoweredCache\Utils\log( sprintf( 'Cleanup via cron: %s', $file_path ) ); |
| 201 |
@unlink( $file_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Return an instance of the current class, create one if it doesn't exist |
| 208 |
* |
| 209 |
* @return object |
| 210 |
* @since 1.0 |
| 211 |
*/ |
| 212 |
public static function factory() { |
| 213 |
|
| 214 |
static $instance; |
| 215 |
|
| 216 |
if ( ! $instance ) { |
| 217 |
$instance = new self(); |
| 218 |
$instance->setup(); |
| 219 |
} |
| 220 |
|
| 221 |
return $instance; |
| 222 |
} |
| 223 |
} |
| 224 |
|