Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Cron | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| register_hooks | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| update | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| add_weekly_schedule | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\EventManager; |
| 6 | use NewfoldLabs\WP\Module\Data\Helpers\Plugin; |
| 7 | |
| 8 | /** |
| 9 | * Schedules Cron event listeners |
| 10 | */ |
| 11 | class Cron extends Listener { |
| 12 | |
| 13 | /** |
| 14 | * Register all required hooks for the listener category |
| 15 | * |
| 16 | * @see Listener::register_hooks() |
| 17 | * @see EventManager::initialize_listeners() |
| 18 | */ |
| 19 | public function register_hooks(): void { |
| 20 | |
| 21 | // Ensure there is a weekly option in the cron schedules |
| 22 | // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected |
| 23 | add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); |
| 24 | |
| 25 | // Weekly cron hook |
| 26 | add_action( 'nfd_data_cron', array( $this, 'update' ) ); |
| 27 | |
| 28 | // Register the cron task |
| 29 | if ( ! wp_next_scheduled( 'nfd_data_cron' ) ) { |
| 30 | wp_schedule_event( |
| 31 | time() + constant( 'DAY_IN_SECONDS' ), |
| 32 | 'weekly', |
| 33 | 'nfd_data_cron' |
| 34 | ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Cron event |
| 40 | * |
| 41 | * @hooked nfd_data_cron |
| 42 | * @see Cron::register_hooks() |
| 43 | */ |
| 44 | public function update(): void { |
| 45 | $data = array( |
| 46 | 'plugins' => ( new Plugin() )->collect_installed(), |
| 47 | ); |
| 48 | |
| 49 | $data = apply_filters( 'newfold_wp_data_module_cron_data_filter', $data ); |
| 50 | |
| 51 | $this->push( 'cron', $data ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add the weekly option to cron schedules if it doesn't exist |
| 56 | * |
| 57 | * @hooked cron_schedules |
| 58 | * @see wp_get_schedules() |
| 59 | * |
| 60 | * @param array<string, array{interval:int, display:string}> $schedules List of cron schedule options |
| 61 | * @return array<string, array{interval:int, display:string}> |
| 62 | */ |
| 63 | public function add_weekly_schedule( $schedules ): array { |
| 64 | if ( ! array_key_exists( 'weekly', $schedules ) || constant( 'WEEK_IN_SECONDS' ) !== $schedules['weekly']['interval'] ) { |
| 65 | $schedules['weekly'] = array( |
| 66 | 'interval' => constant( 'WEEK_IN_SECONDS' ), |
| 67 | 'display' => __( 'Once Weekly' ), |
| 68 | ); |
| 69 | } |
| 70 | return $schedules; |
| 71 | } |
| 72 | } |