Make WordPress Core

Changeset 29939


Ignore:
Timestamp:
10/17/2014 07:16:26 PM (10 years ago)
Author:
wonderboymusic
Message:

wp_schedule_single_event() should not prevent scheduling a future duplicate event. It should only reject an event as a duplicate if there is already a similar event scheduled within 10 minutes of the given timestamp.

Adds unit tests, fixes existing cron test.

Props tellyworth.

See [9181], #6966.
Fixes #28213.

Location:
trunk
Files:
2 edited

Legend:

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

    r29732 r29939  
    2121 */
    2222function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
    23     // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
     23    // don't schedule a duplicate if there's already an identical event due within 10 minutes of it
    2424    $next = wp_next_scheduled($hook, $args);
    25     if ( $next && $next <= $timestamp + 10 * MINUTE_IN_SECONDS )
    26         return;
     25    if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
     26        return;
     27    }
    2728
    2829    $crons = _get_cron_array();
  • trunk/tests/phpunit/tests/cron.php

    r25368 r29939  
    215215        // duplicate events far apart should work normally
    216216        $hook = rand_str();
    217         $args = array(rand_str());
    218         $ts1 = strtotime('+30 minutes');
    219         $ts2 = strtotime('+3 minutes');
     217        $args = array( rand_str() );
     218        $ts1 = strtotime( '+30 minutes' );
     219        $ts2 = strtotime( '+3 minutes' );
    220220
    221221        // first one works
     
    224224        wp_schedule_single_event( $ts2, $hook, $args );
    225225
    226         // the next event should be at +5 minutes, not +3
    227         $this->assertEquals( $ts2, wp_next_scheduled($hook, $args) );
     226        // the next event should be at +3 minutes, even though that one was scheduled second
     227        $this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
    228228        wp_unschedule_event( $ts2, $hook, $args );
     229        // following event at +30 minutes should be there too
     230        $this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
     231    }
     232
     233    function test_not_duplicate_event_reversed() {
     234        // duplicate events far apart should work normally regardless of order
     235        $hook = rand_str();
     236        $args = array( rand_str() );
     237        $ts1 = strtotime( '+3 minutes' );
     238        $ts2 = strtotime( '+30 minutes' );
     239
     240        // first one works
     241        wp_schedule_single_event( $ts1, $hook, $args );
     242        // second works too
     243        wp_schedule_single_event( $ts2, $hook, $args );
     244
     245        // the next event should be at +3 minutes
     246        $this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
     247        wp_unschedule_event( $ts1, $hook, $args );
    229248        // following event should be there too
    230         $this->assertEquals( $ts1, wp_next_scheduled($hook, $args) );
     249        $this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
    231250    }
    232251}
Note: See TracChangeset for help on using the changeset viewer.