Skip to content

Commit c6efb67

Browse files
Add interval check to wp_schedule_event() and differentiate errors in wp_reschedule_event()
Co-authored-by: John Blackbourn <[email protected]>
1 parent cda5935 commit c6efb67

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/wp-includes/cron.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp
298298
return false;
299299
}
300300

301+
if ( ! is_numeric( $event->interval ) || $event->interval <= 0 ) {
302+
if ( $wp_error ) {
303+
return new WP_Error(
304+
'invalid_interval',
305+
__( 'Event schedule has invalid interval.' )
306+
);
307+
}
308+
309+
return false;
310+
}
311+
301312
$key = md5( serialize( $event->args ) );
302313

303314
$crons = _get_cron_array();
@@ -419,15 +430,17 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $
419430
// Now we assume something is wrong and fail to schedule.
420431
if ( ! is_numeric( $interval ) || $interval <= 0 ) {
421432
if ( $wp_error ) {
422-
return new WP_Error(
423-
'invalid_schedule',
424-
sprintf(
425-
/* translators: 1: the event schedule recurrence, 2: the interval encoded as JSON */
426-
__( 'Event schedule with recurrence "%1$s" does not exist or has is invalid. Interval must be positive integer, but got: %2$s' ),
427-
$recurrence,
428-
wp_json_encode( $interval )
429-
)
430-
);
433+
if ( ! isset( $schedules[ $recurrence ] ) ) {
434+
return new WP_Error(
435+
'invalid_schedule',
436+
__( 'Event schedule does not exist.' )
437+
);
438+
} else {
439+
return new WP_Error(
440+
'invalid_interval',
441+
__( 'Event schedule has invalid interval.' )
442+
);
443+
}
431444
}
432445

433446
return false;

tests/phpunit/tests/cron.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ public function test_invalid_timestamp_for_event_returns_error() {
847847

848848
/**
849849
* @ticket 49961
850+
* @ticket 64404
850851
*
851852
* @covers ::wp_schedule_event
852853
* @covers ::wp_reschedule_event
@@ -862,6 +863,33 @@ public function test_invalid_recurrence_for_event_returns_error() {
862863
$this->assertSame( 'invalid_schedule', $rescheduled_event->get_error_code() );
863864
}
864865

866+
/**
867+
* @ticket 64404
868+
*
869+
* @covers ::wp_schedule_event
870+
* @covers ::wp_reschedule_event
871+
*/
872+
public function test_invalid_schedule_interval_returns_error() {
873+
add_filter(
874+
'cron_schedules',
875+
static function ( $schedules ) {
876+
$schedules['backwards_in_time'] = array(
877+
'interval' => -3600,
878+
'display' => 'Back to the future!',
879+
);
880+
return $schedules;
881+
}
882+
);
883+
884+
$event = wp_schedule_event( time(), 'backwards_in_time', 'hook', array(), true );
885+
$this->assertWPError( $event );
886+
$this->assertSame( 'invalid_interval', $event->get_error_code() );
887+
888+
$rescheduled_event = wp_reschedule_event( time(), 'backwards_in_time', 'hook', array(), true );
889+
$this->assertWPError( $rescheduled_event );
890+
$this->assertSame( 'invalid_interval', $rescheduled_event->get_error_code() );
891+
}
892+
865893
/**
866894
* @ticket 49961
867895
*

0 commit comments

Comments
 (0)