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