Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Cron
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
6
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
 get_update_check_cron_job_name
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 get_immediate_update_check_cron_job_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_cron_job
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 handle_update_check_cron_job
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Refresh licence and product information daily.
4 *
5 * @package brianhenryie/bh-wp-plugin-updater
6 */
7
8namespace BrianHenryIE\WP_Plugin_Updater\WP_Includes;
9
10use BrianHenryIE\WP_Plugin_Updater\API_Interface;
11use BrianHenryIE\WP_Plugin_Updater\Settings_Interface;
12
13use function BrianHenryIE\WP_Plugin_Updater\str_dash_to_underscore;
14
15/**
16 * Manage actions related to wp-cron scheduled and background tasks.
17 */
18class Cron {
19
20    /**
21     * Constructor.
22     *
23     * @param API_Interface      $api The main plugin functions, which the cron job will call.
24     * @param Settings_Interface $settings The plugin settings. The slug is used for the cron job name.
25     */
26    public function __construct(
27        protected API_Interface $api,
28        protected Settings_Interface $settings,
29    ) {
30    }
31
32    /**
33     * Get the name of the cron job that will be scheduled.
34     *
35     * The WordPress convention, when searching for {@see wp_schedule_event()}, is to use cron job names with underscores.
36     *
37     * {plugin_slug}_update_check
38     */
39    public function get_update_check_cron_job_name(): string {
40        return str_dash_to_underscore(
41            sprintf(
42                '%s_%s',
43                $this->settings->get_plugin_slug(),
44                'update_check'
45            )
46        );
47    }
48
49    /**
50     * Get the name of the cron job that will be scheduled for an immediate update check.
51     *
52     * I think if the repeating one's name is used it might remove the schedule.
53     *
54     * {plugin_slug}_update_check_immediate
55     */
56    public function get_immediate_update_check_cron_job_name(): string {
57        return "{$this->get_update_check_cron_job_name()}_immediate";
58    }
59
60    /**
61     * When the plugin is activated, schedule a daily update check.
62     *
63     * @see activate_plugin()
64     * @hooked activate_{plugin_slug}
65     */
66    public function register_cron_job(): void {
67        if ( wp_next_scheduled( $this->get_update_check_cron_job_name() ) ) {
68            return;
69        }
70
71        wp_schedule_event(
72            time(),
73            'daily',
74            $this->get_update_check_cron_job_name()
75        );
76    }
77
78    /**
79     * Handle the cron job invocation – refresh the licence details and fetch the product information.
80     *
81     * @see get_update_check_cron_job_name()
82     * @hooked {plugin_slug}_update_check
83     */
84    public function handle_update_check_cron_job(): void {
85        $this->api->get_licence_details( true );
86        $this->api->get_plugin_information( true );
87        $this->api->get_check_update( true );
88    }
89}