Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Garbage_Collection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 setup
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 schedule_single_garbage_collection
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 garbage_collect
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 activate
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 deactivate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache;
4
5use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache;
6use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Logger;
7
8class Garbage_Collection {
9    const ACTION = 'jetpack_boost_cache_garbage_collection';
10
11    /**
12     * Register hooks.
13     */
14    public static function setup() {
15        add_action( self::ACTION, array( self::class, 'garbage_collect' ) );
16
17        // Clear old log files when garbage collection is run. Do not pass the $older_than parameter to the method as it's not supported.
18        add_action( self::ACTION, array( Logger::class, 'delete_old_logs' ), 10, 0 );
19    }
20
21    public static function schedule_single_garbage_collection() {
22        $older_than = time();
23        wp_schedule_single_event( $older_than, self::ACTION, array( 'older_than' => $older_than ) );
24    }
25
26    /**
27     * Garbage collect expired files.
28     *
29     * @param int|null $older_than The timestamp before which files should be deleted. If not provided, the files older than default cache duration will be deleted.
30     */
31    public static function garbage_collect( $older_than = null ) {
32        $cache_ttl = JETPACK_BOOST_CACHE_DURATION;
33
34        /*
35         * If an $older_than value is specified, use it to calculate the cache TTL.
36         * By specifying $older_than, you can instruct garbage collection to apply on files created before a certain point in time.
37         * This ensures garbage collection is not clearing files that were created after the request was made. Useful to avoid race conditions.
38         */
39        if ( $older_than !== null ) {
40            $cache_ttl = time() - $older_than;
41        }
42
43        $cache = new Boost_Cache();
44        $cache->get_storage()->garbage_collect( $cache_ttl );
45    }
46
47    /**
48     * Setup the garbage collection cron job.
49     */
50    public static function activate() {
51        self::setup();
52
53        if ( ! wp_next_scheduled( self::ACTION ) ) {
54            wp_schedule_event( time(), 'hourly', self::ACTION );
55        }
56    }
57
58    /**
59     * Remove the garbage collection cron job.
60     */
61    public static function deactivate() {
62        wp_clear_scheduled_hook( self::ACTION );
63    }
64}