WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 5 months ago

#25266 closed enhancement (wontfix)

wp_unschedule_event should apply filter to changes

Reported by: _ck_ Owned by: chriscct7
Milestone: Priority: normal
Severity: normal Version:
Component: Cron API Keywords: has-patch
Focuses: Cc:

Description

wp_unschedule_event needs to announce removal of events, just like wp_schedule_event does through a filter

I would suggest allowing the filter to cancel the unschedule as a bonus.

ie.

function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
	$crons = _get_cron_array();
	$key = md5(serialize($args));
	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );	
	if ( apply_filters('unschedule_event', $event) ) 
		unset( $crons[$timestamp][$hook][$key] );
	if ( empty($crons[$timestamp][$hook]) )
		unset( $crons[$timestamp][$hook] );
	if ( empty($crons[$timestamp]) )
		unset( $crons[$timestamp] );
	_set_cron_array( $crons );
}

Optionally, consider if the filter returns false and there is no unset, the database should not be rewritten.

Attachments (1)

25266.patch (1.2 KB) - added by jrtashjian 5 years ago.

Download all attachments as: .zip

Change History (8)

#1 @mordauk
7 years ago

It makes sense that both wp_schedule_event() and wp_unschedule_event() have consistent filters.

#2 @sphoid
7 years ago

I would really like to see this happen. It is not possible to replace the cron system through a plugin without consistent filters/actions across all of the event functions.

#3 @chriscct7
5 years ago

  • Keywords needs-patch added
  • Severity changed from major to normal

@jrtashjian
5 years ago

#4 @jrtashjian
5 years ago

  • Keywords has-patch added; needs-patch removed

Patch adds a filter hook so we can hook into wp_unschedule_event before retrieving, removing, and updating the scheduled hooks. I left out the check to determine if we should write to the database because it's a cheap operation and probably not absolutely necessary. If people feel it should be there I'll add it in the future.

#5 @chriscct7
5 years ago

  • Owner set to chriscct7
  • Status changed from new to reviewing

#7 follow-up: @peterwilsoncc
2 years ago

This is now possible with the changes introduced in WordPress 5.1, please refer to the dev note on the Make WordPress Core P2.

#8 in reply to: ↑ 7 @peterwilsoncc
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from reviewing to closed

Replying to peterwilsoncc:

This is now possible with the changes introduced in WordPress 5.1, please refer to the dev note on the Make WordPress Core P2.

Closing this off given the filters introduced in WP 5.1 making it possible.

It may have changed in the years since this ticket was opened but update_option() does not update the database if the option is unchanged and the cron functions now provide meaningful return values.

Note: See TracTickets for help on using tickets.