PluginProbe
Surge / 1.0.3
Surge v1.0.3
trunk 0.1.0 1.0.0 1.0.2 1.0.3 1.0.5 1.1.0 1.2.0 1.2.1
surge / include / cron.php

cron.php in Surge 1.0.3, at include/cron.php

85 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cron-related tasks for Surge.
4 *
5 * @package Surge
6 */
7
8 namespace Surge;
9
10 include_once( __DIR__ . '/common.php' );
11
12 // Runs in a CLI/Cron context, deletes expired cache entries.
13 add_action( 'surge_delete_expired', function() {
14 $cache_dir = CACHE_DIR;
15 $start = microtime( true );
16 $files = [];
17 $deleted = 0;
18 $time = time();
19
20 $levels = scandir( $cache_dir );
21 foreach ( $levels as $level ) {
22 if ( $level == '.' || $level == '..' ) {
23 continue;
24 }
25
26 if ( $level == 'flags.json.php' ) {
27 continue;
28 }
29
30 if ( ! is_dir( "{$cache_dir}/{$level}" ) ) {
31 continue;
32 }
33
34 $items = scandir( "{$cache_dir}/{$level}" );
35 foreach ( $items as $item ) {
36 if ( $item == '.' || $item == '..' ) {
37 continue;
38 }
39
40 if ( substr( $item, -4 ) != '.php' ) {
41 continue;
42 }
43
44 $files[] = "{$cache_dir}/{$level}/{$item}";
45 }
46 }
47
48 foreach ( $files as $filename ) {
49 $stat = stat( $filename );
50
51 // Skip files modified in the last minute.
52 if ( $stat['mtime'] + MINUTE_IN_SECONDS > $time ) {
53 continue;
54 }
55
56 // Empty file.
57 if ( $stat['size'] < 1 ) {
58 unlink( $filename );
59 $deleted++;
60 continue;
61 }
62
63 $f = fopen( $filename, 'rb' );
64 $meta = read_metadata( $f );
65 fclose( $f );
66
67 // This cache entry is still valid.
68 if ( $meta && ! empty( $meta['expires'] ) && $meta['expires'] > $time ) {
69 continue;
70 }
71
72 // Delete the cache entry
73 unlink( $filename );
74 $deleted++;
75 }
76
77 $end = microtime( true );
78 $elapsed = $end - $start;
79
80 if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( '\WP_CLI' ) ) {
81 \WP_CLI::success( sprintf( 'Deleted %d/%d files in %.4f seconds',
82 $deleted, count( $files ), $elapsed ) );
83 }
84 } );
85