Completed
Push — add/sync-partial-sync-checksum... ( 93f4e2...c9ed3f )
by
unknown
253:26 queued 243:12
created

Milestone_Widget::widget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
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: https://automattic.com/
8
License: GPLv2 or later
9
*/
10
11
use Automattic\Jetpack\Assets;
12
13
function jetpack_register_widget_milestone() {
14
	register_widget( 'Milestone_Widget' );
15
}
16
add_action( 'widgets_init', 'jetpack_register_widget_milestone' );
17
18
class Milestone_Widget extends WP_Widget {
19
	private static $dir       = null;
20
	private static $url       = null;
21
	private static $defaults  = null;
22
	private static $config_js = null;
23
24
	/**
25
	 * Available time units sorted in descending order.
26
	 * @var Array
27
	 */
28
	protected $available_units = array(
29
		'years',
30
		'months',
31
		'days',
32
		'hours',
33
		'minutes',
34
		'seconds'
35
	);
36
37
	function __construct() {
38
		$widget = array(
39
			'classname'   => 'milestone-widget',
40
			'description' => __( 'Display a countdown to a certain date.', 'jetpack' ),
41
		);
42
43
		parent::__construct(
44
			'Milestone_Widget',
45
			/** This filter is documented in modules/widgets/facebook-likebox.php */
46
			apply_filters( 'jetpack_widget_name', __( 'Milestone', 'jetpack' ) ),
47
			$widget
48
		);
49
50
		self::$dir = trailingslashit( dirname( __FILE__ ) );
51
		self::$url = plugin_dir_url( __FILE__ );
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(
66
				'milestone-admin-js',
67
				Assets::get_file_url_for_environment(
68
					'_inc/build/widgets/milestone/admin.min.js',
69
					'modules/widgets/milestone/admin.js'
70
				),
71
				array( 'jquery' ),
72
				'20170915',
73
				true
74
			);
75
		}
76
	}
77
78
	public static function enqueue_template() {
79
		if ( Jetpack_AMP_Support::is_amp_request() ) {
80
			return;
81
		}
82
83
		wp_enqueue_script(
84
			'milestone',
85
			Assets::get_file_url_for_environment(
86
				'_inc/build/widgets/milestone/milestone.min.js',
87
				'modules/widgets/milestone/milestone.js'
88
			),
89
			array(),
90
			'20160520',
91
			true
92
		);
93
	}
