initial commit
[danixland-countdown.git] / danixland-countdown.php
1 <?php
2 /*
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
6 Version: 0.4
7 Author: Danilo 'danix' Macri
8 Author URI: http://danixland.net
9 Text Domain: dnxcd
10
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.
14 */
15
16
17 /**
18 * Add plugin i18n domain: dnxcd
19 * @since 0.1
20 */
21 load_plugin_textdomain('dnxcd', plugins_url() . '/danixland-countdown/i18n/', 'danixland-countdown/i18n/');
22
23 /**
24 * Function that installs our widget options
25 * @since 0.1
26 */
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' );
31 }
32
33 /**
34 * Function that deletes our widget options
35 * @since 0.3
36 */
37 function dnxcd_options_unset() {
38 delete_option( 'dnxcd_future_date' );
39 delete_option( 'dnxcd_widget_link' );
40 delete_option( 'dnxcd_use_style' );
41 }
42
43 /**
44 * Use widget's stylesheet if user wants it
45 * @since 0.3
46 */
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') )
50 return;
51 wp_enqueue_style('dnxcd_basic');
52 }
53 add_action( 'wp_enqueue_scripts', 'dnxcd_style_handle' );
54
55 /**
56 * Add function on plugin activation that'll set our plugin options
57 * @since 0.1
58 */
59 register_activation_hook( __FILE__, 'dnxcd_options_set' );
60
61 /**
62 * Add function on plugin deactivation that'll unset our plugin options
63 * @since 0.3
64 */
65 register_deactivation_hook( __FILE__, 'dnxcd_options_unset' );
66
67 /**
68 * Add function to widgets_init that'll load our widget.
69 * @since 0.1
70 */
71 add_action( 'widgets_init', 'danixland_countdown' );
72
73 /**
74 * Register our widget.
75 * 'dnx_CountDown' is the widget class used below.
76 *
77 * @since 0.1
78 */
79 function danixland_countdown() {
80 register_widget( 'dnx_CountDown' );
81 }
82
83 /**
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!
87 *
88 * @since 0.1
89 */
90 class dnx_CountDown extends WP_Widget {
91
92
93 /**
94 * Widget setup.
95 */
96 public function __construct() {
97 $control_ops = array('width' => 400, 'height' => 350);
98 parent::__construct(
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') ),
102 $control_ops
103 );
104 }
105
106 /**
107 * How to display the widget on the public side of the site.
108 */
109 public function widget( $args, $instance ) {
110 extract( $args );
111
112 /* Our variables from the widget settings. */
113 $title = apply_filters('widget_title', $instance['title'] );
114
115 /* Before widget (defined by themes). */
116 echo $before_widget;
117
118 /* Display the widget title if one was input (before and after defined by themes). */
119 if ( $title ) {
120 echo $before_title . $title . $after_title;
121 }
122
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));
130 echo '</a>';
131 } else {
132 echo '-' . floor($diff / (24 * 60 * 60));
133 }
134 echo '</div> <!-- #dnx-countdown -->';
135 echo '<div class="dnx-days">' . __('days to go', 'dnxcd') . '</div>';
136 } else {
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>";
138 }
139
140 /* After widget (defined by themes). */
141 echo $after_widget;
142 }
143
144 //Update the widget
145
146 public function update( $new_instance, $old_instance ) {
147 $instance = $old_instance;
148
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'];
154
155 update_option( 'dnxcd_future_date', $instance['date'] );
156 update_option( 'dnxcd_widget_link', $instance['link'] );
157 update_option( 'dnxcd_use_style', $instance['style'] );
158
159 return $instance;
160 }
161
162 /**
163 * Displays just a quick notice with a link to the Settings page
164 */
165 public function form( $instance ) {
166 $defaults = array(
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' )
171 );
172 $instance = wp_parse_args( (array) $instance, $defaults );
173 extract( $instance, EXTR_SKIP );
174 ?>
175
176 <p>
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%;" />
179 </p>
180 <p>
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%;" />
185 </p>
186 <p>
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>
189 </p>
190 <?php
191 }
192 }
193
194 ?>