Reschedule Events 01

Laying out a WordPress helper to reschedule events.

The problem

While working on an iteration fo the theAverageDev Restricted Content plugin I ran into the need of a quick and re-usable way to schedule chains of single events.
WordPress defines the wp_schedule_event and wp_schedule_single_event functions to deal with cases where something has either to happen once or to happen indefinitely at regular (as regular as WordPress cron system allows) intervals.
There is no built-in function to cover something like “do this each x seconds until” and I’d like to come up with something to cover that need.
The quick and easy solution to that problem would be something like

wp_schedule_event(time(), 'ten_minutes', 'my_hook', array('one', 23, 'foo')); add_action('my_hook', 'my_function'); function my_function(){ if(!get_option('my_option', false)){ wp_clear_scheduled_hook('my_hook', array('one', 23, 'foo')); return; } // do something } 

The model

I like Mark Jaquith transients function very much in their simplcity and clarity; copying and pasting the code example on the repository site

<?php // Define your callback (other examples use this) function my_callback() { return wp_remote_retrieve_body( wp_remote_get( 'http://example.com/feed.xml', array( 'timeout' => 30 ) ) ); } // Grab that feed echo tlc_transient( 'example-feed' ) ->updates_with( 'my_callback' ) ->expires_in( 300 ) ->get(); ?> 

and I’d like to come up with something like this to solve my problem

reschedule( 'my_hook' ) ->while( get_option('my_option', false) ) ->each( 600 ) ->with_args( array('one', 23, 'foo') ); 

The function parameters and API are simple and straightforward enough to require little explanation and I’d like the function chain not to be overreaching in its functionalities and ambitions.

Next: test driven development

While I predict this project being a quetison of hours I will use WP Browser and TDD in general to develop the small plugin.

Probably related

I appreciate your input