From c9df4f75a22f9ffd14753fdc84919d6e48dad6d9 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 16 Aug 2021 15:22:38 +0000 Subject: [PATCH] Code Modernization: Check the return type of `_get_cron_array()` in `wp_schedule_event()`. This fixes a "Deprecated: Automatic conversion of false to array is deprecated" warning on PHP 8.1. In `wp_schedule_event()`, the cron info array is retrieved via a call to `_get_cron_array()`, but as the documentation (correctly) states, the return type of that function is `array|false`, where `false` is returned for a virgin site, with no cron jobs scheduled yet. However, no type check is done on the return value, and the `wp_schedule_event()` function just blindly continues by assigning a value to a subkey of the `$crons` "array". Fixed by adding validation for the returned value from `_get_cron_array()` and initializing an empty array if `false` was returned. Reference: [https://developer.wordpress.org/reference/functions/_get_cron_array/ WordPress Developer Resources: _get_cron_array()] Props jrf, hellofromTonya, lucatume, pbearne, iluy, pedromendonca, SergeyBiryukov. See #53635. git-svn-id: https://develop.svn.wordpress.org/trunk@51619 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/cron.php | 7 +++++-- tests/phpunit/tests/cron.php | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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() {