Make WordPress Core

Changeset 53791


Ignore:
Timestamp:
07/29/2022 03:32:58 AM (2 years ago)
Author:
peterwilsoncc
Message:

Cron API: Modify _get_cron_array() to always return an array.

Change the return type of _get_cron_array() to an empty array if the cron option is either missing or of an
unexpected type.

This change ensures the return value for no registered events is consistently an empty array. Previously the return
value could be either an empty array or false.

Props thakkarhardik, jrf, costdev.
Fixes #53940.

Location:
trunk
Files:
1 added
3 edited

Legend:

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

    r52236 r53791  
    120120     */
    121121    $crons = _get_cron_array();
    122     if ( ! is_array( $crons ) ) {
    123         $crons = array();
    124     }
    125122
    126123    $key       = md5( serialize( $event->args ) );
     
    307304
    308305    $crons = _get_cron_array();
    309     if ( ! is_array( $crons ) ) {
    310         $crons = array();
    311     }
    312306
    313307    $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
     
    11341128
    11351129    $crons = _get_cron_array();
    1136     if ( ! is_array( $crons ) ) {
    1137         return array();
    1138     }
    11391130
    11401131    $gmt_time = microtime( true );
     
    11631154 *
    11641155 * @since 2.1.0
     1156 * @since 6.1.0 Return type modified to consistenty return an array.
    11651157 * @access private
    11661158 *
    1167  * @return array[]|false Array of cron info arrays on success, false on failure.
     1159 * @return array[] Array of cron events.
    11681160 */
    11691161function _get_cron_array() {
    11701162    $cron = get_option( 'cron' );
    11711163    if ( ! is_array( $cron ) ) {
    1172         return false;
     1164        return array();
    11731165    }
    11741166
  • trunk/tests/phpunit/tests/admin/wpMediaListTable.php

    r51639 r53791  
    2525     * @ticket 53949
    2626     * @covers WP_Media_List_Table::prepare_items
     27     * @group cron
    2728     */
    2829    public function test_prepare_items_without_cron_option_does_not_throw_warning() {
     
    4344
    4445        // Verify that the cause of the error is in place.
    45         $this->assertFalse( _get_cron_array(), '_get_cron_array() does not return false' );
     46        $this->assertIsArray( _get_cron_array(), '_get_cron_array() does not return an array.' );
     47        $this->assertEmpty( _get_cron_array(), '_get_cron_array() does not return an empty array.' );
    4648
    4749        // If this test does not error out due to the PHP warning, we're good.
  • trunk/tests/phpunit/tests/cron.php

    r53573 r53791  
    131131
    132132        // Verify that the cause of the error is in place.
    133         $this->assertFalse( _get_cron_array(), '_get_cron_array() does not return false' );
     133        $this->assertIsArray( _get_cron_array(), '_get_cron_array() does not return an array.' );
     134        $this->assertEmpty( _get_cron_array(), '_get_cron_array() does not return an empty array.' );
    134135
    135136        $hook      = __FUNCTION__;
     
    152153
    153154        // Verify that the cause of the error is in place.
    154         $this->assertFalse( _get_cron_array(), '_get_cron_array() does not return false' );
     155        $this->assertIsArray( _get_cron_array(), '_get_cron_array() does not return an array.' );
     156        $this->assertEmpty( _get_cron_array(), '_get_cron_array() does not return an empty array.' );
    155157
    156158        $hook      = __FUNCTION__;
Note: See TracChangeset for help on using the changeset viewer.