* @copyright Copyright 2007 Edward Dale * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0 * @version $Id$ * @link http://www.scompt.com/projects/wp-crontrol * @since 0.2 */ class Crontrol { /** * Hook onto all of the actions and filters needed by the plugin. */ function Crontrol() { if( function_exists('add_action') ) { add_action('init', array(&$this, 'init')); add_action('init', array(&$this, 'handle_posts')); add_action('admin_menu', array(&$this, 'admin_menu')); // Make sure the activation works from subdirectories as well as // directly in the plugin directory. $activate_action = str_replace(ABSPATH.PLUGINDIR.'/', 'activate_', __FILE__); add_action($activate_action, array(&$this, 'activate')); add_filter('cron_schedules', array(&$this, 'cron_schedules')); add_action('wp_ajax_delete-sched', array(&$this, 'handle_ajax')); add_action('wp_ajax_delete-cron', array(&$this, 'handle_ajax')); } } /** * Run using the 'init' action. */ function init() { load_plugin_textdomain('crontrol', PLUGINDIR.'/wp-crontrol/gettext'); } /** * Run using the 'admin_print_scripts' action. Added in the 'admin_menu' * hook. */ function scripts() { wp_enqueue_script( 'listman'); } /** * Handles any ajax requests made by the plugin. Run using * the 'wp_ajax_*' actions. */ function handle_ajax() { switch( $_POST['action'] ) { case 'delete-sched': if( !current_user_can('manage_options') ) die('-1'); $to_delete = $_POST['id']; $this->delete_schedule($to_delete); exit('1'); break; case 'delete-cron': if( !current_user_can('manage_options') ) die('-1'); break; } } /** * Handles any POSTs made by the plugin. Run using the 'init' action. */ function handle_posts() { if( isset($_POST['new_cron']) ) { if( !current_user_can('manage_options') ) die('You are not allowed to add new cron events.'); wp_schedule_event(time(), $_POST['schedule'], $_POST['hookname']); } else if( isset($_POST['new_schedule']) ) { if( !current_user_can('manage_options') ) die('You are not allowed to add new cron schedules.'); check_admin_referer("new-sched"); $name = $_POST['internal_name']; $interval = $_POST['interval']; $display = $_POST['display_name']; $this->add_schedule($name, $interval, $display); wp_redirect('options-general.php?page=crontrol_admin_options_page'); } else if( isset($_GET['action']) && $_GET['action']=='delete-sched') { if( !current_user_can('manage_options') ) die('You are not allowed to delete cron schedules.'); $id = $_GET['id']; check_admin_referer("delete-sched_$id"); $this->delete_schedule($id); wp_redirect('options-general.php?page=crontrol_admin_options_page'); } } /** * Adds a new custom cron schedule. * * @param string $name The internal name of the schedule * @param int $interval The interval between executions of the new schedule * @param string $display The display name of the schedule */ function add_schedule($name, $interval, $display) { $old_scheds = get_option('crontrol_schedules'); $old_scheds[$name] = array('interval'=>$interval, 'display'=>$display); update_option('crontrol_schedules', $old_scheds); } /** * Deletes a custom cron schedule. * * @param string $name The internal_name of the schedule to delete. */ function delete_schedule($name) { $scheds = get_option('crontrol_schedules'); unset($scheds[$name]); update_option('crontrol_schedules', $scheds); } /** * Sets up the plugin environment upon first activation. * * Run using the 'activate_' action. */ function activate() { $extra_scheds = array('twicedaily'=>array('interval'=>43200, 'display'=>'Twice Daily')); add_option('crontrol_schedules', $extra_scheds); } /** * Adds options & management pages to the admin menu. * * Run using the 'admin_menu' action. */ function admin_menu() { $page = add_options_page('Crontrol', 'Crontrol', 'manage_options', 'crontrol_admin_options_page', array(&$this, 'admin_options_page') ); add_action("admin_print_scripts-$page", array(&$this, 'scripts') ); $page = add_management_page('Crontrol', "Crontrol", 'manage_options', 'crontrol_admin_manage_page', array(&$this, 'admin_manage_page') ); } /** * Gives WordPress the plugin's set of cron schedules. * * Called by the 'cron_schedules' filter. * * @param array $scheds The current cron schedules. Usually an empty array. * @return array The existing cron schedules along with the plugin's schedules. */ function cron_schedules($scheds) { $new_scheds = get_option('crontrol_schedules'); return array_merge($new_scheds, $scheds); } /** * Displays the options page for the plugin. */ function admin_options_page() { $schedules = wp_get_schedules(); $custom_schedules = get_option('crontrol_schedules'); $custom_keys = array_keys($custom_schedules); uasort($schedules, create_function('$a,$b', 'return $a["interval"]-$b["interval"];')); ?>

Cron Schedules (add new)

$data ) { echo ""; echo ""; echo ""; echo ""; if( in_array($name, $custom_keys) ) { echo ""; } echo ""; $class = empty($class)?"alternate":""; } ?>
Name Interval Display Name Actions
$name{$data['interval']} (".$this->time_since(time(), time()+$data['interval'])."){$data['display']}".__( 'Delete' )."

Add new cron schedule

WP-Cron Entries (add new)

$cron ) { foreach( $cron as $hook=>$data) { $data = array_shift($data); echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; $class = empty($class)?"alternate":""; } } ?>
Hook Name Next Run Recurrence Actions
$hook".strftime("%D %T", $time)." (".$this->time_since(time(), $time)."){$data['interval']} (".$this->time_since(time(), time()+$data['interval']).")

Add new cron entry