1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Plugin Name: Milestone |
4
|
|
|
Description: Countdown to a specific date. |
5
|
|
|
Version: 1.0 |
6
|
|
|
Author: Automattic |
7
|
|
|
Author URI: http://automattic.com/ |
8
|
|
|
License: GPLv2 or later |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
xmpp_message( '[email protected]', wp_debug_backtrace_summary() ); |
12
|
|
|
|
13
|
|
|
class Milestone { |
14
|
|
|
public static function init() { |
15
|
|
|
add_action( 'widgets_init', array( __class__, 'register_widget' ) ); |
16
|
|
|
} |
17
|
|
|
public static function register_widget() { |
18
|
|
|
register_widget( 'Milestone_Widget' ); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
Milestone::init(); |
23
|
|
|
|
24
|
|
|
class Milestone_Widget extends WP_Widget { |
|
|
|
|
25
|
|
|
private static $dir = null; |
26
|
|
|
private static $url = null; |
27
|
|
|
private static $labels = null; |
28
|
|
|
private static $defaults = null; |
|
|
|
|
29
|
|
|
private static $config_js = null; |
30
|
|
|
|
31
|
|
|
function __construct() { |
32
|
|
|
$widget = array( |
33
|
|
|
'classname' => 'milestone-widget', |
34
|
|
|
'description' => __( 'Display a countdown to a certain date.' ) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$control = array( |
38
|
|
|
'width' => 251, // Chrome needs a little extra room for the date fields. |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
parent::__construct( 'Milestone_Widget', __( 'Milestone' ), $widget, $control ); |
42
|
|
|
|
43
|
|
|
self::$dir = trailingslashit( __DIR__ ); |
44
|
|
|
self::$url = plugin_dir_url( __FILE__ ); |
45
|
|
|
self::$labels = array( |
46
|
|
|
'year' => __( 'year' ), |
47
|
|
|
'years' => __( 'years' ), |
48
|
|
|
'month' => __( 'month' ), |
49
|
|
|
'months' => __( 'months' ), |
50
|
|
|
'day' => __( 'day' ), |
51
|
|
|
'days' => __( 'days' ), |
52
|
|
|
'hour' => __( 'hour' ), |
53
|
|
|
'hours' => __( 'hours' ), |
54
|
|
|
'minute' => __( 'minute' ), |
55
|
|
|
'minutes' => __( 'minutes' ), |
56
|
|
|
'second' => __( 'second' ), |
57
|
|
|
'seconds' => __( 'seconds' ), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
add_action( 'wp_enqueue_scripts', array( __class__, 'enqueue_template' ) ); |
61
|
|
|
add_action( 'admin_enqueue_scripts', array( __class__, 'enqueue_admin' ) ); |
62
|
|
|
add_action( 'wp_footer', array( __class__, 'localize_script' ) ); |
63
|
|
|
|
64
|
|
|
if ( is_active_widget( false, false, $this->id_base, true ) || is_active_widget( false, false, 'monster', true ) ) |
65
|
|
|
add_action( 'wp_head', array( __class__, 'styles_template' ) ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function enqueue_admin( $hook_suffix ) { |
69
|
|
|
if ( 'widgets.php' == $hook_suffix ) |
70
|
|
|
wp_enqueue_style( 'milestone-admin', self::$url . 'style-admin.css', array(), '20111212' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function enqueue_template() { |
74
|
|
|
wp_enqueue_script( 'milestone', self::$url . 'milestone.js', array( 'jquery' ), '20160520', true ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function styles_template() { |
78
|
|
|
global $themecolors; |
79
|
|
|
$colors = wp_parse_args( $themecolors, array( |
80
|
|
|
'bg' => 'ffffff', |
81
|
|
|
'border' => 'cccccc', |
82
|
|
|
'text' => '333333', |
83
|
|
|
) ); |
84
|
|
|
?> |
85
|
|
|
<style> |
86
|
|
|
.milestone-widget { |
87
|
|
|
margin-bottom: 1em; |
88
|
|
|
} |
89
|
|
|
.milestone-content { |
90
|
|
|
line-height: 2; |
91
|
|
|
margin-top: 5px; |
92
|
|
|
max-width: 17em; |
93
|
|
|
padding: 0; |
94
|
|
|
text-align: center; |
95
|
|
|
} |
96
|
|
|
.milestone-header { |
97
|
|
|
background-color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>; |
98
|
|
|
color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>; |
99
|
|
|
line-height: 1.3; |
100
|
|
|
margin: 0; |
101
|
|
|
padding: .8em; |
102
|
|
|
} |
103
|
|
|
.milestone-header .event, |
104
|
|
|
.milestone-header .date { |
105
|
|
|
display: block; |
106
|
|
|
} |
107
|
|
|
.milestone-header .event { |
108
|
|
|
font-size: 120%; |
109
|
|
|
} |
110
|
|
|
.milestone-countdown .difference { |
111
|
|
|
display: block; |
112
|
|
|
font-size: 500%; |
113
|
|
|
font-weight: bold; |
114
|
|
|
line-height: 1.2; |
115
|
|
|
} |
116
|
|
|
.milestone-countdown, |
117
|
|
|
.milestone-message { |
118
|
|
|
background-color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>; |
119
|
|
|
border: 1px solid <?php echo self::sanitize_color_hex( $colors['border'] ); ?>; |
120
|
|
|
border-top: 0; |
121
|
|
|
color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>; |
122
|
|
|
padding-bottom: 1em; |
123
|
|
|
} |
124
|
|
|
.milestone-message { |
125
|
|
|
padding-top: 1em |
126
|
|
|
} |
127
|
|
|
</style> |
128
|
|
|
<?php |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Ensure that a string representing a color in hexadecimal |
133
|
|
|
* notation is safe for use in css and database saves. |
134
|
|
|
* |
135
|
|
|
* @param string Color in hexadecimal notation. "#" may or may not be prepended to the string. |
136
|
|
|
* @return string Color in hexadecimal notation on success - the string "transparent" otherwise. |
137
|
|
|
*/ |
138
|
|
|
public static function sanitize_color_hex( $hex, $prefix = '#' ) { |
139
|
|
|
$hex = trim( $hex ); |
140
|
|
|
|
141
|
|
|
/* Strip recognized prefixes. */ |
142
|
|
|
if ( 0 === strpos( $hex, '#' ) ) |
143
|
|
|
$hex = substr( $hex, 1 ); |
144
|
|
|
elseif ( 0 === strpos( $hex, '%23' ) ) |
145
|
|
|
$hex = substr( $hex, 3 ); |
146
|
|
|
|
147
|
|
|
if ( 0 !== preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) |
148
|
|
|
return $prefix . $hex; |
149
|
|
|
|
150
|
|
|
return 'transparent'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Localize Front-end Script. |
155
|
|
|
* |
156
|
|
|
* Print the javascript configuration array only if the |
157
|
|
|
* current template has an instance of the widget that |
158
|
|
|
* is still counting down. In all other cases, this |
159
|
|
|
* function will dequeue milestone.js. |
160
|
|
|
* |
161
|
|
|
* Hooks into the "wp_footer" action. |
162
|
|
|
*/ |
163
|
|
|
function localize_script() { |
164
|
|
|
if ( empty( self::$config_js['instances'] ) ) { |
165
|
|
|
wp_dequeue_script( 'milestone' ); |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
self::$config_js['labels'] = self::$labels; |
169
|
|
|
wp_localize_script( 'milestone', 'MilestoneConfig', self::$config_js ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Widget |
174
|
|
|
*/ |
175
|
|
|
function widget( $args, $instance ) { |
176
|
|
|
$instance = $this->sanitize_instance( $instance ); |
177
|
|
|
|
178
|
|
|
$milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] ); |
179
|
|
|
$now = (int) current_time( 'timestamp' ); |
180
|
|
|
$diff = (int) floor( $milestone - $now ); |
181
|
|
|
|
182
|
|
|
$number = 0; |
|
|
|
|
183
|
|
|
$label = ''; |
|
|
|
|
184
|
|
|
|
185
|
|
|
if ( 63113852 < $diff ) { // more than 2 years - show in years, one decimal point |
186
|
|
|
$number = round( $diff / 60 / 60 / 24 / 365, 1 ); |
187
|
|
|
$label = self::$labels['years']; |
188
|
|
|
} else if ( 7775999 < $diff ) { // fewer than 2 years - show in months |
189
|
|
|
$number = floor( $diff / 60 / 60 / 24 / 30 ); |
190
|
|
|
$label = ( 1 == $number ) ? self::$labels['month'] : self::$labels['months']; |
191
|
|
|
} else if ( 86399 < $diff ) { // fewer than 3 months - show in days |
192
|
|
|
$number = floor( $diff / 60 / 60 / 24 ) + 1; |
193
|
|
|
$label = ( 1 == $number ) ? self::$labels['day'] : self::$labels['days']; |
194
|
|
|
} else if ( 3599 < $diff ) { // less than 1 day - show in hours |
195
|
|
|
$number = floor( $diff / 60 / 60 ); |
196
|
|
|
$label = ( 1 == $number ) ? self::$labels['hour'] : self::$labels['hours']; |
197
|
|
|
} else if ( 59 < $diff ) { // less than 1 hour - show in minutes |
198
|
|
|
$number = floor( $diff / 60 ) + 1; |
199
|
|
|
$label = ( 1 == $number ) ? self::$labels['minute'] : self::$labels['minutes']; |
200
|
|
|
} else { // less than 1 minute - show in seconds |
201
|
|
|
$number = $diff; |
202
|
|
|
$label = ( 1 == $number ) ? self::$labels['second'] : self::$labels['seconds'] ; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
echo $args['before_widget']; |
206
|
|
|
|
207
|
|
|
$title = apply_filters( 'widget_title', $instance['title'] ); |
208
|
|
|
if ( ! empty( $title ) ) |
209
|
|
|
echo $args['before_title'] . $title . $args['after_title']; |
210
|
|
|
|
211
|
|
|
echo '<div class="milestone-content">'; |
212
|
|
|
|
213
|
|
|
echo '<div class="milestone-header">'; |
214
|
|
|
echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>'; |
215
|
|
|
echo '<span class="date">' . esc_html( date_i18n( __( 'F jS, Y' ), $milestone ) ) . '</span>'; |
216
|
|
|
echo '</div>'; |
217
|
|
|
|
218
|
|
|
if ( 1 > $diff ) { |
219
|
|
|
/* Milestone has past. */ |
220
|
|
|
echo '<div class="milestone-message">' . $instance['message'] . '</div>'; |
221
|
|
|
} else { |
222
|
|
|
/* Countdown to the milestone. */ |
223
|
|
|
echo '<div class="milestone-countdown">' . sprintf( __( '%1$s %2$s to go.' ), |
224
|
|
|
'<span class="difference">' . esc_html( $number ) . '</span>', |
225
|
|
|
'<span class="label">' . esc_html( $label ) . '</span>' |
226
|
|
|
) . '</div>'; |
227
|
|
|
|
228
|
|
|
self::$config_js['instances'][] = array( |
229
|
|
|
'id' => $args['widget_id'], |
230
|
|
|
'diff' => $diff, |
231
|
|
|
'message' => $instance['message'], |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
echo '</div><!--milestone-content-->'; |
236
|
|
|
|
237
|
|
|
echo $args['after_widget']; |
238
|
|
|
stats_extra( 'widget_view', 'milestone' ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Update |
243
|
|
|
*/ |
244
|
|
|
function update( $new_instance, $old_instance ) { |
245
|
|
|
return $this->sanitize_instance( $new_instance ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/* |
249
|
|
|
* Make sure that a number is within a certain range. |
250
|
|
|
* If the number is too small it will become the possible lowest value. |
251
|
|
|
* If the number is too large it will become the possible highest value. |
252
|
|
|
* |
253
|
|
|
* @param int $n The number to check. |
254
|
|
|
* @param int $floor The lowest possible value. |
255
|
|
|
* @param int $ceil The highest possible value. |
256
|
|
|
*/ |
257
|
|
|
function sanitize_range( $n, $floor, $ceil ) { |
258
|
|
|
$n = (int) $n; |
259
|
|
|
if ( $n < $floor ) |
260
|
|
|
$n = $floor; |
261
|
|
|
else if ( $n > $ceil ) |
262
|
|
|
$n = $ceil; |
263
|
|
|
return $n; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/* |
267
|
|
|
* Sanitize an instance of this widget. |
268
|
|
|
* |
269
|
|
|
* Date ranges match the documentation for mktime in the php manual. |
270
|
|
|
* @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters |
271
|
|
|
* |
272
|
|
|
* @uses Milestone_Widget::sanitize_range(). |
273
|
|
|
*/ |
274
|
|
|
function sanitize_instance( $dirty ) { |
275
|
|
|
$now = (int) current_time( 'timestamp' ); |
276
|
|
|
|
277
|
|
|
$dirty = wp_parse_args( $dirty, array( |
278
|
|
|
'title' => '', |
279
|
|
|
'event' => __( 'The Big Day' ), |
280
|
|
|
'message' => __( 'The big day is here.' ), |
281
|
|
|
'day' => date( 'd', $now ), |
282
|
|
|
'month' => date( 'm', $now ), |
283
|
|
|
'year' => date( 'Y', $now ), |
284
|
|
|
'hour' => 0, |
285
|
|
|
'min' => 0, |
286
|
|
|
) ); |
287
|
|
|
|
288
|
|
|
$allowed_tags = array( |
289
|
|
|
'a' => array( 'title' => array(), 'href' => array() ), |
290
|
|
|
'em' => array( 'title' => array() ), |
291
|
|
|
'strong' => array( 'title' => array() ), |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
$clean = array( |
295
|
|
|
'title' => trim( strip_tags( stripslashes( $dirty['title'] ) ) ), |
296
|
|
|
'event' => trim( strip_tags( stripslashes( $dirty['event'] ) ) ), |
297
|
|
|
'message' => wp_kses( $dirty['message'], $allowed_tags ), |
298
|
|
|
'year' => $this->sanitize_range( $dirty['year'], 1901, 2037 ), |
299
|
|
|
'month' => $this->sanitize_range( $dirty['month'], 1, 12 ), |
300
|
|
|
'hour' => $this->sanitize_range( $dirty['hour'], 0, 23 ), |
301
|
|
|
'min' => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ), |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
$clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) ); |
305
|
|
|
|
306
|
|
|
return $clean; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Form |
311
|
|
|
*/ |
312
|
|
|
function form( $instance ) { |
313
|
|
|
$instance = $this->sanitize_instance( $instance ); |
314
|
|
|
?> |
315
|
|
|
|
316
|
|
|
<div class="milestone-widget"> |
317
|
|
|
<p> |
318
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title' ); ?></label> |
319
|
|
|
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
320
|
|
|
</p> |
321
|
|
|
|
322
|
|
|
<p> |
323
|
|
|
<label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Event' ); ?></label> |
324
|
|
|
<input class="widefat" id="<?php echo $this->get_field_id( 'event' ); ?>" name="<?php echo $this->get_field_name( 'event' ); ?>" type="text" value="<?php echo esc_attr( $instance['event'] ); ?>" /> |
325
|
|
|
</p> |
326
|
|
|
|
327
|
|
|
<fieldset> |
328
|
|
|
<legend><?php _e( 'Date and Time' ); ?></legend> |
329
|
|
|
|
330
|
|
|
<label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month' ); ?></label> |
331
|
|
|
<select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php |
332
|
|
|
global $wp_locale; |
333
|
|
|
for ( $i = 1; $i < 13; $i++ ) { |
334
|
|
|
$monthnum = zeroise( $i, 2 ); |
335
|
|
|
echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>'; |
336
|
|
|
} |
337
|
|
|
?></select> |
338
|
|
|
|
339
|
|
|
<label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day' ); ?></label> |
340
|
|
|
<input id="<?php echo $this->get_field_id( 'day' ); ?>" class="day" name="<?php echo $this->get_field_name( 'day' ); ?>" type="text" value="<?php echo esc_attr( $instance['day'] ); ?>">, |
341
|
|
|
|
342
|
|
|
<label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year' ); ?></label> |
343
|
|
|
<input id="<?php echo $this->get_field_id( 'year' ); ?>" class="year" name="<?php echo $this->get_field_name( 'year' ); ?>" type="text" value="<?php echo esc_attr( $instance['year'] ); ?>"> |
344
|
|
|
|
345
|
|
|
@ <label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour' ); ?></label> |
346
|
|
|
<input id="<?php echo $this->get_field_id( 'hour' ); ?>" class="hour" name="<?php echo $this->get_field_name( 'hour' ); ?>" type="text" value="<?php echo esc_attr( $instance['hour'] ); ?>"> |
347
|
|
|
|
348
|
|
|
<label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes' ); ?></label> |
349
|
|
|
: <input id="<?php echo $this->get_field_id( 'min' ); ?>" class="minutes" name="<?php echo $this->get_field_name( 'min' ); ?>" type="text" value="<?php echo esc_attr( $instance['min'] ); ?>"> |
350
|
|
|
</fieldset> |
351
|
|
|
|
352
|
|
|
<p> |
353
|
|
|
<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Message' ); ?></label> |
354
|
|
|
<textarea id="<?php echo $this->get_field_id( 'message' ); ?>" name="<?php echo $this->get_field_name( 'message' ); ?>" class="widefat"><?php echo esc_textarea( $instance['message'] ); ?></textarea> |
355
|
|
|
</p> |
356
|
|
|
</div> |
357
|
|
|
|
358
|
|
|
<?php |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.