Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Cron
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_delete_logs_cron_job
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 delete_old_logs
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Automatically delete old log files.
4 *
5 * @package brianhenryie/bh-wp-logger
6 */
7
8namespace BrianHenryIE\WP_Logger\WP_Includes;
9
10use BrianHenryIE\WC_Logger\WC_PSR_Logger;
11use BrianHenryIE\WP_Logger\API_Interface;
12use BrianHenryIE\WP_Logger\API\BH_WP_PSR_Logger;
13use BrianHenryIE\WP_Logger\Logger_Settings_Interface;
14use Psr\Log\LoggerAwareTrait;
15
16/**
17 * Functions to register the cron job and handle the action to execute the job to delete old logs.
18 */
19class Cron {
20
21    use LoggerAwareTrait;
22
23    /**
24     * The logger settings are used to determine which plugin we're working with.
25     *
26     * @see Logger_Settings_Interface::get_plugin_slug()
27     * @var Logger_Settings_Interface
28     */
29    protected Logger_Settings_Interface $settings;
30
31    /**
32     * The API instance will delete the old logs.
33     *
34     * @see API_Interface::delete_old_logs()
35     * @var API_Interface
36     */
37    protected API_Interface $api;
38
39    /**
40     * Cron constructor.
41     *
42     * @param API_Interface             $api The logger's main functions.
43     * @param Logger_Settings_Interface $settings The logger settings.
44     * @param BH_WP_PSR_Logger          $logger The logger itself for logging.
45     */
46    public function __construct( $api, $settings, BH_WP_PSR_Logger $logger ) {
47
48        $this->setLogger( $logger );
49        $this->settings = $settings;
50        $this->api      = $api;
51    }
52
53    /**
54     * Schedule a daily cron job to delete old logs, just after midnight.
55     *
56     * Does not schedule the cleanup if it is a WooCommerce logger (since WooCommerce handles that itself).
57     *
58     * @hooked init
59     */
60    public function register_delete_logs_cron_job(): void {
61
62        /**
63         * Cast the logger to the logger facade so we can access the true logger itself.
64         *
65         * @var BH_WP_PSR_Logger $bh_wp_psr_logger
66         */
67        $bh_wp_psr_logger = $this->logger;
68        $logger           = $bh_wp_psr_logger->get_logger();
69
70        if ( $logger instanceof WC_PSR_Logger ) {
71            return;
72        }
73
74        $cron_hook = "delete_logs_{$this->settings->get_plugin_slug()}";
75
76        if ( false !== wp_get_scheduled_event( $cron_hook ) ) {
77            return;
78        }
79
80        wp_schedule_event( strtotime( 'tomorrow' ), 'daily', $cron_hook );
81
82        $this->logger->debug( "Registered the `{$cron_hook}` cron job." );
83    }
84
85    /**
86     * Handle the cron job.
87     *
88     * @hooked delete_logs_{plugin-slug}
89     */
90    public function delete_old_logs(): void {
91        $action = current_action();
92        $this->logger->debug( "Executing {$action} cron job." );
93
94        $this->api->delete_old_logs();
95    }
96}