| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class Powered_Cache_Cron { |
| 7 |
|
| 8 |
/** |
| 9 |
* Placeholder constructer |
| 10 |
*/ |
| 11 |
public function __construct() { } |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* Setup actions and filters |
| 16 |
* |
| 17 |
* @since 1.0 |
| 18 |
*/ |
| 19 |
private function setup() { |
| 20 |
add_action( 'powered_cache_purge_cache', array( $this, 'purge_cache' ) ); |
| 21 |
add_action( 'init', array( $this, 'schedule_events' ) ); |
| 22 |
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add custom cron schedule |
| 27 |
* |
| 28 |
* @param array $schedules |
| 29 |
* |
| 30 |
* @since 1.0 |
| 31 |
* @return array $schedules |
| 32 |
*/ |
| 33 |
public function cron_schedules( $schedules ) { |
| 34 |
$interval = powered_cache_get_option( 'cache_timeout' ) * 60; |
| 35 |
|
| 36 |
$schedules['powered_cache'] = array( |
| 37 |
'interval' => apply_filters( 'powered_cache_cache_purge_interval', $interval ), |
| 38 |
'display' => esc_html__( 'Powered Cache Purge Interval', 'powered-cache' ), |
| 39 |
); |
| 40 |
|
| 41 |
return $schedules; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Unschedule events |
| 46 |
* |
| 47 |
* @since 1.0 |
| 48 |
*/ |
| 49 |
public function unschedule_events() { |
| 50 |
$timestamp = wp_next_scheduled( 'powered_cache_purge_cache' ); |
| 51 |
|
| 52 |
wp_unschedule_event( $timestamp, 'powered_cache_purge_cache' ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Setup cron jobs |
| 57 |
* |
| 58 |
* @since 1.0 |
| 59 |
*/ |
| 60 |
public function schedule_events() { |
| 61 |
|
| 62 |
$timestamp = wp_next_scheduled( 'powered_cache_purge_cache' ); |
| 63 |
|
| 64 |
// we don't need when page cache off |
| 65 |
if ( true !== powered_cache_get_option( 'enable_page_caching' ) ) { |
| 66 |
wp_unschedule_event( $timestamp, 'powered_cache_purge_cache' ); |
| 67 |
|
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// Expire cache never |
| 72 |
if ( intval( powered_cache_get_option( 'cache_timeout' ) ) === 0 ) { |
| 73 |
wp_unschedule_event( $timestamp, 'powered_cache_purge_cache' ); |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! $timestamp ) { |
| 79 |
wp_schedule_event( time(), 'powered_cache', 'powered_cache_purge_cache' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Initiate a cache purge |
| 85 |
* |
| 86 |
* @since 1.0 |
| 87 |
*/ |
| 88 |
public function purge_cache() { |
| 89 |
// Do nothing, caching is turned off |
| 90 |
if ( true !== powered_cache_get_option( 'enable_page_caching' ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$lifespan = powered_cache_get_option( 'cache_timeout' ) * 60; |
| 95 |
|
| 96 |
$expired_files = powered_cache_get_exprired_files( powered_cache_site_cache_dir(), $lifespan ); |
| 97 |
|
| 98 |
foreach ( $expired_files as $file_path ) { |
| 99 |
@unlink( $file_path ); |
| 100 |
} |
| 101 |
|
| 102 |
do_action( 'powered_cache_expired_files_deleted' ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Return an instance of the current class, create one if it doesn't exist |
| 107 |
* |
| 108 |
* @since 1.0 |
| 109 |
* @return object |
| 110 |
*/ |
| 111 |
public static function factory() { |
| 112 |
|
| 113 |
static $instance; |
| 114 |
|
| 115 |
if ( ! $instance ) { |
| 116 |
$instance = new self(); |
| 117 |
$instance->setup(); |
| 118 |
} |
| 119 |
|
| 120 |
return $instance; |
| 121 |
} |
| 122 |
} |
| 123 |
|