94
95
	public static function styles_template() {
96
		global $themecolors;
97
		$colors = wp_parse_args( $themecolors, array(
0 ignored issues
show
Documentation introduced by
array('bg' => 'ffffff', ...c', 'text' => '333333') is of type array<string,string,{"bg...ring","text":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
			'bg'     => 'ffffff',
99
			'border' => 'cccccc',
100
			'text'   => '333333',
101
		) );
102
?>
103
<style>
104
.milestone-widget {
105
	margin-bottom: 1em;
106
}
107
.milestone-content {
108
	line-height: 2;
109
	margin-top: 5px;
110
	max-width: 100%;
111
	padding: 0;
112
	text-align: center;
113
}
114
.milestone-header {
115
	background-color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
116
	color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
117
	line-height: 1.3;
118
	margin: 0;
119
	padding: .8em;
120
}
121
.milestone-header .event,
122
.milestone-header .date {
123
	display: block;
124
}
125
.milestone-header .event {
126
	font-size: 120%;
127
}
128
.milestone-countdown .difference {
129
	display: block;
130
	font-size: 500%;
131
	font-weight: bold;
132
	line-height: 1.2;
133
}
134
.milestone-countdown,
135
.milestone-message {
136
	background-color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
137
	border: 1px solid <?php echo self::sanitize_color_hex( $colors['border'] ); ?>;
138
	border-top: 0;
139
	color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
140
	padding-bottom: 1em;
141
}
142
.milestone-message {
143
	padding-top: 1em
144
}
145
</style>
146
<?php
147
	}
148
149
	/**
150
	 * Ensure that a string representing a color in hexadecimal
151
	 * notation is safe for use in css and database saves.
152
	 *
153
	 * @param string Color in hexadecimal notation. "#" may or may not be prepended to the string.
154
	 * @return string Color in hexadecimal notation on success - the string "transparent" otherwise.
155
	 */
156
	public static function sanitize_color_hex( $hex, $prefix = '#' ) {
157
		$hex = trim( $hex );
158
159
		/* Strip recognized prefixes. */
160
		if ( 0 === strpos( $hex, '#' ) ) {
161
			$hex = substr( $hex, 1 );
162
		} elseif ( 0 === strpos( $hex, '%23' ) ) {
163
			$hex = substr( $hex, 3 );
164
		}
165
166
		if ( 0 !== preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
167
			return $prefix . $hex;
168
		}
169
170
		return 'transparent';
171
	}
172
173
	/**
174
	 * Localize Front-end Script.
175
	 *
176
	 * Print the javascript configuration array only if the
177
	 * current template has an instance of the widget that
178
	 * is still counting down. In all other cases, this
179
	 * function will dequeue milestone.js.
180
	 *
181
	 * Hooks into the "wp_footer" action.
182
	 */
183
	function localize_script() {
184
		if ( Jetpack_AMP_Support::is_amp_request() ) {
185
			return;
186
		}
187
188
		if ( empty( self::$config_js['instances'] ) ) {
189
			wp_dequeue_script( 'milestone' );
190
			return;
191
		}
192
		self::$config_js['api_root'] = esc_url_raw( rest_url() );
193
		wp_localize_script( 'milestone', 'MilestoneConfig', self::$config_js );
194
	}
195
196
	/**
197
	 * Widget
198
	 */
199
	function widget( $args, $instance ) {
200
		echo $args['before_widget'];
201
202
		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
203
		$title = apply_filters( 'widget_title', $instance['title'] );
204
		if ( ! empty( $title ) ) {
205
			echo $args['before_title'] . $title . $args['after_title'];
206
		}
207
208
		$data   = $this->get_widget_data( $instance );
209
		$config = array(
210
			'id'      => $args['widget_id'],
211
			'message' => $data['message'],
212
			'refresh' => $data['refresh'],
213
		);
214
215
		/*
216
		 * Sidebars may be configured to not expose the `widget_id`. Example: `twentytwenty` footer areas.
217
		 *
218
		 * We need our own unique identifier.
219
		 */
220
		$config['content_id'] = $args['widget_id'] . '-content';
221
222
		self::$config_js['instances'][] = $config;
223
224
		echo sprintf( '<div id="%s" class="milestone-content">', esc_html( $config['content_id'] ) );
225
226
		echo '<div class="milestone-header">';
227
		echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>';
228
		echo '<span class="date">' . esc_html( date_i18n( get_option( 'date_format' ), $data['milestone'] ) ) . '</span>';
229
		echo '</div>';
230
231
		echo $data['message'];
232
233
		echo '</div><!--milestone-content-->';
234
235
		echo $args['after_widget'];
236
237
		/** This action is documented in modules/widgets/gravatar-profile.php */
238
		do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' );
239
	}
240
241
	function get_widget_data( $instance ) {
242
		$data = array();
243
244
		$instance = $this->sanitize_instance( $instance );
245
246
		$milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] );
247
		$now  = (int) current_time( 'timestamp' );
248
		$type = $instance['type'];
249
250
		if ( 'since' === $type ) {
251
			$diff = (int) floor( $now - $milestone );
252
		} else {
253
			$diff = (int) floor( $milestone - $now );
254
		}
255
256
		$data['diff'] = $diff;
257
		$data['unit'] = $this->get_unit( $diff, $instance['unit'] );
258
259
		// Setting the refresh counter to equal the number of seconds it takes to flip a unit
