Notifications
2 months ago
Settings
1 month ago
Admin.php
1 month ago
CLI.php
2 weeks ago
Config.php
1 year ago
ConflictingPlugins.php
10 months ago
Connect.php
2 weeks ago
Cron.php
1 year ago
Invalidations.php
2 months ago
NitroPack.php
2 weeks ago
Settings.php
4 months ago
Cron.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Cron Class |
| 5 | * |
| 6 | * @package nitropack |
| 7 | */ |
| 8 | |
| 9 | namespace NitroPack\WordPress; |
| 10 | |
| 11 | /** |
| 12 | * Cron class for sheduling events. |
| 13 | */ |
| 14 | class Cron { |
| 15 | /** |
| 16 | * Init class. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | add_action( 'nitropack_remove_old_logs', [ $this, 'remove_old_logs' ] ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Schedule events. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public static function schedule_events() { |
| 28 | |
| 29 | if ( ! wp_next_scheduled( 'nitropack_remove_old_logs' ) ) { |
| 30 | wp_schedule_event( time(), 'daily', 'nitropack_remove_old_logs' ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Unschedule events when plugin is deactivated. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public static function unschedule_events() { |
| 40 | $timestamp = wp_next_scheduled( 'nitropack_remove_old_logs' ); |
| 41 | wp_unschedule_event( $timestamp, 'nitropack_remove_old_logs' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Remove old logs .csv and the .zip archive. |
| 46 | * Default to 14 days. |
| 47 | * Can be filtered with the 'nitropack_remove_old_logs_interval' filter by seconds. |
| 48 | * @return void |
| 49 | */ |
| 50 | public function remove_old_logs() { |
| 51 | $files = glob( NITROPACK_LOGS_DATA_DIR . '/*.{csv,zip}', GLOB_BRACE ); |
| 52 | if ( ! $files ) { |
| 53 | return; |
| 54 | } |
| 55 | $now = time(); |
| 56 | $days = 14; |
| 57 | $seconds = $days * 24 * 60 * 60; |
| 58 | |
| 59 | $seconds = apply_filters( 'nitropack_remove_old_logs_interval', $seconds ); |
| 60 | |
| 61 | foreach ( $files as $file ) { |
| 62 | if ( is_file( $file ) ) { |
| 63 | if ( $now - filemtime( $file ) >= $seconds ) { |
| 64 | unlink( $file ); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | } |
| 71 |