3 Plugin Name: danixland CountDown
4 Plugin URI: http://danixland.net/?p=3330
5 Description: A simple plugin that shows a widget with a countdown
7 Author: Danilo 'danix' Macri
8 Author URI: http://danixland.net
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 * Add plugin i18n domain: dnxcd
21 load_plugin_textdomain('dnxcd', plugins_url() . '/danixland-countdown/i18n/', 'danixland-countdown/i18n/');
24 * Function that installs our widget options
27 function dnxcd_options_set() {
28 add_option( 'dnxcd_future_date', '06-05-2020', '', 'yes' );
29 add_option( 'dnxcd_widget_link', '', '', 'yes' );
30 add_option( 'dnxcd_use_style', 1, '', 'yes' );
34 * Function that deletes our widget options
37 function dnxcd_options_unset() {
38 delete_option( 'dnxcd_future_date' );
39 delete_option( 'dnxcd_widget_link' );
40 delete_option( 'dnxcd_use_style' );
44 * Use widget's stylesheet if user wants it
47 function dnxcd_style_handle() {
48 wp_register_style('dnxcd_basic', plugins_url() . '/danixland-countdown/inc/style/basic.css');
49 if( false == get_option('dnxcd_use_style') )
51 wp_enqueue_style('dnxcd_basic');
53 add_action( 'wp_enqueue_scripts', 'dnxcd_style_handle' );
56 * Add function on plugin activation that'll set our plugin options
59 register_activation_hook( __FILE__
, 'dnxcd_options_set' );
62 * Add function on plugin deactivation that'll unset our plugin options
65 register_deactivation_hook( __FILE__
, 'dnxcd_options_unset' );
68 * Add function to widgets_init that'll load our widget.
71 add_action( 'widgets_init', 'danixland_countdown' );
74 * Register our widget.
75 * 'dnx_CountDown' is the widget class used below.
79 function danixland_countdown() {
80 register_widget( 'dnx_CountDown' );
84 * dnx_CountDown class.
85 * This class handles everything that needs to be handled with the widget:
86 * the settings, form, display, and update. Nice!
90 class dnx_CountDown
extends WP_Widget
{
96 public function __construct() {
97 $control_ops = array('width' => 400, 'height' => 350);
99 'dnx-countdown', // id_base
100 __('danixland CountDown', 'dnxcd' ), // Name
101 array( 'description' => __('Use this widget to add a simple Count Down to your Sidebar', 'dnxcd') ),
107 * How to display the widget on the public side of the site.
109 public function widget( $args, $instance ) {
112 /* Our variables from the widget settings. */
113 $title = apply_filters('widget_title', $instance['title'] );
115 /* Before widget (defined by themes). */
118 /* Display the widget title if one was input (before and after defined by themes). */
120 echo $before_title . $title . $after_title;
123 if ( '' != get_option('dnxcd_future_date') ) {
124 echo '<div class="dnx-countdown">';
125 $date = gmdate('U', strtotime( get_option('dnxcd_future_date') ));
126 $diff = $date - gmdate('U');
127 if ( '' != get_option('dnxcd_widget_link') ) {
128 echo '<a href="' . get_option('dnxcd_widget_link') . '">';
129 echo '-' . floor($diff / (24 * 60 * 60));
132 echo '-' . floor($diff / (24 * 60 * 60));
134 echo '</div> <!-- #dnx-countdown -->';
135 echo '<div class="dnx-days">' . __('days to go', 'dnxcd') . '</div>';
137 echo "<div class='dnx-warning'><a href='" . get_admin_url('', 'widgets.php', '') . "' title='" . __('configure widget', 'dnxcd') . "'>" . __('the date is missing<br />configure widget', 'dnxcd') . "</a></div>";
140 /* After widget (defined by themes). */
146 public function update( $new_instance, $old_instance ) {
147 $instance = $old_instance;
149 //Strip tags from title and name to remove HTML
150 $instance['title'] = sanitize_text_field( $new_instance['title'] );
151 $instance['date'] = strip_tags( $new_instance['date'] );
152 $instance['link'] = esc_url( $new_instance['link'], array('http', 'https') );
153 $instance['style'] = (bool) $new_instance['style'];
155 update_option( 'dnxcd_future_date', $instance['date'] );
156 update_option( 'dnxcd_widget_link', $instance['link'] );
157 update_option( 'dnxcd_use_style', $instance['style'] );
163 * Displays just a quick notice with a link to the Settings page
165 public function form( $instance ) {
167 'title' => __( 'Count Down', 'dnxcd' ),
168 'date' => get_option( 'dnxcd_future_date' ),
169 'link' => get_option( 'dnxcd_widget_link' ),
170 'style' => get_option( 'dnxcd_use_style' )
172 $instance = wp_parse_args( (array) $instance, $defaults );
173 extract( $instance, EXTR_SKIP
);
177 <label
for="<?php echo $this->get_field_id( 'title' ); ?>"><?php
_e('Title:', 'dnxcd'); ?
></label
>
178 <input
class="widefat" id
="<?php echo $this->get_field_id( 'title' ); ?>" name
="<?php echo $this->get_field_name( 'title' ); ?>" value
="<?php echo $title; ?>" style
="width:100%;" />
181 <label
for="<?php echo $this->get_field_id( 'date' ); ?>"><?php
_e('Future Date (dd-mm-yyyy):', 'dnxcd'); ?
></label
>
182 <input type
="date" class="widefat" id
="<?php echo $this->get_field_id( 'date' ); ?>" name
="<?php echo $this->get_field_name( 'date' ); ?>" value
="<?php echo $date ?>" style
="width:100%;" />
183 <label
for="<?php echo $this->get_field_id( 'link' ); ?>"><?php
_e('Link (only http/https allowed):', 'dnxcd'); ?
></label
>
184 <input
class="widefat" id
="<?php echo $this->get_field_id( 'link' ); ?>" name
="<?php echo $this->get_field_name( 'link' ); ?>" value
="<?php echo $link; ?>" style
="width:100%;" />
187 <input
class="checkbox" type
="checkbox" <?php
checked( $style, true ); ?
> id
="<?php echo $this->get_field_id( 'style' ); ?>" name
="<?php echo $this->get_field_name( 'style' ); ?>" />
188 <label
for="<?php echo $this->get_field_id( 'style' ); ?>"><?php
_e('Enable Widget Stylesheet?', 'dnxcd'); ?
></label
>