260
		$refresh_intervals = array(
261
			0, // should be YEAR_IN_SECONDS, but doing setTimeout for a year doesn't seem to be logical
262
			0, // same goes for MONTH_IN_SECONDS,
263
			DAY_IN_SECONDS,
264
			HOUR_IN_SECONDS,
265
			MINUTE_IN_SECONDS,
266
			1
267
		);
268
269
		$data['refresh'] = $refresh_intervals[ array_search( $data['unit'], $this->available_units ) ];
270
		$data['milestone'] = $milestone;
271
272
		if ( ( 1 > $diff ) && ( 'until' === $type ) ) {
273
			$data['message'] = '<div class="milestone-message">' . $instance['message'] . '</div>';
274
			$data['refresh'] = 0; // No need to refresh, the milestone has been reached
275
		} else {
276
			$interval_text = $this->get_interval_in_units( $diff, $data['unit'] );
277
			$interval = (int) $interval_text;
278
279
			if ( 'since' === $type ) {
280
281 View Code Duplication
				switch ( $data['unit'] ) {
282
					case 'years':
283
						$data['message'] = sprintf(
284
							_n(
285
								'<span class="difference">%s</span> <span class="label">year ago.</span>',
286
								'<span class="difference">%s</span> <span class="label">years ago.</span>',
287
								$interval,
288
								'jetpack'
289
							),
290
							$interval_text
291
						);
292
					break;
293
					case 'months':
294
						$data['message'] = sprintf(
295
							_n(
296
								'<span class="difference">%s</span> <span class="label">month ago.</span>',
297
								'<span class="difference">%s</span> <span class="label">months ago.</span>',
298
								$interval,
299
								'jetpack'
300
							),
301
							$interval_text
302
						);
303
					break;
304
					case 'days':
305
						$data['message'] = sprintf(
306
							_n(
307
								'<span class="difference">%s</span> <span class="label">day ago.</span>',
308
								'<span class="difference">%s</span> <span class="label">days ago.</span>',
309
								$interval,
310
								'jetpack'
311
							),
312
							$interval_text
313
						);
314
					break;
315
					case 'hours':
316
						$data['message'] = sprintf(
317
							_n(
318
								'<span class="difference">%s</span> <span class="label">hour ago.</span>',
319
								'<span class="difference">%s</span> <span class="label">hours ago.</span>',
320
								$interval,
321
								'jetpack'
322
							),
323
							$interval_text
324
						);
325
					break;
326
					case 'minutes':
327
						$data['message'] = sprintf(
328
							_n(
329
								'<span class="difference">%s</span> <span class="label">minute ago.</span>',
330
								'<span class="difference">%s</span> <span class="label">minutes ago.</span>',
331
								$interval,
332
								'jetpack'
333
							),
334
							$interval_text
335
						);
336
					break;
337
					case 'seconds':
338
						$data['message'] = sprintf(
339
							_n(
340
								'<span class="difference">%s</span> <span class="label">second ago.</span>',
341
								'<span class="difference">%s</span> <span class="label">seconds ago.</span>',
342
								$interval,
343
								'jetpack'
344
							),
345
							$interval_text
346
						);
347
					break;
348
				}
349
			} else {
350 View Code Duplication
				switch ( $this->get_unit( $diff, $instance['unit'] ) ) {
351
					case 'years':
352
						$data['message'] = sprintf(
353
							_n(
354
								'<span class="difference">%s</span> <span class="label">year to go.</span>',
355
								'<span class="difference">%s</span> <span class="label">years to go.</span>',
356
								$interval,
357
								'jetpack'
358
							),
359
							$interval_text
360
						);
361
					break;
362
					case 'months':
363
						$data['message'] = sprintf(
364
							_n(
365
								'<span class="difference">%s</span> <span class="label">month to go.</span>',
366
								'<span class="difference">%s</span> <span class="label">months to go.</span>',
367
								$interval,
368
								'jetpack'
369
							),
370
							$interval_text
371
						);
372
					break;
373
					case 'days':
374
						$data['message'] = sprintf(
375
							_n(
376
								'<span class="difference">%s</span> <span class="label">day to go.</span>',
377
								'<span class="difference">%s</span> <span class="label">days to go.</span>',
378
								$interval,
379
								'jetpack'
380
							),
381
							$interval_text
382
						);
383
					break;
384
					case 'hours':
385
						$data['message'] = sprintf(
386
							_n(
387
								'<span class="difference">%s</span> <span class="label">hour to go.</span>',
388
								'<span class="difference">%s</span> <span class="label">hours to go.</span>',
389
								$interval,
390
								'jetpack'
391
							),
392
							$interval_text
393
						);
394
					break;
395
					case 'minutes':
396
						$data['message'] = sprintf(
397
							_n(
398
								'<span class="difference">%s</span> <span class="label">minute to go.</span>',
399
								'<span class="difference">%s</span> <span class="label">minutes to go.</span>',
400
								$interval,
401
								'jetpack'
402
							),
403
							$interval_text
404
						);
405
					break;
406
					case 'seconds':
407
						$data['message'] = sprintf(
408
							_n(
409
								'<span class="difference">%s</span> <span class="label">second to go.</span>',
410
								'<span class="difference">%s</span> <span class="label">seconds to go.</span>',
411
								$interval,
412
								'jetpack'
413
							),
414
							$interval_text
415
						);
416
					break;
417
				}
418
			}
419
			$data['message'] = '<div class="milestone-countdown">' . $data['message'] . '</div>';
420
		}
