Make WordPress Core


Ignore:
Timestamp:
08/16/2021 03:22:38 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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: WordPress Developer Resources: _get_cron_array()

Props jrf, hellofromTonya, lucatume, pbearne, iluy, pedromendonca, SergeyBiryukov.
See #53635.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/cron.php

    r50394 r51619  
    303303
    304304    $crons = _get_cron_array();
     305    if ( ! is_array( $crons ) ) {
     306        $crons = array();
     307    }
     308
    305309    $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
    306310        'schedule' => $event->schedule,
     
    11261130
    11271131    $crons = _get_cron_array();
    1128 
    1129     if ( false === $crons ) {
     1132    if ( ! is_array( $crons ) ) {
    11301133        return array();
    11311134    }
Note: See TracChangeset for help on using the changeset viewer.