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