421
422
		return $data;
423
	}
424
425
	/**
426
	 * Return the largest possible time unit that the difference will be displayed in.
427
	 *
428
	 * @param Integer $seconds the interval in seconds
429
	 * @param String $maximum_unit the maximum unit that will be used. Optional.
430
	 * @return String $calculated_unit
431
	 */
432
	protected function get_unit( $seconds, $maximum_unit = 'automatic' ) {
433
		$unit = '';
0 ignored issues
show
Unused Code introduced by
$unit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
434
435
		if ( $seconds >= YEAR_IN_SECONDS * 2 ) {
436
			// more than 2 years - show in years, one decimal point
437
			$unit = 'years';
438
439
		} else if ( $seconds >= YEAR_IN_SECONDS ) {
440
			if ( 'years' === $maximum_unit ) {
441
				$unit = 'years';
442
			} else {
443
				// automatic mode - showing months even if it's between one and two years
444
				$unit = 'months';
445
			}
446
447
		} else if ( $seconds >= MONTH_IN_SECONDS * 3 ) {
448
			// fewer than 2 years - show in months
449
			$unit = 'months';
450
451
		} else if ( $seconds >= MONTH_IN_SECONDS ) {
452
			if ( 'months' === $maximum_unit ) {
453
				$unit = 'months';
454
			} else {
455
				// automatic mode - showing days even if it's between one and three months
456
				$unit = 'days';
457
			}
458
459
		} else if ( $seconds >= DAY_IN_SECONDS - 1 ) {
460
			// fewer than a month - show in days
461
			$unit = 'days';
462
463
		} else if ( $seconds >= HOUR_IN_SECONDS - 1 ) {
464
			// less than 1 day - show in hours
465
			$unit = 'hours';
466
467
		} else if ( $seconds >= MINUTE_IN_SECONDS - 1 ) {
468
			// less than 1 hour - show in minutes
469
			$unit = 'minutes';
470
471
		} else {
472
			// less than 1 minute - show in seconds
473
			$unit = 'seconds';
474
		}
475
476
		$maximum_unit_index = array_search( $maximum_unit, $this->available_units );
477
		$unit_index = array_search( $unit, $this->available_units );
478
479
		if (
480
			false === $maximum_unit_index // the maximum unit parameter is automatic
481
			|| $unit_index > $maximum_unit_index // there is not enough seconds for even one maximum time unit
482
		) {
483
			return $unit;
484
		}
485
		return $maximum_unit;
486
	}
