All pastes #2121908 Raw Edit

cron-test for Wordpress

public php v1 · immutable
#2121908 ·published 2012-02-27 01:26 UTC
rendered paste body
<?php/*Plugin Name: Cron TestDescription: Unit test for odd cron behavior..Version: 1.0Author: Jeff Brand*/if ( !function_exists( 'get_option' ) )	die( 'No direct access!' );DFCronTest::setup();class DFCronTest {	static $hook = 'df_cron_test';	static $sched = 'df_every_minute';	static function setup() {		//Basics		add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) );		add_action( 'df_cron_test',   array( __CLASS__, 'debug' ) );		//UI		add_action( 'admin_menu',     array( __CLASS__, 'admin_menu' ) );	}	static function cron_schedules( $schedules ) {		$schedules['df_every_minute'] = array(			'interval' => 60,			'display' => 'Every Minute'		);		return $schedules;	}	static function cron_active( $activate = null ) {		if ( $activate === null )			return (bool)wp_next_scheduled( self::$hook );		if ( $activate ) {			if ( !wp_next_scheduled( self::$hook ) )				wp_schedule_event( time(), self::$sched, self::$hook );		} else {			wp_clear_scheduled_hook( self::$hook );		}		return (bool)$activate;	}	static function admin_menu() {		add_submenu_page( 'tools.php', 'Cron test', 'Cron Test', 'manage_options', 'cron_test_page', array( __CLASS__, 'page_cron_test' ) );	}	static function page_cron_test() {		global $title;		if ( !empty( $_REQUEST['noheader'] ) && !empty( $_REQUEST['action'] ) ) {			switch ($_REQUEST['action'] ) {				case 'toggle_cron':					$current_cron = self::cron_active();					$new_cron = self::cron_active( !$current_cron );					$msg = $new_cron ? 'Enabled' : 'Disabled';					wp_redirect( add_query_arg( 'msg', $msg, admin_url( 'tools.php?page=' . $_REQUEST['page'] ) ) );					break;			}		}		?><div class="wrap">	<h2><?php echo $title; ?></h2>	<?php if ( !empty( $_REQUEST['msg'] ) ) echo '<div class="updated"><p>' . $_REQUEST['msg'] . '</p></div>'; ?>	<table class="form-table">		<tr>			<th>Hook Status</th>			<td><?php				$url = add_query_arg( 'action', 'toggle_cron', admin_url( 'tools.php?noheader=1&page=' . $_REQUEST['page'] ) );				$label = self::cron_active() ? 'Disable' : 'Enable';				echo sprintf( '<a class="button" href="%s">%s</a>', $url, $label );			?></td>		</tr>		<tr>			<th>Last Run</th>			<td><?php echo date( 'm/d/Y H:i:s', get_option( 'df_cron_test_lastrun' ) ); ?></td>		</tr>		<tr>			<th>Last Run From Action</th>			<td><?php echo date( 'm/d/Y H:i:s', get_option( 'df_cron_test_lastrun_from_action' ) ); ?></td>		</tr>		<tr>			<th>Next Run</th>			<td><?php				$next_run = wp_next_scheduled( 'df_cron_test' );				echo $next_run ? date( 'm/d/Y H:i:s', $next_run ) : 'Disabled';			?></td>		</tr><!--		<tr>			<th>Debug</th>			<td><pre>			<?php print_r( _get_cron_array() ); ?>			</pre></td>		</tr>-->	<table></div>		<?php	}	static function debug() {		update_option( 'df_cron_test_lastrun_from_action', time() );	}}function df_cron_test( $args ) {	update_option( 'df_cron_test_lastrun', time() );	do_action( 'df_cron_test' );}