* @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', str_replace(ABSPATH, '', dirname(__FILE__).'/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'); $to_delete = $_POST['id']; $this->delete_cron($to_delete); exit('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.', 'crontrol')); check_admin_referer("new-cron"); $next_run = $_POST['nextrun']; $schedule = $_POST['schedule']; $hookname = $_POST['hookname']; $this->add_cron($next_run, $schedule, $hookname); wp_redirect("edit.php?page=crontrol_admin_manage_page&crontrol_message=5&crontrol_name=$hookname"); } else if( isset($_POST['edit_cron']) ) { if( !current_user_can('manage_options') ) die(__('You are not allowed to add new cron events.', 'crontrol')); $next_run = $_POST['nextrun']; $schedule = $_POST['schedule']; $hookname = $_POST['hookname']; $original_hookname = $_POST['original_hookname']; check_admin_referer("edit-cron_{$original_hookname}"); $this->delete_cron($original_hookname); $this->add_cron($next_run, $schedule, $hookname); wp_redirect("edit.php?page=crontrol_admin_manage_page&crontrol_message=4&crontrol_name=$hookname"); } else if( isset($_POST['new_schedule']) ) { if( !current_user_can('manage_options') ) die(__('You are not allowed to add new cron schedules.', 'crontrol')); check_admin_referer("new-sched"); $name = $_POST['internal_name']; $interval = $_POST['interval']; $display = $_POST['display_name']; // The user entered something that wasn't a number. // Try to convert it with strtotime if( !is_numeric($interval) ) { $now = time(); $future = strtotime($interval, $now); if( $future===FALSE || $future == -1 || $now>$future) { wp_redirect("options-general.php?page=crontrol_admin_options_page&crontrol_message=7&crontrol_name=".urlencode($interval)); return; } $interval = $future-$now; } else if( $interval<=0 ) { wp_redirect("options-general.php?page=crontrol_admin_options_page&crontrol_message=7&crontrol_name=".urlencode($interval)); return; } $this->add_schedule($name, $interval, $display); wp_redirect("options-general.php?page=crontrol_admin_options_page&crontrol_message=3&crontrol_name=$name"); } else if( isset($_GET['action']) && $_GET['action']=='delete-sched') { if( !current_user_can('manage_options') ) die(__('You are not allowed to delete cron schedules.', 'crontrol')); $id = $_GET['id']; check_admin_referer("delete-sched_$id"); $this->delete_schedule($id); wp_redirect("options-general.php?page=crontrol_admin_options_page&crontrol_message=2&crontrol_name=$id"); } else if( isset($_GET['action']) && $_GET['action']=='delete-cron') { if( !current_user_can('manage_options') ) die(__('You are not allowed to delete cron events.', 'crontrol')); $id = $_GET['id']; check_admin_referer("delete-cron_$id"); $this->delete_cron($id); wp_redirect("edit.php?page=crontrol_admin_manage_page&crontrol_message=6&crontrol_name=$id"); } else if( isset($_GET['action']) && $_GET['action']=='run-cron') { if( !current_user_can('manage_options') ) die(__('You are not allowed to run cron events.', 'crontrol')); $id = $_GET['id']; check_admin_referer("run-cron_$id"); $this->run_cron($id); wp_redirect("edit.php?page=crontrol_admin_manage_page&crontrol_message=1&crontrol_name=$id"); } } /** * Executes a cron entry immediately. * * Executes an entry by deleting it and adding it again with a * run time of 'now'. * * @param string $hookname The hookname of the cron entry to run */ function run_cron($hookname) { $sched = wp_get_schedule($hookname); wp_clear_scheduled_hook($hookname); $this->add_cron('now', $sched, $hookname); } /** * Adds a new cron entry. * * @param string $next_run A human-readable (strtotime) time that the entry should be run at * @param string $schedule The recurrence of the cron entry * @param string $hookname The name of the hook to execute */ function add_cron($next_run, $schedule, $hookname) { $next_run = strtotime($next_run); if( $next_run===FALSE || $next_run==-1 ) $next_run=time(); if( !wp_schedule_event( $next_run, $schedule, $hookname ) ) { $error=True; } } /** * Deletes a cron entry. * * @param string $name The hookname of the entry to delete. */ function delete_cron($to_delete) { wp_clear_scheduled_hook($to_delete); } /** * 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', 'crontrol'))); add_option('crontrol_schedules', $extra_scheds); // if there's never been a cron entry, _get_cron_array will return FALSE if( _get_cron_array() === FALSE ) { _set_cron_array(array()); } } /** * 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') ); add_action("admin_print_scripts-$page", array(&$this, 'scripts') ); } /** * 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"];')); if( isset($_GET['crontrol_message']) ) { $messages = array( '2' => __("Successfully deleted the cron schedule %s", 'crontrol'), '3' => __("Successfully added the cron schedule %s", 'crontrol'), '7' => __("Cron schedule not added because there was a problem parsing %s", 'crontrol')); $hook = $_GET['crontrol_name']; $msg = sprintf($messages[$_GET['crontrol_message']], $hook); echo "
$msg
| $name | "; echo "{$data['interval']} (".$this->interval($data['interval']).") | "; echo "{$data['display']} | "; if( in_array($name, $custom_keys) ) { echo "".__( 'Delete' )." | "; } else { echo "\n"; } echo " |
$msg
| $hook | "; echo "".strftime("%D %T", $time)." (".$this->time_since(time(), $time).") | "; echo "{$data['interval']} (".$this->interval($data['interval']).") | "; echo "Edit | "; echo "Do Now | "; echo "Delete | "; echo "
functions.php file in your theme.', 'crontrol'); ?>