Make WordPress Core

Ticket #35491: 35491.2.patch

File 35491.2.patch, 2.0 KB (added by dlh, 9 years ago)
  • src/wp-includes/cron.php

     
    232232}
    233233
    234234/**
     235 * Check whether an action hook is scheduled to fire.
     236 *
     237 * Unlike {@see wp_next_scheduled()}, this checks only whether a hook is
     238 * scheduled with cron, not whether it will pass given arguments or when.
     239 *
     240 * @since x.y.z
     241 *
     242 * @param string $hook Hook to look for.
     243 * @return bool Whether the hook is scheduled.
     244 */
     245function wp_is_scheduled_hook( $hook ) {
     246        $crons = _get_cron_array();
     247
     248        if ( empty( $crons ) ) {
     249                return false;
     250        }
     251
     252        foreach ( $crons as $cron ) {
     253                if ( isset( $cron[ $hook ] ) ) {
     254                        return true;
     255                }
     256        }
     257
     258        return false;
     259}
     260
     261/**
    235262 * Sends a request to run cron through HTTP request that doesn't halt page loading.
    236263 *
    237264 * @since 2.1.0
  • tests/phpunit/tests/cron.php

     
    8585
    8686        }
    8787
     88        function test_is_scheduled_hook() {
     89                $hook = rand_str();
     90                $ts1 = strtotime( '+30 minutes' );
     91                $ts2 = strtotime( '+2 hours' );
     92
     93                $this->assertFalse( wp_is_scheduled_hook( $hook ) );
     94
     95                wp_schedule_event( $ts1, 'hourly', $hook, array( 'foo' ) );
     96                wp_schedule_event( $ts2, 'daily', $hook, 'bar' );
     97                wp_schedule_event( $ts2, 'twicedaily', $hook, array( 'baz' ) );
     98
     99                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     100
     101                wp_unschedule_event( $ts1, $hook, array( 'foo' ) );
     102                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     103
     104                wp_unschedule_event( $ts2, $hook, 'bar' );
     105                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     106
     107                wp_unschedule_event( $ts2, $hook, array( 'baz' ) );
     108                $this->assertFalse( wp_is_scheduled_hook( $hook ) );
     109        }
     110
    88111        function test_unschedule_event() {
    89112                // schedule an event and make sure it's returned by wp_next_scheduled
    90113                $hook = rand_str();