| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the scheduling of cron jobs within the WordPress environment. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Scheduler |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Scheduler; |
| 9 |
|
| 10 |
/** |
| 11 |
* Service layer to handle cron schedule. |
| 12 |
*/ |
| 13 |
class Scheduler { |
| 14 |
|
| 15 |
/** |
| 16 |
* Filter array cron schedules with matched cron keys. |
| 17 |
* |
| 18 |
* @param array $cron_keys Array of cron keys to fetch respective cron schedules. |
| 19 |
* |
| 20 |
* @return array Array of cron schedules. |
| 21 |
*/ |
| 22 |
public function filter_cron_schedules( array $cron_keys ): array { |
| 23 |
return array_filter( |
| 24 |
wp_get_schedules(), |
| 25 |
function ( $v, $k ) use ( $cron_keys ): bool { |
| 26 |
return in_array( |
| 27 |
$k, |
| 28 |
$cron_keys, |
| 29 |
true |
| 30 |
); |
| 31 |
}, |
| 32 |
ARRAY_FILTER_USE_BOTH |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Override existing schedule with new schedule. |
| 38 |
* |
| 39 |
* @param string $hook Schedule hook tag to execute/trigger. |
| 40 |
* @param string $recurrence Schedule name. |
| 41 |
*/ |
| 42 |
public function override_schedule( string $hook, string $recurrence ): void { |
| 43 |
$timestamp = (int) wp_next_scheduled( $hook ); |
| 44 |
|
| 45 |
wp_clear_scheduled_hook( $hook ); |
| 46 |
|
| 47 |
wp_reschedule_event( $timestamp, $recurrence, $hook ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get cron schedule interval. |
| 52 |
* |
| 53 |
* @param string $schedule Schedule name. |
| 54 |
* |
| 55 |
* @since 5.1.1 |
| 56 |
* @return int|bool |
| 57 |
*/ |
| 58 |
public function get_cron_schedule_interval( $schedule ) { |
| 59 |
$schedules = wp_get_schedules(); |
| 60 |
return isset( $schedules[ $schedule ] ) ? $schedules[ $schedule ]['interval'] : false; |
| 61 |
} |
| 62 |
} |
| 63 |
|