WordPress.org

Make WordPress Core

Ticket #35491: wp_next_scheduled.diff

File wp_next_scheduled.diff, 2.4 KB (added by swissspidy, 4 years ago)
  • src/wp-includes/cron.php

    diff --git src/wp-includes/cron.php src/wp-includes/cron.php
    index 493e0ec..3028afe 100644
    function wp_clear_scheduled_hook( $hook, $args = array() ) { 
    214214 * Retrieve the next timestamp for a cron event.
    215215 *
    216216 * @since 2.1.0
     217 * @since 4.5.0 The `$args` parameter can now be `false`.
    217218 *
    218  * @param string $hook Action hook to execute when cron is run.
    219  * @param array $args Optional. Arguments to pass to the hook's callback function.
     219 * @param string      $hook Action hook to execute when cron is run.
     220 * @param array|false $args Optional. Arguments to pass to the hook's callback function.
     221 *                          Pass false for any arguments. Default empty.
    220222 * @return false|int The UNIX timestamp of the next time the scheduled event will occur.
    221223 */
    222224function wp_next_scheduled( $hook, $args = array() ) {
    function wp_next_scheduled( $hook, $args = array() ) { 
    225227        if ( empty($crons) )
    226228                return false;
    227229        foreach ( $crons as $timestamp => $cron ) {
    228                 if ( isset( $cron[$hook][$key] ) )
     230                if ( false === $args && isset( $cron[ $hook ] ) || isset( $cron[$hook][$key] ) )
    229231                        return $timestamp;
    230232        }
    231233        return false;
  • tests/phpunit/tests/cron.php

    diff --git tests/phpunit/tests/cron.php tests/phpunit/tests/cron.php
    index b411a5b..892b92d 100644
    class Tests_Cron extends WP_UnitTestCase { 
    8585
    8686        }
    8787
     88        /**
     89         * @ticket 35491
     90         */
     91        function test_wp_next_scheduled_no_args() {
     92                $hook = rand_str();
     93                $ts1 = strtotime( '+30 minutes' );
     94                $ts2 = strtotime( '+2 hours' );
     95
     96                $this->assertFalse( wp_next_scheduled( $hook, false ) );
     97
     98                wp_schedule_event( $ts1, 'hourly', $hook, array( 'foo' ) );
     99                wp_schedule_event( $ts2, 'daily', $hook, 'bar' );
     100                wp_schedule_event( $ts2, 'twicedaily', $hook, array( 'baz' ) );
     101
     102                $this->assertSame( $ts1, wp_next_scheduled( $hook, false ) );
     103
     104                wp_unschedule_event( $ts1, $hook, array( 'foo' ) );
     105                $this->assertSame( $ts2, wp_next_scheduled( $hook, false ) );
     106
     107                wp_unschedule_event( $ts2, $hook, 'bar' );
     108                $this->assertSame( $ts2, wp_next_scheduled( $hook, false  ) );
     109
     110                wp_unschedule_event( $ts2, $hook, array( 'baz' ) );
     111                $this->assertFalse( wp_next_scheduled( $hook, false ) );
     112        }
     113
    88114        function test_unschedule_event() {
    89115                // schedule an event and make sure it's returned by wp_next_scheduled
    90116                $hook = rand_str();