Make WordPress Core


Ignore:
Timestamp:
10/18/2021 12:51:00 PM (5 years ago)
Author:
hellofromTonya
Message:

Cron: Fix malformed cron array in wp_schedule_single_event() when _get_cron_array() returns false.

In wp_schedule_single_event(), the cron info array is retrieved via a call to _get_cron_array() and straight away cast to an array. But as the documentation for that function (correctly) states, the return type of that function is array|false, where false is returned for a site where no cron jobs have been scheduled (yet).

In the case that _get_cron_array() would return false, this would now unintentionally create an array with a single entry with key 0 and as the value false.

This is a bug. Fixed now by adding validation to the output of _get_cron_array() and initializing $crons to an empty array if false was returned.

Tests added first to prove the bug (a) was introduced in #44818 [44917] and (b) is now fixed.

Follow-up to [44917].

Props jrf, peterwilsoncc.
Fixes #53950.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/cron.php

    r51657 r51916  
    101101        }
    102102
    103 
    104103        /**
    105104         * Tests that a call to wp_schedule_event() on a site without any scheduled events
     
    124123                // Add an event.
    125124                $this->assertTrue( wp_schedule_event( $timestamp, 'daily', $hook ) );
     125        }
     126
     127        /**
     128         * Tests that a call to wp_schedule_single_event() on a site without any scheduled events
     129         * does not result in the value "false" being added into the cron array.
     130         *
     131         * @ticket 53950
     132         *
     133         * @covers ::wp_schedule_single_event
     134         */
     135        function test_wp_schedule_single_event_without_cron_option() {
     136                delete_option( 'cron' );
     137
     138                // Verify that the cause of the error is in place.
     139                $this->assertFalse( _get_cron_array(), '_get_cron_array() does not return false' );
     140
     141                $hook      = __FUNCTION__;
     142                $timestamp = strtotime( '+10 minutes' );
     143
     144                // Add an event.
     145                $this->assertTrue( wp_schedule_single_event( $timestamp, $hook ), 'Scheduling single event failed' );
     146
     147                // Verify that "false" is not a value in the final cron array.
     148                $this->assertNotContains( false, get_option( 'cron' ), 'Resulting cron array contains the value "false"' );
    126149        }
    127150
Note: See TracChangeset for help on using the changeset viewer.