initial commit
[danixland-countdown.git] / danixland-countdown.php
CommitLineData
8da42654 1<?php\r
2/*\r
3Plugin Name: danixland CountDown\r
4Plugin URI: http://danixland.net/?p=3330 \r
5Description: A simple plugin that shows a widget with a countdown\r
6Version: 0.4\r
7Author: Danilo 'danix' Macri\r
8Author URI: http://danixland.net\r
9Text Domain: dnxcd\r
10\r
11This program is distributed in the hope that it will be useful,\r
12but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
14*/\r
15\r
16\r
17/**\r
18 * Add plugin i18n domain: dnxcd\r
19 * @since 0.1\r
20 */\r
21load_plugin_textdomain('dnxcd', plugins_url() . '/danixland-countdown/i18n/', 'danixland-countdown/i18n/');\r
22\r
23/**\r
24 * Function that installs our widget options\r
25 * @since 0.1\r
26 */\r
27function dnxcd_options_set() {\r
28 add_option( 'dnxcd_future_date', '06-05-2020', '', 'yes' );\r
29 add_option( 'dnxcd_widget_link', '', '', 'yes' );\r
30 add_option( 'dnxcd_use_style', 1, '', 'yes' );\r
31}\r
32\r
33/**\r
34 * Function that deletes our widget options\r
35 * @since 0.3\r
36 */\r
37function dnxcd_options_unset() {\r
38 delete_option( 'dnxcd_future_date' );\r
39 delete_option( 'dnxcd_widget_link' );\r
40 delete_option( 'dnxcd_use_style' );\r
41}\r
42\r
43/**\r
44 * Use widget's stylesheet if user wants it\r
45 * @since 0.3\r
46 */\r
47function dnxcd_style_handle() {\r
48 wp_register_style('dnxcd_basic', plugins_url() . '/danixland-countdown/inc/style/basic.css');\r
49 if( false == get_option('dnxcd_use_style') )\r
50 return;\r
51 wp_enqueue_style('dnxcd_basic');\r
52}\r
53add_action( 'wp_enqueue_scripts', 'dnxcd_style_handle' );\r
54\r
55/**\r
56 * Add function on plugin activation that'll set our plugin options\r
57 * @since 0.1\r
58 */\r
59register_activation_hook( __FILE__, 'dnxcd_options_set' );\r
60\r
61/**\r
62 * Add function on plugin deactivation that'll unset our plugin options\r
63 * @since 0.3\r
64 */\r
65register_deactivation_hook( __FILE__, 'dnxcd_options_unset' );\r
66\r
67/**\r
68 * Add function to widgets_init that'll load our widget.\r
69 * @since 0.1\r
70 */\r
71add_action( 'widgets_init', 'danixland_countdown' );\r
72\r
73/**\r
74 * Register our widget.\r
75 * 'dnx_CountDown' is the widget class used below.\r
76 *\r
77 * @since 0.1\r
78 */\r
79function danixland_countdown() {\r
80 register_widget( 'dnx_CountDown' );\r
81}\r
82\r
83/**\r
84 * dnx_CountDown class.\r
85 * This class handles everything that needs to be handled with the widget:\r
86 * the settings, form, display, and update. Nice!\r
87 *\r
88 * @since 0.1\r
89 */\r
90class dnx_CountDown extends WP_Widget {\r
91\r
92\r
93 /**\r
94 * Widget setup.\r
95 */\r
96 public function __construct() {\r
97 $control_ops = array('width' => 400, 'height' => 350);\r
98 parent::__construct(\r
99 'dnx-countdown', // id_base\r
100 __('danixland CountDown', 'dnxcd' ), // Name\r
101 array( 'description' => __('Use this widget to add a simple Count Down to your Sidebar', 'dnxcd') ),\r
102 $control_ops\r
103 );\r
104 }\r
105\r
106 /**\r
107 * How to display the widget on the public side of the site.\r
108 */\r
109 public function widget( $args, $instance ) {\r
110 extract( $args );\r
111\r
112 /* Our variables from the widget settings. */\r
113 $title = apply_filters('widget_title', $instance['title'] );\r
114\r
115 /* Before widget (defined by themes). */\r
116 echo $before_widget;\r
117\r
118 /* Display the widget title if one was input (before and after defined by themes). */\r
119 if ( $title ) {\r
120 echo $before_title . $title . $after_title;\r
121 }\r
122\r
123 if ( '' != get_option('dnxcd_future_date') ) {\r
124 echo '<div class="dnx-countdown">';\r
125 $date = gmdate('U', strtotime( get_option('dnxcd_future_date') ));\r
126 $diff = $date - gmdate('U');\r
127 if ( '' != get_option('dnxcd_widget_link') ) {\r
128 echo '<a href="' . get_option('dnxcd_widget_link') . '">';\r
129 echo '-' . floor($diff / (24 * 60 * 60));\r
130 echo '</a>';\r
131 } else {\r
132 echo '-' . floor($diff / (24 * 60 * 60));\r
133 }\r
134 echo '</div> <!-- #dnx-countdown -->';\r
135 echo '<div class="dnx-days">' . __('days to go', 'dnxcd') . '</div>'; \r
136 } else {\r
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>";\r
138 }\r
139\r
140 /* After widget (defined by themes). */\r
141 echo $after_widget;\r
142 }\r
143\r
144 //Update the widget \r
145 \r
146 public function update( $new_instance, $old_instance ) {\r
147 $instance = $old_instance;\r
148\r
149 //Strip tags from title and name to remove HTML \r
150 $instance['title'] = sanitize_text_field( $new_instance['title'] );\r
151 $instance['date'] = strip_tags( $new_instance['date'] );\r
152 $instance['link'] = esc_url( $new_instance['link'], array('http', 'https') );\r
153 $instance['style'] = (bool) $new_instance['style'];\r
154\r
155 update_option( 'dnxcd_future_date', $instance['date'] );\r
156 update_option( 'dnxcd_widget_link', $instance['link'] );\r
157 update_option( 'dnxcd_use_style', $instance['style'] );\r
158 \r
159 return $instance;\r
160 }\r
161\r
162 /**\r
163 * Displays just a quick notice with a link to the Settings page\r
164 */\r
165 public function form( $instance ) {\r
166 $defaults = array( \r
167 'title' => __( 'Count Down', 'dnxcd' ),\r
168 'date' => get_option( 'dnxcd_future_date' ),\r
169 'link' => get_option( 'dnxcd_widget_link' ),\r
170 'style' => get_option( 'dnxcd_use_style' )\r
171 );\r
172 $instance = wp_parse_args( (array) $instance, $defaults );\r
173 extract( $instance, EXTR_SKIP );\r
174 ?>\r
175\r
176 <p>\r
177 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'dnxcd'); ?></label>\r
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%;" />\r
179 </p>\r
180 <p>\r
181 <label for="<?php echo $this->get_field_id( 'date' ); ?>"><?php _e('Future Date (dd-mm-yyyy):', 'dnxcd'); ?></label>\r
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%;" />\r
183 <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e('Link (only http/https allowed):', 'dnxcd'); ?></label>\r
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%;" />\r
185 </p>\r
186 <p>\r
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' ); ?>" />\r
188 <label for="<?php echo $this->get_field_id( 'style' ); ?>"><?php _e('Enable Widget Stylesheet?', 'dnxcd'); ?></label>\r
189 </p>\r
190 <?php\r
191 }\r
192}\r
193\r
194?>\r