487
488
	/**
489
	 * Returns a time difference value in specified units.
490
	 *
491
	 * @param Integer $seconds
492
	 * @param String $units
493
	 * @return Integer|String $time_in_units
494
	 */
495
	protected function get_interval_in_units( $seconds, $units ) {
496
		switch ( $units ) {
497
			case 'years':
498
				$years = $seconds / YEAR_IN_SECONDS;
499
				$decimals = abs( round( $years, 1 ) - round( $years ) ) > 0 ? 1 : 0;
500
				return number_format_i18n( $years, $decimals );
501
			case 'months':
502
				return (int) ( $seconds / 60 / 60 / 24 / 30 );
503
			case 'days':
504
				return (int) ( $seconds / 60 / 60 / 24 + 1 );
505
			case 'hours':
506
				return (int) ( $seconds / 60 / 60 );
507
			case 'minutes':
508
				return (int) ( $seconds / 60 + 1 );
509
			default:
510
				return $seconds;
511
		}
512
	}
513
514
	/**
515
	 * Update
516
	 */
517
	function update( $new_instance, $old_instance ) {
518
		return $this->sanitize_instance( $new_instance );
519
	}
520
521
	/*
522
	 * Make sure that a number is within a certain range.
523
	 * If the number is too small it will become the possible lowest value.
524
	 * If the number is too large it will become the possible highest value.
525
	 *
526
	 * @param int $n The number to check.
527
	 * @param int $floor The lowest possible value.
528
	 * @param int $ceil The highest possible value.
529
	 */
530
	function sanitize_range( $n, $floor, $ceil ) {
531
		$n = (int) $n;
532
		if ( $n < $floor ) {
533
			$n = $floor;
534
		} elseif ( $n > $ceil ) {
535
			$n = $ceil;
536
		}
537
		return $n;
538
	}
539
540
	/*
541
	 * Sanitize an instance of this widget.
542
	 *
543
	 * Date ranges match the documentation for mktime in the php manual.
544
	 * @see https://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters
545
	 *
546
	 * @uses Milestone_Widget::sanitize_range().
547
	 */
548
	function sanitize_instance( $dirty ) {
549
		$now = (int) current_time( 'timestamp' );
550
551
		$dirty = wp_parse_args( $dirty, array(
0 ignored issues
show
Documentation introduced by
array('title' => '', 'ev...hour' => 0, 'min' => 0) is of type array<string,?,{"title":...eger","min":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
552
			'title'   => '',
553
			'event'   => __( 'The Big Day', 'jetpack' ),
554
			'unit'    => 'automatic',
555
			'type'    => 'until',
556
			'message' => __( 'The big day is here.', 'jetpack' ),
557
			'day'     => date( 'd', $now ),
558
			'month'   => date( 'm', $now ),
559
			'year'    => date( 'Y', $now ),
560
			'hour'    => 0,
561
			'min'     => 0,
562
		) );
563
564
		$allowed_tags = array(
565
			'a'      => array( 'title' => array(), 'href' => array(), 'target' => array() ),
566
			'em'     => array( 'title' => array() ),
567
			'strong' => array( 'title' => array() ),
568
		);
569
570
		$clean = array(
571
			'title'   => trim( strip_tags( stripslashes( $dirty['title'] ) ) ),
572
			'event'   => trim( strip_tags( stripslashes( $dirty['event'] ) ) ),
573
			'unit'    => $dirty['unit'],
574
			'type'    => $dirty['type'],
575
			'message' => wp_kses( $dirty['message'], $allowed_tags ),
576
			'year'    => $this->sanitize_range( $dirty['year'],  1901, 2037 ),
577
			'month'   => $this->sanitize_range( $dirty['month'], 1, 12 ),
578
			'hour'    => $this->sanitize_range( $dirty['hour'],  0, 23 ),
579
			'min'     => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ),
580
		);
581
582
		$clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) );
