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
This commit is contained in:
Sergey Biryukov 2021-08-16 15:22:38 +00:00
parent 8905ffc319
commit c9df4f75a2
2 changed files with 29 additions and 2 deletions

View File

@ -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();
}

View File

@ -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() {