Make WordPress Core

Ticket #35491: 35491-wp_next_scheduled.4.patch

File 35491-wp_next_scheduled.4.patch, 3.1 KB (added by jrf, 9 years ago)

Rebased against current master & upped version nr of change

  • src/wp-includes/cron.php

    From dcffe22a0a1ac9c6a36ba65f70348cabc57edcc1 Mon Sep 17 00:00:00 2001
    Date: Fri, 22 Apr 2016 15:04:29 +0200
    Subject: [PATCH] Updated @dhl's patch with my suggestion in comment #13 & rebased against current master
    
    ---
     src/wp-includes/cron.php     | 15 ++++++++++-----
     tests/phpunit/tests/cron.php | 26 ++++++++++++++++++++++++++
     2 files changed, 36 insertions(+), 5 deletions(-)
    
    diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php
    index 493e0ec..09ab5e2 100644
    a b 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.6.0 The `$ignore_args` parameter was added.
    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 * @param bool        $ignore_args Optional. Whether to check for any event scheduled on the action hook
     222 *                                 independently of arguments. Defaults to false.
    220223 * @return false|int The UNIX timestamp of the next time the scheduled event will occur.
    221224 */
    222 function wp_next_scheduled( $hook, $args = array() ) {
     225function wp_next_scheduled( $hook, $args = array(), $ignore_args = false ) {
    223226        $crons = _get_cron_array();
    224227        $key = md5(serialize($args));
    225         if ( empty($crons) )
     228        if ( empty($crons) ) {
    226229                return false;
     230        }
    227231        foreach ( $crons as $timestamp => $cron ) {
    228                 if ( isset( $cron[$hook][$key] ) )
     232                if ( ( true === $ignore_args && isset( $cron[ $hook ] ) ) || isset( $cron[ $hook ][ $key ] ) ) {
    229233                        return $timestamp;
     234                }
    230235        }
    231236        return false;
    232237}
  • tests/phpunit/tests/cron.php

    diff --git a/tests/phpunit/tests/cron.php b/tests/phpunit/tests/cron.php
    index b411a5b..59a16c7 100644
    a b 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, null, true ) );
     97
     98                wp_schedule_event( $ts1, 'hourly', $hook, array( 'foo' ) );
     99                wp_schedule_event( $ts2, 'daily', $hook, array( 'bar' ) );
     100                wp_schedule_event( $ts2, 'twicedaily', $hook, array( 'baz' ) );
     101
     102                $this->assertSame( $ts1, wp_next_scheduled( $hook, null, true ) );
     103
     104                wp_unschedule_event( $ts1, $hook, array( 'foo' ) );
     105                $this->assertSame( $ts2, wp_next_scheduled( $hook, null, true ) );
     106
     107                wp_unschedule_event( $ts2, $hook, array( 'bar' ) );
     108                $this->assertSame( $ts2, wp_next_scheduled( $hook, null, true  ) );
     109
     110                wp_unschedule_event( $ts2, $hook, array( 'baz' ) );
     111                $this->assertFalse( wp_next_scheduled( $hook, null, true ) );
     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();