From 641a4b643f0abcae61943108e6cdf728fb61cdbc Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 30 Jun 2017 00:22:44 +0000 Subject: [PATCH] Cron API: Introduce `wp_unschedule_hook()` to clear all events attached to a hook. This complements `wp_clear_scheduled_hook()`, which only clears events attached with specific arguments. Props arena, mordauk, jrf. Fixes #18997. Built from https://develop.svn.wordpress.org/trunk@40965 git-svn-id: http://core.svn.wordpress.org/trunk@40815 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/cron.php | 25 ++++++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/wp-includes/cron.php b/wp-includes/cron.php index f7e444b83e..69502a3e45 100644 --- a/wp-includes/cron.php +++ b/wp-includes/cron.php @@ -191,7 +191,7 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) { } /** - * Unschedule all events attached to the specified hook. + * Unschedule all events attached to the hook with the specified arguments. * * @since 2.1.0 * @@ -221,6 +221,29 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) { } } +/** + * Unschedule all events attached to the hook. + * + * Can be useful for plugins when deactivating to clean up the cron queue. + * + * @since 4.9.0 + * + * @param string $hook Action hook, the execution of which will be unscheduled. + */ +function wp_unschedule_hook( $hook ) { + $crons = _get_cron_array(); + + foreach( $crons as $timestamp => $args ) { + unset( $crons[ $timestamp ][ $hook ] ); + + if ( empty( $crons[ $timestamp ] ) ) { + unset( $crons[ $timestamp ] ); + } + } + + _set_cron_array( $crons ); +} + /** * Retrieve the next timestamp for an event. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 5361953162..a4401006a7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9-alpha-40964'; +$wp_version = '4.9-alpha-40965'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.