functions.php
file in your theme."
+msgstr ""
+
+#: wp-crontrol.php:440
+msgid "Add Cron Entry »"
+msgstr ""
+
+#: wp-crontrol.php:471
+msgid "now"
+msgstr "jetzt"
diff --git a/trunk/gettext/crontrol.pot b/trunk/gettext/crontrol.pot
new file mode 100644
index 0000000..2b589d7
--- /dev/null
+++ b/trunk/gettext/crontrol.pot
@@ -0,0 +1,174 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR functions.php
file in your theme."
+msgstr ""
+
+#: wp-crontrol.php:440
+msgid "Add Cron Entry »"
+msgstr ""
+
+#: wp-crontrol.php:471
+msgid "now"
+msgstr ""
diff --git a/trunk/readme.txt b/trunk/readme.txt
new file mode 100644
index 0000000..3ad5c17
--- /dev/null
+++ b/trunk/readme.txt
@@ -0,0 +1,87 @@
+=== WP-Crontrol ===
+Contributors: scompt
+Donate link: http://scompt.com/projects/wp-crontrol
+Tags: admin, cron, plugin, control
+Requires at least: 2.1
+Tested up to: 2.5
+Stable tag: 0.3
+
+WP-Crontrol lets you take control over what's happening in the WP-Cron system.
+
+== Description ==
+
+WP-Crontrol lets you take control over what's happening in the WP-Cron system.
+
+== Installation ==
+
+1. Upload the `wp-crontrol` directory to the `/wp-content/plugins/` directory
+1. Activate the plugin through the 'Plugins' menu in WordPress
+1. Goto the Options->Crontrol panel to add some new cron schedules.
+1. Goto the Manage->Crontrol panel to see what cron entries are scheduled and to add some new ones.
+
+== Frequently Asked Questions ==
+
+= What's the use of adding new cron schedules? =
+
+Cron schedules are used by WordPress and WordPress plugins to allow you to schedule commands to be executed at regular intervals. Intervals must be provided by the WordPress core or a plugin in order to be used. An example of a plugin that uses these schedules is [WordPress Database Backup](http://www.ilfilosofo.com/blog/wp-db-backup/). Out of the box, only daily and hourly backups are supported. In order to do a weekly backup, a weekly cron schedule must be entered into WP-Crontrol first and then the backup plugin can take advantage of it as an interval.
+
+= How do I create a new cron entry? =
+
+There are two steps to getting a functioning cron entry that executes regularly. The first step is telling WordPress about the hook. This is the part that WP-Crontrol was created to provide. The second step is calling your function when your hook is executed. You've got to do that on your own, but I'll explain how below.
+
+*Step One: Adding the hook*
+
+In the Manage->Crontrol admin panel, enter the details of the hook. You're best off having a hookname that conforms to normal PHP variable naming conventions. This could save you trouble later. Other than that, the hookname can be whatever you want it to be. Next run is the next time that the hook will execute. This can be entered in using [GNU Date Input Formats](http://www.gnu.org/software/tar/manual/html_node/tar_113.html), but often *now* is good enough. The entry schedule is how often your hook will be executed. If you don't see a good interval, then add one in the Options->Crontrol admin panel.
+
+*Step Two: Writing the function*
+
+This part takes place in PHP code (for example, in the `functions.php` file from your theme). To execute your hook, WordPress runs an [action](http://codex.wordpress.org/Plugin_API#Actions). For this reason, we need to now tell WordPress which function to execute when this action is run. The following line accomplishes that:
+
+`add_action('my_hookname', 'my_function');`
+
+The next step is to write your function. Here's a simple example:
+
+`function my_function() {
+ wp_mail('scompt@scompt.com', 'WP-Crontrol', 'WP-Crontrol rocks!');
+}`
+
+= Do I really need the entire `wp-crontrol` directory? =
+
+Nope! The only file that you really need is `wp-crontrol.php`. Toss this file in your `wp-content/plugins` directory and you're good to go.
+
+= How do I ask a frequently asked question? =
+
+Email [me](mailto:scompt@scompt.com).
+
+== Screenshots ==
+
+1. New cron entries can be added, modified, and deleted. In addition, they can be executed on-demand.
+1. New cron schedules can be added to WordPress, giving plugin developers more options when scheduling commands.
+
+== Future Plans ==
+
+= Coming up in Version 0.4! =
+
+* Ability to add PHP code to be executed for the hook name
+* German localization
+
+== Version History ==
+
+= Version 0.1 =
+
+* Super basic, look at what's in WP-Cron functionality.
+
+= Version 0.2 =
+
+* Fully documented the code.
+* Fixed the bug that the activate action wouldn't be run if the plugin wasn't in a subdirectory.
+* Now will play nicely in case any other plugins specify additional cron schedules.
+* Minor cosmetic fixes.
+
+= Version 0.3 =
+
+* Internationalization
+* Editing/deleting/execution of cron entries
+* More text, status messages, etc.
+* Allow a user to enter a schedule entry in a human manner
+* Looks better on WordPress 2.5
\ No newline at end of file
diff --git a/trunk/screenshot-1.png b/trunk/screenshot-1.png
new file mode 100644
index 0000000..cbf7ed6
Binary files /dev/null and b/trunk/screenshot-1.png differ
diff --git a/trunk/screenshot-2.png b/trunk/screenshot-2.png
new file mode 100644
index 0000000..9b196df
Binary files /dev/null and b/trunk/screenshot-2.png differ
diff --git a/trunk/wp-crontrol.php b/trunk/wp-crontrol.php
new file mode 100644
index 0000000..6a78675
--- /dev/null
+++ b/trunk/wp-crontrol.php
@@ -0,0 +1,552 @@
+
+ * @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'); ?>
+ +