583
584
		return $clean;
585
	}
586
587
	/**
588
	 * Form
589
	 */
590
	function form( $instance ) {
591
		$instance = $this->sanitize_instance( $instance );
592
593
		$units = array(
594
			'automatic' => _x( 'Automatic', 'Milestone widget: mode in which the date unit is determined automatically', 'jetpack' ),
595
			'years' => _x( 'Years', 'Milestone widget: mode in which the date unit is set to years', 'jetpack' ),
596
			'months' => _x( 'Months', 'Milestone widget: mode in which the date unit is set to months', 'jetpack' ),
597
			'days' => _x( 'Days', 'Milestone widget: mode in which the date unit is set to days', 'jetpack' ),
598
			'hours' => _x( 'Hours', 'Milestone widget: mode in which the date unit is set to hours', 'jetpack' ),
599
		);
600
		?>
601
602
	<div class="milestone-widget">
603
        <p>
604
        	<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'jetpack' ); ?></label>
605
        	<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'] ); ?>" />
606
        </p>
607
608
        <p>
609
        	<label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Description', 'jetpack' ); ?></label>
610
        	<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'] ); ?>" />
611
        </p>
612
613
		<fieldset class="jp-ms-data-time">
614
			<legend><?php esc_html_e( 'Date', 'jetpack' ); ?></legend>
615
616
			<label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month', 'jetpack' ); ?></label>
617
			<select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php
618
				global $wp_locale;
619
				for ( $i = 1; $i < 13; $i++ ) {
620
					$monthnum = zeroise( $i, 2 );
621
					echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>';
622
				}
623
			?></select>
624
625
			<label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day', 'jetpack' ); ?></label>
626
			<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'] ); ?>">,
627
628
			<label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year', 'jetpack' ); ?></label>
629
			<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'] ); ?>">
630
		</fieldset>
631
632
		<fieldset class="jp-ms-data-time">
633
			<legend><?php esc_html_e( 'Time', 'jetpack' ); ?></legend>
634
635
			<label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour', 'jetpack' ); ?></label>
636
			<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'] ); ?>">
637
638
			<label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes', 'jetpack' ); ?></label>
639
640
			<span class="time-separator">:</span>
641
642
			<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'] ); ?>">
643
		</fieldset>
644
645
		<fieldset class="jp-ms-data-unit">
646
			<legend><?php esc_html_e( 'Time Unit', 'jetpack' ); ?></legend>
647
648
			<label for="<?php echo $this->get_field_id( 'unit' ); ?>" class="assistive-text">
649
				<?php _e( 'Time Unit', 'jetpack' ); ?>
650
			</label>
651
			<select id="<?php echo $this->get_field_id( 'unit' ); ?>" class="unit" name="<?php echo $this->get_field_name( 'unit' ); ?>">
652
			<?php
653
				foreach ( $units as $key => $unit ) {
654
					echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $instance['unit'], false ) . '>' . $unit . '</option>';
655
				}
656
			?></select>
657
		</fieldset>
658
659
		<ul class="milestone-type">
660
			<li>
661
				<label>
662
					<input
663
						<?php checked( $instance['type'], 'until' ); ?>
664
						name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>"
665
						type="radio"
666
						value="until"
667
					/>
668
					<?php esc_html_e( 'Until your milestone', 'jetpack' ); ?>
669
				</label>
670
			</li>
671
672
			<li>
673
				<label>
674
					<input
675
						<?php checked( $instance['type'], 'since' ); ?>
676
						name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>"
677
						type="radio"
678
						value="since"
679
					/>
680
					<?php esc_html_e( 'Since your milestone', 'jetpack' ); ?>
681
				</label>
682
			</li>
683
		</ul>
684
685
		<p class="milestone-message-wrapper">
686
			<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Milestone Reached Message', 'jetpack' ); ?></label>
687
			<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>
688
		</p>
689
	</div>
690
691
		<?php
692
    }
693
}
694