Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/wp-includes/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
// Make sure timestamp is a positive integer.
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
if ( ! is_numeric( $timestamp ) || $timestamp < 0 ) {
if ( $wp_error ) {
return new WP_Error(
'invalid_timestamp',
Expand Down Expand Up @@ -233,7 +233,7 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error
*/
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
// Make sure timestamp is a positive integer.
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
if ( ! is_numeric( $timestamp ) || $timestamp < 0 ) {
if ( $wp_error ) {
return new WP_Error(
'invalid_timestamp',
Expand Down Expand Up @@ -339,7 +339,7 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp
*/
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
// Make sure timestamp is a positive integer.
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
if ( ! is_numeric( $timestamp ) || $timestamp < 0 ) {
if ( $wp_error ) {
return new WP_Error(
'invalid_timestamp',
Expand Down Expand Up @@ -461,7 +461,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $
*/
function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
// Make sure timestamp is a positive integer.
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
if ( ! is_numeric( $timestamp ) || $timestamp < 0 ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be the same code block applied on wp_schedule_single_event() as well?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp_schedule_event + wp_reschedule_event too since all of them uses same validation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this. I’ve updated wp_schedule_single_event accordingly, and also applied the same changes to wp_schedule_event and wp_reschedule_event as they contained similar code.

if ( $wp_error ) {
return new WP_Error(
'invalid_timestamp',
Expand Down Expand Up @@ -784,7 +784,7 @@ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {

$key = md5( serialize( $args ) );

if ( ! $timestamp ) {
if ( null === $timestamp ) {
// Get next event.
$next = false;
foreach ( $crons as $timestamp => $cron ) {
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/tests/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,41 @@ public function test_get_scheduled_event_recurring() {
$this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
}

/**
* Ensure wp_get_scheduled_event() can retrieve events scheduled at timestamp 0.
*
* @ticket 63987
*
* @covers ::wp_get_scheduled_event
*/
public function test_get_scheduled_event_timestamp_zero() {
$hook = __FUNCTION__;
$args = array( 'test_arg' );

// Manually add an event at timestamp 0 to the cron array.
$crons = _get_cron_array();
if ( ! $crons ) {
$crons = array();
}

$key = md5( serialize( $args ) );
$crons[0][ $hook ][ $key ] = array(
'schedule' => false,
'args' => $args,
);
_set_cron_array( $crons );

// Test that wp_get_scheduled_event() can retrieve the event at timestamp 0.
$event = wp_get_scheduled_event( $hook, $args, 0 );
$this->assertIsObject( $event, 'wp_get_scheduled_event() should return an object for timestamp 0 event' );
$this->assertSame( $hook, $event->hook );
$this->assertSame( 0, $event->timestamp );

// Test that wp_unschedule_event() can remove the event at timestamp 0.
$unscheduled = wp_unschedule_event( 0, $hook, $args );
$this->assertTrue( $unscheduled );
}

/**
* Ensure wp_get_scheduled_event() returns false when expected.
*
Expand Down
Loading