Laying out the plan for an extended reschedule function.
Time based rescheduling
With the tad-reschedule plugin I’ve tried to make rescheduling a finite number of single events easier.
Without going over it again I’d like to seamlessly expand its functionalities to support hook-based scheduling, in plain English terms:
Until a condition is true do this action on this hook.
In coding terms I would like the function chain not to change and allow for this new definition:
tad_reschedule('my_function')
->until('my_condition')
->each('shutdown')
->with_args('my_args');
The changes are in what arguments are passed to the methods, extending the example;
function my_function($name, $says){
$people_says = get_option('people_says', array());
$people_says[] = sprintf('% says %s',$name, $says);
update_option('people_says', $people_says);
}
function my_condition(){
return count(get_option('people_says',array())) < 10;
}
function my_args(){
$user = wp_get_current_user();
$name = $user->user_firstname;
$says = get_user_meta($user->ID, 'quote_of_the_day', true);
return array($name, $says);
}
tad_reschedule('my_function')
->until('my_condition')
->each('shutdown')
->with_args('my_args');
The example now makes “some” sense. The code I would not like to write is
if( my_condition() ){
$args = my_args();
add_action('shutdown', 'my_function', 10, $args);
}
But why?
The last code snippet above, given the common part requiring the definition of the accessory functions, is not longer than the one I propose. Then why bother?
It might be a personal case but I tend to delegate a lot of tasks to the shutdown
, init
or plugins_loaded
hooks until those tasks become simply too heavy to be executed on each WordPress run and I end up with a polluted hook list full of checks and operations that could happen every once in a while.
Using the syntax above, should the above scenario happen, moving from a task running on each init
to a task running each 10 minutes will require little rewrite from this
tad_reschedule('my_function')
->until('my_condition')
->each('init')
->with_args('my_args');
to this
tad_reschedule('my_function')
->until('my_condition')
->each(600)
->with_args('my_args');
That’s why.
Next
More tests and a version new and same.