diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php index 1666a09b27..18fe14196e 100644 --- a/src/wp-includes/cron.php +++ b/src/wp-includes/cron.php @@ -302,6 +302,10 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp $key = md5( serialize( $event->args ) ); $crons = _get_cron_array(); + if ( ! is_array( $crons ) ) { + $crons = array(); + } + $crons[ $event->timestamp ][ $event->hook ][ $key ] = array( 'schedule' => $event->schedule, 'args' => $event->args, @@ -1125,8 +1129,7 @@ function wp_get_ready_cron_jobs() { } $crons = _get_cron_array(); - - if ( false === $crons ) { + if ( ! is_array( $crons ) ) { return array(); } diff --git a/tests/phpunit/tests/cron.php b/tests/phpunit/tests/cron.php index e0f973d51e..9b9c475954 100644 --- a/tests/phpunit/tests/cron.php +++ b/tests/phpunit/tests/cron.php @@ -98,7 +98,31 @@ class Tests_Cron extends WP_UnitTestCase { $this->assertFalse( wp_next_scheduled( $hook, array( 'bar' ) ) ); $this->assertSame( $recur, wp_get_schedule( $hook, $args ) ); + } + + /** + * Tests that a call to wp_schedule_event() on a site without any scheduled events + * does not result in a PHP deprecation warning on PHP 8.1 or higher. + * + * The warning that we should not see: + * `Deprecated: Automatic conversion of false to array is deprecated`. + * + * @ticket 53635 + * + * @covers ::wp_schedule_event + */ + function test_wp_schedule_event_without_cron_option_does_not_throw_warning() { + delete_option( 'cron' ); + + // Verify that the cause of the error is in place. + $this->assertFalse( _get_cron_array(), '_get_cron_array() does not return false' ); + + $hook = __FUNCTION__; + $timestamp = strtotime( '+10 minutes' ); + + // Add an event. + $this->assertTrue( wp_schedule_event( $timestamp, 'daily', $hook ) ); } function test_unschedule_event() {