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