WordPress.org

Make WordPress Core

Ticket #18997: 18997-refreshed-mordauks-patch.patch

File 18997-refreshed-mordauks-patch.patch, 2.5 KB (added by jrf, 4 years ago)
  • src/wp-includes/cron.php

    From 7907bc94d52fc077844a697e07f87dbb8db38d21 Mon Sep 17 00:00:00 2001
    From: jrfnl <github_nospam@adviesenzo.nl>
    Date: Tue, 8 Dec 2015 20:40:27 +0100
    Subject: [PATCH] Refresh of @mordauk's patch: Add wp_unschedule_hook() + unit
     tests.
    
    ---
     src/wp-includes/cron.php     | 25 +++++++++++++++++++++++++
     tests/phpunit/tests/cron.php | 22 ++++++++++++++++++++++
     2 files changed, 47 insertions(+)
    
    diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php
    index 60492c5..88ca1b5 100644
    a b function wp_clear_scheduled_hook( $hook, $args = array() ) { 
    211211}
    212212
    213213/**
     214 * Unschedule all previously scheduled cron jobs for a hook.
     215 *
     216 * Can be useful for plugins when deactivating to clean up the cron queue.
     217 *
     218 * The $hook parameter is required, so that the events can be identified.
     219 *
     220 * @since x.x
     221 *
     222 * @param string $hook Action hook, the execution of which will be unscheduled.
     223 */
     224function wp_unschedule_hook( $hook ) {
     225        $crons = _get_cron_array();
     226
     227        foreach( $crons as $timestamp => $args ) {
     228                unset( $crons[ $timestamp ][ $hook ] );
     229
     230                if ( empty( $crons[ $timestamp ] ) ) {
     231                        unset( $crons[ $timestamp ] );
     232                }
     233        }
     234
     235        _set_cron_array( $crons );
     236}
     237
     238/**
    214239 * Retrieve the next timestamp for a cron event.
    215240 *
    216241 * @since 2.1.0
  • tests/phpunit/tests/cron.php

    diff --git a/tests/phpunit/tests/cron.php b/tests/phpunit/tests/cron.php
    index b411a5b..e03bd3e 100644
    a b class Tests_Cron extends WP_UnitTestCase { 
    124124                $this->assertFalse( wp_next_scheduled($hook, $args) );
    125125        }
    126126
     127        /**
     128         * @ticket 18997
     129         */
     130        function test_unschedule_hook() {
     131                $hook = rand_str();
     132                $args = array(rand_str());
     133
     134                // schedule several events with and without arguments.
     135                wp_schedule_single_event( strtotime('+1 hour'), $hook );
     136                wp_schedule_single_event( strtotime('+2 hour'), $hook );
     137                wp_schedule_single_event( strtotime('+3 hour'), $hook, $args );
     138                wp_schedule_single_event( strtotime('+4 hour'), $hook, $args );
     139
     140                // make sure they're returned by wp_next_scheduled().
     141                $this->assertTrue( wp_next_scheduled($hook) > 0 );
     142                $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
     143
     144                // clear the schedule for the no args events and make sure it's gone.
     145                wp_unschedule_hook($hook);
     146                $this->assertFalse( wp_next_scheduled($hook) );
     147        }
     148
    127149        function test_clear_schedule_multiple_args() {
    128150                $hook = rand_str();
    129151                $args = array(rand_str(), rand_str());