<?php
/*
Plugin Name: Test Cron
Plugin URI: http://simonwheatley.co.uk/wordpress/
Description: A plugin to run WP CRON through it's paces.
Author: Simon Wheatley
Version: 0.50b
Author URI: http://simonwheatley.co.uk/wordpress/
*/

function tc_activate()
{
    
tc_clear_hook();
    
tc_list_cron();

    
error_log"---------------------------------------" );

    
error_log"Setting a single event in five seconds with the hook 'tc_cron_job'" );
    
$timestamp time() + 5// Five seconds into the future
    
$args = array( 'job' => 'Job 1' );
    
wp_schedule_single_event$timestamp'tc_cron_job'$args );

    
error_log"Setting a single event in ten seconds with the hook 'tc_cron_job'" );
    
$timestamp time() + 10// Ten seconds into the future
    
$args'job' ] = 'Job 2'// Different args
    
wp_schedule_single_event$timestamp'tc_cron_job'$args );

    
error_log"Setting *another* single event in ten seconds with the hook 'tc_cron_job'" );
    
$args'job' ] = 'Job 3'// Different args
    
wp_schedule_single_event$timestamp'tc_cron_job'$args );

    
tc_list_cron();
}

function 
tc_deactivate()
{
    
tc_clear_hook();
    
tc_list_cron();
}

function 
tc_clear_hook()
{
    
error_log"Clear the CRONs of anything using our hook, so we have a clean sheet" );
    
$crons _get_cron_array(); // Naughty using this function really, but as it's just part of a test
    
if ( ! is_array$crons ) ) return;
    foreach ( 
$crons as $timestamp => $cronhooks ) {
        foreach ( (array) 
$cronhooks as $hook => $args ) {
            if ( 
$hook == 'tc_cron_job' ) unset( $crons$timestamp ][ $hook ] );
            if ( empty( 
$crons$timestamp ] ) ) unset( $crons$timestamp ] );
        }
    }
    
// Write the Crons back in
    
_set_cron_array$crons ); // Naughty using this function really, but as it's just part of a test
    
error_log"All jobs with a hook of 'tc_cron_job' now cleared" );
}

function 
tc_init()
{
    
error_log"Current timestamp: " time() );
    
tc_list_cron();
}

function 
tc_list_cron()
{
    
$cron _get_cron_array();
    
error_log"CRON jobs: " print_r$crontrue ) );
}

function 
tc_cron_job$arg )
{
    
$now dateDATE_RFC822time() );
    
error_log"Cron test, $now, Arg: $arg);
}

add_action'init''tc_init' );
register_activation_hook __FILE__'tc_activate' );
register_deactivation_hook __FILE__'tc_deactivate' );
add_action'tc_cron_job''tc_cron_job' );

?>