| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions related to schedules. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Crontrol\Schedule; |
| 7 |
|
| 8 |
use Crontrol\Exception\DuplicateScheduleException; |
| 9 |
use Crontrol\Schedule\Schedule; |
| 10 |
|
| 11 |
/** |
| 12 |
* Adds a new custom cron schedule. |
| 13 |
* |
| 14 |
* @throws DuplicateScheduleException If the schedule name already exists. |
| 15 |
* |
| 16 |
* @param string $name The internal name of the schedule. |
| 17 |
* @param int $interval The interval between executions of the new schedule. |
| 18 |
* @param string $display The display name of the schedule. |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
function add( $name, $interval, $display ) { |
| 22 |
$schedules = get(); |
| 23 |
|
| 24 |
if ( array_key_exists( $name, $schedules ) ) { |
| 25 |
throw new DuplicateScheduleException( |
| 26 |
sprintf( |
| 27 |
/* translators: %s: The internal name of the schedule. */ |
| 28 |
__( 'A schedule with the name "%s" already exists.', 'wp-crontrol' ), |
| 29 |
$name |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** @var array<string,int|string> */ |
| 35 |
$old_scheds = get_option( 'crontrol_schedules', array() ); |
| 36 |
|
| 37 |
$old_scheds[ $name ] = array( |
| 38 |
'interval' => $interval, |
| 39 |
'display' => $display, |
| 40 |
); |
| 41 |
update_option( 'crontrol_schedules', $old_scheds ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Fires after a new cron schedule is added. |
| 45 |
* |
| 46 |
* @param string $name The internal name of the schedule. |
| 47 |
* @param int $interval The interval between executions of the new schedule. |
| 48 |
* @param string $display The display name of the schedule. |
| 49 |
*/ |
| 50 |
do_action( 'crontrol/added_new_schedule', $name, $interval, $display ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Deletes a custom cron schedule. |
| 55 |
* |
| 56 |
* @param string $name The internal name of the schedule to delete. |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
function delete( $name ) { |
| 60 |
/** @var array<string,int|string> */ |
| 61 |
$scheds = get_option( 'crontrol_schedules', array() ); |
| 62 |
unset( $scheds[ $name ] ); |
| 63 |
update_option( 'crontrol_schedules', $scheds ); |
| 64 |
|
| 65 |
/** |
| 66 |
* Fires after a cron schedule is deleted. |
| 67 |
* |
| 68 |
* @param string $name The internal name of the schedule. |
| 69 |
*/ |
| 70 |
do_action( 'crontrol/deleted_schedule', $name ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Gets a sorted (according to interval) list of the cron schedules |
| 75 |
* |
| 76 |
* @return array<string,Schedule> Array of Schedule objects keyed by schedule name. |
| 77 |
*/ |
| 78 |
function get() { |
| 79 |
/** |
| 80 |
* @phpstan-var array<string,array{ |
| 81 |
* interval: int, |
| 82 |
* display?: string, |
| 83 |
* }> $schedules |
| 84 |
*/ |
| 85 |
$schedules = wp_get_schedules(); |
| 86 |
uasort( |
| 87 |
$schedules, |
| 88 |
fn( array $a, array $b ) => $a['interval'] <=> $b['interval'] |
| 89 |
); |
| 90 |
|
| 91 |
$result = []; |
| 92 |
foreach ( $schedules as $name => $schedule ) { |
| 93 |
$display = $schedule['display'] ?? $name; |
| 94 |
$result[ $name ] = Schedule::create( $name, $schedule['interval'], $display ); |
| 95 |
} |
| 96 |
|
| 97 |
return $result; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Displays a dropdown filled with the possible schedules, including non-repeating. |
| 102 |
* |
| 103 |
* @param ?string $current The currently selected schedule, or null for none. |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
function dropdown( ?string $current = null ) { |
| 107 |
$schedules = get(); |
| 108 |
?> |
| 109 |
<select class="postform" name="crontrol_schedule" id="crontrol_schedule" required> |
| 110 |
<option <?php selected( $current, '_oneoff' ); ?> value="_oneoff"><?php esc_html_e( 'Non-repeating', 'wp-crontrol' ); ?></option> |
| 111 |
<?php foreach ( $schedules as $schedule ) { ?> |
| 112 |
<option <?php selected( $current, $schedule->name ); ?> value="<?php echo esc_attr( $schedule->name ); ?>"> |
| 113 |
<?php |
| 114 |
printf( |
| 115 |
'%s (%s)', |
| 116 |
esc_html( $schedule->display ), |
| 117 |
esc_html( $schedule->name ) |
| 118 |
); |
| 119 |
?> |
| 120 |
</option> |
| 121 |
<?php } ?> |
| 122 |
</select> |
| 123 |
<?php |
| 124 |
} |
| 125 |
|