WordPress.org

Make WordPress Core

Ticket #35491: 35491.patch

File 35491.patch, 1.9 KB (added by dlh, 4 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                wp_schedule_event( $ts1, 'hourly', $hook, array( 'foo' ) );
     94                wp_schedule_event( $ts2, 'daily', $hook, 'bar' );
     95                wp_schedule_event( $ts2, 'twicedaily', $hook, array( 'baz' ) );
     96
     97                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     98
     99                wp_unschedule_event( $ts1, $hook, array( 'foo' ) );
     100                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     101
     102                wp_unschedule_event( $ts2, $hook, 'bar' );
     103                $this->assertTrue( wp_is_scheduled_hook( $hook ) );
     104
     105                wp_unschedule_event( $ts2, $hook, array( 'baz' ) );
     106                $this->assertFalse( wp_is_scheduled_hook( $hook ) );
     107        }
     108
    88109        function test_unschedule_event() {
    89110                // schedule an event and make sure it's returned by wp_next_scheduled
    90111                $hook = rand_str();