Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created August 19, 2011 19:06
Show Gist options
  • Save trepmal/1157711 to your computer and use it in GitHub Desktop.
Save trepmal/1157711 to your computer and use it in GitHub Desktop.
XKCD Timer WordPress Widget
<?php
/*
Plugin Name: XKCD Timer
Plugin URI:
Description: Pointless timer inspired by http://xkcd.com/363/ (forked from http://wordpress.org/extend/plugins/wp-timer/) Screenshot: http://screencast.com/t/V5JeD4GQot
Version: 1
Author: Kailey Lampert
Author URI: http://kaileylampert.com
Copyright (C) 2011 Kailey Lampert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
*/
add_action( 'widgets_init', 'xkcd_timer_load' );
function xkcd_timer_load() {
register_widget( 'XKCD_Timer' );
}
class XKCD_Timer extends WP_Widget {
function XKCD_Timer() {
$widget_ops = array('classname' => 'xkcd_timer', 'description' => __( '' ) );
$control_ops = array( 'width' => 300, 'id_base' => 'xkcd_timer' );
parent::WP_Widget( 'xkcd_timer', __( 'XKCD Timer', 'xkcd_timer' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
extract( $instance, EXTR_SKIP );
echo $before_widget;
// $instance['title'] = empty($instance['title_url']) ? $instance['title'] : '<a href="'. $instance['title_url'] .'">'. $instance['title'] .'</a>';
// echo $instance['hide_title'] ? '' : $before_title . $instance['title'] . $after_title;
$now = time();
$diff = $now - $start;
$units = $diff / $interval;
$floor = floor($units);
if ($interval == '60') {
$time = 'minute';
}
elseif ($interval == "3600") {
$time = 'hour';
}
elseif ($interval == "86400") {
$time = 'day';
}
echo $before_title;
$format = str_replace( '%number%', $floor, $format );
$format = str_replace( '%unit%', $time. ($floor == '1' ? '' : 's'), $format );
echo stripslashes( $format );
echo $after_title;
echo $after_widget;
} //end widget()
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['hide_title'] = (bool) $new_instance['hide_title'] ? 1 : 0;
$instance['interval'] = intval( $new_instance['interval'] );
$instance['format'] = wp_filter_post_kses( $new_instance['format'] );
$instance['reset'] = (bool) $new_instance['reset'] ? 1 : 0;
$instance['start'] = intval( $new_instance['start'] );
if ($instance['reset']) {
$instance['start'] = time();
}
return $instance;
} //end update()
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'XKCD Timer', 'hide_title' => 0, 'interval' => 3600, 'format' => "It has been <span style='color: #c55;'>%number% %unit%</span> since this timer was reset", 'start' => time(), 'reset' => 0 ) );
extract( $instance );
?>
<!--
<p style="width:63%;float:left;">
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p style="width:33%;float:right;padding-top:20px;height:20px;">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_title'); ?>" name="<?php echo $this->get_field_name('hide_title'); ?>"<?php checked( $hide_title ); ?> />
<label for="<?php echo $this->get_field_id('hide_title'); ?>"><?php _e('Hide Title?' );?></label>
</p>
-->
<p><label>Timer Interval</label>
<select id="<?php echo $this->get_field_id('interval'); ?>" name="<?php echo $this->get_field_name('interval'); ?>">
<option value="86400" <?php selected($interval, '86400'); ?>>days</option>
<option value="3600" <?php selected($interval, '3600'); ?>>hours</option>
<option value="60" <?php selected($interval, '60'); ?>>minutes</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'format' ); ?>"><?php _e( 'Format:' );?>
<!--
<input class="widefat" id="<?php echo $this->get_field_id('format'); ?>" name="<?php echo $this->get_field_name('format'); ?>" type="text" value="<?php echo apply_filters( 'the_editor_content', $format ); ?>" />
-->
</label>
<textarea class="widefat" id="<?php echo $this->get_field_id('format'); ?>" name="<?php echo $this->get_field_name('format'); ?>"><?php echo stripslashes( $format ); ?></textarea>
<small>Use <code>%number&</code> and <code>%unit&</code> placeholders</small>
</p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('reset'); ?>" name="<?php echo $this->get_field_name('reset'); ?>" />
<label for="<?php echo $this->get_field_id('reset'); ?>"><?php _e('Reset timer on save?' );?></label>
<input type="hidden" id="<?php echo $this->get_field_id('start'); ?>" name="<?php echo $this->get_field_name('start'); ?>" value="<?php echo $start; ?>" />
<?php
} //end form()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment