Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active December 29, 2015 21:30
Show Gist options
  • Save johnregan3/7272957 to your computer and use it in GitHub Desktop.
Save johnregan3/7272957 to your computer and use it in GitHub Desktop.
Code for a wp_schedule_single_event Tutorial.
<?php
/**
* Plugin Name: Schedule Event Tutorial
* Description: Schedules single event to uncheck a checkbox on a custom Menu page
* Version: 0.1
* Author: John Regan
* Author URI: http://johnregan3.me
* Text Domain: setjr3
* License: GPLv2+
*/
/**
* Copyright (c) 2013 X-Team (http://x-team.com/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class is where you can see Scheduled Events in action.
*
* Here, the old scheduled event is unscheduled and the new event is set.
*/
class Schedule_Event_Tutorial {
/**
* Registers our 'uncheck_event_hook" action hook that fires our un-checking action.
*/
static function setup() {
// Scheduled Event Hook
add_action( 'uncheck_event_hook', array( __CLASS__, 'uncheck_checkbox' ) );
}
/**
* On settings save, Update scheduled event time.
*
* Begin by removing any scheduled event on our 'uncheck_event_hook' hook.
* Then, we format the saved time to Unix.
* Finally, we schedule the event.
*
* Typically, this could be fired when a post is saved ('save_post' hook, for instance),
* but it's not really that easy on an options page like this one.
*
* @action 'load-{page}'
* See Schedule_Event_Tutorial_Setup::menu_page()
*/
static function schedule_event() {
if ( ! isset( $_GET['settings-updated'] ) )
return;
// Remove any scheduled event on this hook
$scheduled_event = wp_next_scheduled( 'uncheck_event_hook' );
if ( $scheduled_event !== false ) {
wp_unschedule_event( $scheduled_event, 'uncheck_event_hook' );
}
// Schedule the event to uncheck the checkbox
$exp_time = self::expiration_time();
if ( is_numeric( $exp_time ) ) {
wp_schedule_single_event( $exp_time, 'uncheck_event_hook' );
}
}
/**
* Fetches and formats the saved expiration time
*
* Get expiration time from options, set it according to the local timezone
* then format it as Unix time.
*
* @return int $time Unix-formatted time string
*/
static function expiration_time() {
$exp_time = get_option( 'setjr3_time' );
$time = ( ! empty( $exp_time ) ) ? $exp_time : null ;
// If no time is set, exit.
if ( is_null( $time ) )
return false;
$tz_local = new DateTimeZone( get_option( 'timezone_string' ) );
$time = new DateTime( $time, $tz_local );
$time = (int)$time->format('U');
return $time;
}
/**
* The actual un-checking of the checkbox
*
* Fires on 'uncheck_event_hook' action, set in setup() above.
*
* @action 'uncheck_event_hook'
*/
static function uncheck_checkbox() {
update_option( 'setjr3_checkbox', null );
}
} // End class Schedule_Event_Tutorial
/**
* Set up the Tutorial
*
* The content of this class simply sets up the tutorial form.
* If you find it confusing, just ignore it.
*/
class Schedule_Event_Tutorial_Setup {
/**
* Add our actions/filter
*/
static function setup() {
$plugin = plugin_basename(__FILE__);
add_filter( "plugin_action_links_$plugin", array( __CLASS__, 'tutorial_link' ) );
// add action for Menu page
add_action( 'admin_menu', array( __CLASS__, 'menu_page' ) );
// add action for Settings
add_action( 'admin_init', array( __CLASS__, 'settings' ) );
// Run our Schedule_Event_Tutorial class above
Schedule_Event_Tutorial::setup();
// Clean up when this plugin is uninstalled
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
}
/**
* Print direct link to Tutorial Admin Page
*
* Fetches array of links generated by WP Plugin admin page ( Deactivate | Edit )
* and inserts a link to the Custom CSS admin page
*
* @param array $links Array of links generated by WP in Plugin Admin page.
* @return array $links Array of links to be output on Plugin Admin page.
*/
static function tutorial_link( $links ) {
$settings_page = '<a href="' . admin_url( 'admin.php?page=schedule-event-tutorial.php' ) .'">Tutorial</a>';
array_unshift( $links, $settings_page );
return $links;
}
/**
* Create Admin Menu Page
*
* Also fires action to Schedule the Event when page reloads.
* see Schedule_Event_Tutorial::schedule_event()
*/
static function menu_page() {
$slug = add_menu_page( __( 'Schedule Event Tutorial', 'setjr3' ), __( 'Schedule Event Tutorial', 'setjr3' ), 'manage_options', basename(__FILE__), array( __CLASS__, 'render_tutorial_page' ) );
add_action( 'load-' . $slug, array( 'Schedule_Event_Tutorial', 'schedule_event' ) );
}
/**
* Settings Page setup
*
* For more info: http://codex.wordpress.org/Settings_API
*/
static function settings() {
register_setting( 'setjr3_settings_group', 'setjr3_checkbox' );
register_setting( 'setjr3_settings_group', 'setjr3_time' );
add_settings_section( 'setjr3_section', '', '', __FILE__ );
add_settings_field( 'checkbox', __( 'Checkbox', 'setjr3' ), array( __CLASS__, 'checkbox' ), __FILE__, 'setjr3_section' );
add_settings_field( 'expiration_time', __( 'Expiration Time', 'setjr3' ), array( __CLASS__, 'expiration_time' ), __FILE__, 'setjr3_section' );
}
/**
* Render Checkbox
*/
static function checkbox() {
$checkbox_value = get_option( 'setjr3_checkbox' );
echo '<input type="checkbox" name="setjr3_checkbox" value="1" ' . checked( 1, $checkbox_value, false ) . '/>&nbsp;&nbsp;<span class="description">This checkbox will uncheck after the expiration time set below.</span>';
}
/**
* Render Date Input
*/
static function expiration_time() {
$exp_time = get_option( 'setjr3_time' );
$exp_time_value = ( ! empty( $exp_time ) ) ? $exp_time : substr_replace( current_time( 'mysql' ), 'T', 10, 1 ) ;
echo '<input name="setjr3_time" type="datetime-local" value="' . $exp_time_value .'" step="1" />';
}
/**
* Render Menu Page
*/
static function render_tutorial_page() {
$curr_time = substr_replace( current_time( 'mysql' ), 'T', 10, 1 );
$exp_time = get_option( 'setjr3_time' );
$exp_time_value = ( ! empty( $exp_time ) ) ? $exp_time : 'Not set' ;
?>
<div class="wrap">
<h2><?php echo __( 'Schedule Event Tuorial', 'setjr3') ?></h2>
<?php if ( isset( $_GET['settings-updated'] ) ) { ?>
<div id="message" class="updated">
<p><strong><?php _e( 'Settings saved.', 'setjr3' ) ?></strong></p>
</div>
<?php } ?>
<p>
<?php _e( 'This is a tutorial demonstrating how to Schedule Events.<br />Begin by checking the checkbox below, then set a time for the checkbox to become unchecked.<br />Then, on the next page refresh after that time, the checkbox will be unchecked.', 'setjr3' ) ?>
</p>
<p>
<code><?php _e( 'Current Time', 'setjr3' ) ?>: <?php echo $curr_time ?></code><br />
<code><?php _e( 'Uncheck Time', 'setjr3' ) ?>: <?php echo $exp_time_value ?></code>
</p>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php settings_fields( 'setjr3_settings_group' ) ?>
<?php do_settings_sections( __FILE__ ) ?>
<?php submit_button() ?>
</form>
</div>
<?php
}
/**
* Cleanup on unistnall
*/
static function uninstall() {
unregister_setting( 'setjr3_settings_group', 'setjr3_checkbox' );
unregister_setting( 'setjr3_settings_group', 'setjr3_time' );
}
}
add_action( 'plugins_loaded', array( 'Schedule_Event_Tutorial_Setup', 'setup' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment