[wp-trac] [WordPress Trac] #63987: wp_get_scheduled_event() incorrectly returns false for cron events scheduled with a timestamp of 0.
WordPress Trac
noreply at wordpress.org
Wed Mar 25 10:27:40 UTC 2026
#63987: wp_get_scheduled_event() incorrectly returns false for cron events
scheduled with a timestamp of 0.
---------------------------------------+-----------------------
Reporter: codekraft | Owner: (none)
Type: defect (bug) | Status: reopened
Priority: normal | Milestone:
Component: Cron API | Version:
Severity: normal | Resolution:
Keywords: needs-patch has-test-info | Focuses:
---------------------------------------+-----------------------
Comment (by liaison):
Test Report for Ticket #63987 (PR #9914)
Environment:
OS: Windows 10
Server: XAMPP
PHP: 8.2.x
WordPress: 7.0-RC2-src (trunk)
Testing Process:
I used a standalone script to mock a cron array with an event at timestamp
0 and tested the wp_get_scheduled_event() function. This report also
validates edge cases related to PHP's float-to-int truncation for array
keys.
Results:
Pre-patch: The function returned false for timestamp 0. This is due to
PHP's loose typing where ! 0 evaluates to true, causing the function to
incorrectly trigger the fallback logic (finding the next event) instead of
retrieving the event at index 0.
Post-patch: After changing the check to if ( null === $timestamp ), the
function correctly identified and returned the event object at timestamp
0.
Edge Case (Float 0.5): Confirmed that even if a float is passed (which PHP
truncates to integer 0 as an array key), the patched code successfully
retrieves the event.
Regression (NULL): Confirmed that passing null still correctly triggers
the fallback behavior, ensuring zero breaking changes for existing core
logic.
Conclusion:
The fix is verified and works as expected on Windows/PHP 8.2. It correctly
distinguishes between an explicit 0 timestamp and a null (missing) value.
+1 for commit.
Reproducible Test Script (verify-63987.php):
{{{#!php
<?php
/**
* Test script for Ticket #63987 / PR #9914
* Verifies that wp_get_scheduled_event() correctly handles timestamp 0.
*/
// Basic WP environment mocks
define( 'ABSPATH', __DIR__ . '/' );
define( 'WPINC', 'wp-includes' );
function apply_filters( $tag, $value ) { return $value; }
function get_option( $option, $default = false ) {
return isset($GLOBALS['mock_options'][$option]) ?
$GLOBALS['mock_options'][$option] : $default;
}
if ( ! defined( 'WP_CRON_LOCK_TIMEOUT' ) ) define( 'WP_CRON_LOCK_TIMEOUT',
60 );
// Load the file under test
require_once ABSPATH . WPINC . '/cron.php';
// Mock a cron event at timestamp 0
$hook = 'test_event';
$args = array( 'key' => 'val' );
$sig = md5( serialize( $args ) );
$GLOBALS['mock_options']['cron'] = array(
0 => array( $hook => array( $sig => array( 'schedule' => 'hourly',
'args' => $args ) ) ),
'version' => 2
);
echo "Testing wp_get_scheduled_event with timestamp 0..." . PHP_EOL;
$event = wp_get_scheduled_event( $hook, $args, 0 );
if ( is_object( $event ) ) {
echo "RESULT: [PASS] - Event found at timestamp 0." . PHP_EOL;
} else {
echo "RESULT: [FAIL] - Event NOT found (Bug reproduced)." . PHP_EOL;
}
/**
* SCENARIO A: Float Truncation (The 0.5 Case)
* PHP truncates array key 0.5 to integer 0.
*/
echo "Testing Scenario A: Float 0.5 (truncated to 0 internally)..." .
PHP_EOL;
$event_float = wp_get_scheduled_event( $hook, $args, 0.5 ); // Passing 0.5
if ( is_object( $event_float ) ) {
echo "✅ [SUCCESS] Corrected code found the event even when passed as
float 0.5." . PHP_EOL;
} else {
echo "❌ [FAIL] Could not find event with float 0.5." . PHP_EOL;
}
/**
* SCENARIO B: Regression Test for NULL
*/
echo "Testing Scenario B: NULL value (Should maintain legacy fallback)..."
. PHP_EOL;
$event_null = wp_get_scheduled_event( $hook, $args, null );
if ( false === $event_null ) {
echo "✅ [SUCCESS] NULL correctly triggers fallback (returned false as
expected)." . PHP_EOL;
} else {
echo "❌ [FAIL] NULL behavior changed! Possible regression." .
PHP_EOL;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63987#comment:24>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list