Make WordPress Core

Opened 4 years ago

Closed 2 months ago

Last modified 4 weeks ago

#52655 closed enhancement (fixed)

Ability to Change the wp_next_scheduled() function output.

Reported by: infosatech's profile infosatech Owned by: johnbillion's profile johnbillion
Milestone: 6.8 Priority: normal
Severity: normal Version:
Component: Cron API Keywords: has-patch
Focuses: Cc:

Description

If I want to replace WP Cron with the Action Scheduler Library it is necessary to change the wp_next_scheduled() function output by overriding it using a filter to use as_next_scheduled_action() function.

function wp_next_scheduled( $hook, $args = array() ) {
    $next_event = wp_get_scheduled_event( $hook, $args );
    if ( ! $next_event ) {
        return false;
    }
 
    return apply_filters( 'wp_next_scheduled', $next_event->timestamp, $next_event, $hook, $args );
}

If we use 'pre_get_scheduled_event' filter, it will also override the default Cron functions. So that I think 'wp_next_scheduled' is necessary.

Change History (10)

This ticket was mentioned in PR #1045 on WordPress/wordpress-develop by iamsayan.


4 years ago
#1

Added 'wp_next_scheduled' filter to add the ability to change the wp_next_scheduled() function output.

Trac ticket: https://core.trac.wordpress.org/ticket/52655

#2 @johnbillion
4 years ago

  • Component changed from General to Cron API
  • Version 5.6.2 deleted

#3 @peterwilsoncc
4 years ago

@infosatech Hello, and welcome to trac!

If we use 'pre_get_scheduled_event' filter, it will also override the default Cron functions. So that I think 'wp_next_scheduled' is necessary.

Are you able to expand on this a little bit? I absolutely understand why you might wish to replace wp-cron with action scheduler but I am unclear as to why overriding the other wp-cron functionality is undesirable? Doesn't replacing cron require you to use all the pre_* filters within wp-cron?

I'm not opposed to adding the filter, by the way, it was something I considered back in 5.1 but ultimately decided against, see #45976.

#4 @infosatech
4 years ago

Hello @peterwilsoncc

I have written a plugin to automatically migrate WP Cron to the Action Scheduler library. Here is the link: https://github.com/iamsayan/migrate-cron-to-action-scheduler/

But Action Scheduler does not store recurrence value as WordPress stores using 'cron_schedules' filter. So, If I hijack wp_get_scheduled_event() function using filter 'pre_get_scheduled_event' then I can't set the recurrence. See here(https://github.com/iamsayan/migrate-cron-to-action-scheduler/blob/main/migrate-cron-to-action-scheduler.php#L299). I have to set it as false which is, I think, not recommended.

So I think just to hijack the wp_next_scheduled() function and not the wp_get_scheduled_event() function.

#5 @peterwilsoncc
4 years ago

Thanks for the further explanation @infosatech. There's a gotcha with not returning the named schedule (which I've logged on your GitHub repo).

@johnbillion has created a pull request for the new filter, I will review and follow up there.

#6 @peterwilsoncc
4 years ago

Sorry, I misread the email -- I now realise @infosatech has created the patch.

#7 @johnbillion
2 months ago

  • Milestone changed from Awaiting Review to 6.8
  • Owner set to johnbillion
  • Status changed from new to reviewing

#8 @johnbillion
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 59824:

Cron API: Introduce the wp_next_scheduled filter which allows overriding the timestamp for the next scheduled event for the given hook.

This change also standardises some descriptions of the timestamp field used in various functions.

Props infosatech, peterwilsoncc

Fixes #52655

#9 @peterwilsoncc
4 weeks ago

  • Keywords add-to-field-guide added

@jeffpaul, as discussed in Slack I think it would be good to include this as a short note in the field guide:

Cron API: Introduce a new filter of the same name to wp_next_scheduled(). This allows plugin developers to modify the timestamp of the next scheduled event for a given wp-cron job. The full event object, hook name and arguments are provided as additional arguments for this filter.

Feel free to edit for length or clarity if you wish.

Last edited 4 weeks ago by peterwilsoncc (previous) (diff)

#10 @peterwilsoncc
4 weeks ago

  • Keywords add-to-field-guide removed
Note: See TracTickets for help on using tickets.