Completed
Push — fix/milestone-widget-i18n ( 011dc0 )
by
unknown
25:08 queued 17:27
created

Milestone_Widget   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 610
Duplicated Lines 22.3 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 136
loc 610
rs 6.6804
c 0
b 0
f 0
wmc 55
lcom 2
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 4
A enqueue_admin() 0 6 2
A enqueue_template() 0 3 1
A styles_template() 0 53 1
A sanitize_color_hex() 0 16 4
A localize_script() 0 8 2
B widget() 0 32 2
D get_widget_data() 136 182 17
C get_unit() 0 28 8
B get_interval_in_units() 0 16 6
A update() 0 3 1
A sanitize_range() 0 9 3
B sanitize_instance() 0 38 1
B form() 0 102 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Milestone_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Milestone_Widget, and based on these observations, apply Extract Interface, too.

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 $defaults  = null;
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
	private static $config_js = null;
21
22
	/**
23
	 * Available time units sorted in descending order.
24
	 * @var Array
25
	 */
26
	protected $available_units = array(
27
		'years',
28
		'months',
29
		'days',
30
		'hours',
31
		'minutes',
32
		'seconds'
33
	);
34
35
	function __construct() {
36
		$widget = array(
37
			'classname'   => 'milestone-widget',
38
			'description' => __( 'Display a countdown to a certain date.', 'jetpack' ),
39
		);
40
41
		parent::__construct(
42
			'Milestone_Widget',
43
			/** This filter is documented in modules/widgets/facebook-likebox.php */
44
			apply_filters( 'jetpack_widget_name', __( 'Milestone', 'jetpack' ) ),
45
			$widget
46
		);
47
48
		self::$dir = trailingslashit( dirname( __FILE__ ) );
49
		self::$url = plugin_dir_url( __FILE__ );
50
51
		add_action( 'wp_enqueue_scripts', array( __class__, 'enqueue_template' ) );
52
		add_action( 'admin_enqueue_scripts', array( __class__, 'enqueue_admin' ) );
53
		add_action( 'wp_footer', array( $this, 'localize_script' ) );
54
55
		if ( is_active_widget( false, false, $this->id_base, true ) || is_active_widget( false, false, 'monster', true ) || is_customize_preview() ) {
56
			add_action( 'wp_head', array( __class__, 'styles_template' ) );
57
		}
58
	}
59
60
	public static function enqueue_admin( $hook_suffix ) {
61
		if ( 'widgets.php' == $hook_suffix ) {
62
			wp_enqueue_style( 'milestone-admin', self::$url . 'style-admin.css', array(), '20161215' );
63
			wp_enqueue_script( 'milestone-admin-js', self::$url . 'admin.js', array( 'jquery' ), '20170915', true );
64
		}
65
	}
66
67
	public static function enqueue_template() {
68
		wp_enqueue_script( 'milestone', self::$url . 'milestone.js', array( 'jquery' ), '20160520', true );
69
	}
70
71
	public static function styles_template() {
72
		global $themecolors;
73
		$colors = wp_parse_args( $themecolors, array(
74
			'bg'     => 'ffffff',
75
			'border' => 'cccccc',
76
			'text'   => '333333',
77
		) );
78
?>
79
<style>
80
.milestone-widget {
81
	margin-bottom: 1em;
82
}
83
.milestone-content {
84
	line-height: 2;
85
	margin-top: 5px;
86
	max-width: 100%;
87
	padding: 0;
88
	text-align: center;
89
}
90
.milestone-header {
91
	background-color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
92
	color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
93
	line-height: 1.3;
94
	margin: 0;
95
	padding: .8em;
96
}
97
.milestone-header .event,
98
.milestone-header .date {
99
	display: block;
100
}
101
.milestone-header .event {
102
	font-size: 120%;
103
}
104
.milestone-countdown .difference {
105
	display: block;
106
	font-size: 500%;
107
	font-weight: bold;
108
	line-height: 1.2;
109
}
110
.milestone-countdown,
111
.milestone-message {
112
	background-color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
113
	border: 1px solid <?php echo self::sanitize_color_hex( $colors['border'] ); ?>;
114
	border-top: 0;
115
	color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
116
	padding-bottom: 1em;
117
}
118
.milestone-message {
119
	padding-top: 1em
120
}
121
</style>
122
<?php
123
	}
124
125
	/**
126
	 * Ensure that a string representing a color in hexadecimal
127
	 * notation is safe for use in css and database saves.
128
	 *
129
	 * @param string Color in hexadecimal notation. "#" may or may not be prepended to the string.
130
	 * @return string Color in hexadecimal notation on success - the string "transparent" otherwise.
131
	 */
132
	public static function sanitize_color_hex( $hex, $prefix = '#' ) {
133
		$hex = trim( $hex );
134
135
		/* Strip recognized prefixes. */
136
		if ( 0 === strpos( $hex, '#' ) ) {
137
			$hex = substr( $hex, 1 );
138
		} elseif ( 0 === strpos( $hex, '%23' ) ) {
139
			$hex = substr( $hex, 3 );
140
		}
141
142
		if ( 0 !== preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
143
			return $prefix . $hex;
144
		}
145
146
		return 'transparent';
147
	}
148
149
	/**
150
	 * Localize Front-end Script.
151
	 *
152
	 * Print the javascript configuration array only if the
153
	 * current template has an instance of the widget that
154
	 * is still counting down. In all other cases, this
155
	 * function will dequeue milestone.js.
156
	 *
157
	 * Hooks into the "wp_footer" action.
158
	 */
159
	function localize_script() {
160
		if ( empty( self::$config_js['instances'] ) ) {
161
			wp_dequeue_script( 'milestone' );
162
			return;
163
		}
164
		self::$config_js['api_root'] = esc_url_raw( rest_url() );
165
		wp_localize_script( 'milestone', 'MilestoneConfig', self::$config_js );
166
	}
167
168
	/**
169
	 * Widget
170
	 */
171
	function widget( $args, $instance ) {
172
		echo $args['before_widget'];
173
174
		$title = apply_filters( 'widget_title', $instance['title'] );
175
		if ( ! empty( $title ) ) {
176
			echo $args['before_title'] . $title . $args['after_title'];
177
		}
178
179
		$data = $this->get_widget_data( $instance );
180
181
		self::$config_js['instances'][] = array(
182
			'id'      => $args['widget_id'],
183
			'message' => $data['message'],
184
			'refresh' => $data['refresh']
185
		);
186
187
		echo '<div class="milestone-content">';
188
189
		echo '<div class="milestone-header">';
190
		echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>';
191
		echo '<span class="date">' . esc_html( date_i18n( get_option( 'date_format' ), $data['milestone'] ) ) . '</span>';
192
		echo '</div>';
193
194
		echo '<div class="milestone-message">' . $data['message'] . '</div>';
195
196
		echo '</div><!--milestone-content-->';
197
198
		echo $args['after_widget'];
199
200
		/** This action is documented in modules/widgets/gravatar-profile.php */
201
		do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' );
202
	}
203
204
	function get_widget_data( $instance ) {
205
		$data = array();
206
207
		$instance = $this->sanitize_instance( $instance );
208
209
		$milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] );
210
		$now  = (int) current_time( 'timestamp' );
211
		$type = $instance['type'];
212
213
		if ( 'since' === $type ) {
214
			$diff = (int) floor( $now - $milestone );
215
		} else {
216
			$diff = (int) floor( $milestone - $now );
217
		}
218
219
		$data['diff'] = $diff;
220
		$data['unit'] = $this->get_unit( $diff, $instance['unit'] );
221
222
		// Setting the refresh counter to equal the number of seconds it takes to flip a unit
223
		$refresh_intervals = array(
224
			0, // should be YEAR_IN_SECONDS, but doing setTimeout for a year doesn't seem to be logical
225
			0, // same goes for MONTH_IN_SECONDS,
226
			DAY_IN_SECONDS,
227
			HOUR_IN_SECONDS,
228
			MINUTE_IN_SECONDS,
229
			1
230
		);
231
232
		$data['refresh'] = $refresh_intervals[ array_search( $data['unit'], $this->available_units ) ];
233
		$data['milestone'] = $milestone;
234
235
		if ( ( 1 > $diff ) && ( 'until' === $type ) ) {
236
			$data['message'] = $instance['message'];
237
			$data['refresh'] = 0; // No need to refresh, the milestone has been reached
238
		} else {
239
			$interval_text = $this->get_interval_in_units( $diff, $data['unit'] );
240
			$interval = intval( $interval_text );
241
242
			if ( 'since' === $type ) {
243
244 View Code Duplication
				switch ( $data['unit'] ) {
245
					case 'years':
246
						$data['message'] = sprintf(
247
							_n(
248
								'<span class="difference">%1$s</span> <span class="label">year ago.</span>',
249
								'<span class="difference">%1$s</span> <span class="label">years ago.</span>',
250
								$interval,
251
								'jetpack'
252
							),
253
							$interval_text
254
						);
255
					break;
256
					case 'months':
257
						$data['message'] = sprintf(
258
							_n(
259
								'<span class="difference">%1$s</span> <span class="label">month ago.</span>',
260
								'<span class="difference">%1$s</span> <span class="label">months ago.</span>',
261
								$interval,
262
								'jetpack'
263
							),
264
							$interval_text
265
						);
266
					break;
267
					case 'days':
268
						$data['message'] = sprintf(
269
							_n(
270
								'<span class="difference">%1$s</span> <span class="label">day ago.</span>',
271
								'<span class="difference">%1$s</span> <span class="label">days ago.</span>',
272
								$interval,
273
								'jetpack'
274
							),
275
							$interval_text
276
						);
277
					break;
278
					case 'hours':
279
						$data['message'] = sprintf(
280
							_n(
281
								'<span class="difference">%1$s</span> <span class="label">hour ago.</span>',
282
								'<span class="difference">%1$s</span> <span class="label">hours ago.</span>',
283
								$interval,
284
								'jetpack'
285
							),
286
							$interval_text
287
						);
288
					break;
289
					case 'minutes':
290
						$data['message'] = sprintf(
291
							_n(
292
								'<span class="difference">%1$s</span> <span class="label">minute ago.</span>',
293
								'<span class="difference">%1$s</span> <span class="label">minutes ago.</span>',
294
								$interval,
295
								'jetpack'
296
							),
297
							$interval_text
298
						);
299
					break;
300
					case 'seconds':
301
						$data['message'] = sprintf(
302
							_n(
303
								'<span class="difference">%1$s</span> <span class="label">second ago.</span>',
304
								'<span class="difference">%1$s</span> <span class="label">seconds ago.</span>',
305
								$interval,
306
								'jetpack'
307
							),
308
							$interval_text
309
						);
310
					break;
311
				}
312
			} else {
313 View Code Duplication
				switch ( $this->get_unit( $diff, $instance['unit'] ) ) {
314
					case 'years':
315
						$data['message'] = sprintf(
316
							_n(
317
								'<span class="difference">%1$s</span> <span class="label">year to go.</span>',
318
								'<span class="difference">%1$s</span> <span class="label">years to go.</span>',
319
								$interval,
320
								'jetpack'
321
							),
322
							$interval_text
323
						);
324
					break;
325
					case 'months':
326
						$data['message'] = sprintf(
327
							_n(
328
								'<span class="difference">%1$s</span> <span class="label">month to go.</span>',
329
								'<span class="difference">%1$s</span> <span class="label">months to go.</span>',
330
								$interval,
331
								'jetpack'
332
							),
333
							$interval_text
334
						);
335
					break;
336
					case 'days':
337
						$data['message'] = sprintf(
338
							_n(
339
								'<span class="difference">%1$s</span> <span class="label">day to go.</span>',
340
								'<span class="difference">%1$s</span> <span class="label">days to go.</span>',
341
								$interval,
342
								'jetpack'
343
							),
344
							$interval_text
345
						);
346
					break;
347
					case 'hours':
348
						$data['message'] = sprintf(
349
							_n(
350
								'<span class="difference">%1$s</span> <span class="label">hour to go.</span>',
351
								'<span class="difference">%1$s</span> <span class="label">hours to go.</span>',
352
								$interval,
353
								'jetpack'
354
							),
355
							$interval_text
356
						);
357
					break;
358
					case 'minutes':
359
						$data['message'] = sprintf(
360
							_n(
361
								'<span class="difference">%1$s</span> <span class="label">minute to go.</span>',
362
								'<span class="difference">%1$s</span> <span class="label">minutes to go.</span>',
363
								$interval,
364
								'jetpack'
365
							),
366
							$interval_text
367
						);
368
					break;
369
					case 'seconds':
370
						$data['message'] = sprintf(
371
							_n(
372
								'<span class="difference">%1$s</span> <span class="label">second to go.</span>',
373
								'<span class="difference">%1$s</span> <span class="label">seconds to go.</span>',
374
								$interval,
375
								'jetpack'
376
							),
377
							$interval_text
378
						);
379
					break;
380
				}
381
			}
382
		}
383
384
		return $data;
385
	}
386
387
	/**
388
	 * Return the largest possible time unit that the difference will be displayed in.
389
	 *
390
	 * @param Integer $seconds the interval in seconds
391
	 * @param String $maximum_unit the maximum unit that will be used. Optional.
392
	 * @return String $calculated_unit
393
	 */
394
	protected function get_unit( $seconds, $maximum_unit = 'automatic' ) {
395
		$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...
396
397
		if ( $seconds >= 63113852 ) { // more than 2 years - show in years, one decimal point
398
			$unit = 'years';
399
		} else if ( $seconds >= 7775999 ) { // fewer than 2 years - show in months
400
			$unit = 'months';
401
		} else if ( $seconds >= DAY_IN_SECONDS - 1 ) { // fewer than 3 months - show in days
402
			$unit = 'days';
403
		} else if ( $seconds >= HOUR_IN_SECONDS - 1 ) { // less than 1 day - show in hours
404
			$unit = 'hours';
405
		} else if ( $seconds >= MINUTE_IN_SECONDS - 1 ) { // less than 1 hour - show in minutes
406
			$unit = 'minutes';
407
		} else { // less than 1 minute - show in seconds
408
			$unit = 'seconds';
409
		}
410
411
		$maximum_unit_index = array_search( $maximum_unit, $this->available_units );
412
		$unit_index = array_search( $unit, $this->available_units );
413
414
		if (
415
			false === $maximum_unit_index // the maximum unit parameter is automatic
416
			|| $unit_index > $maximum_unit_index // there is not enough seconds for even one maximum time unit
417
		) {
418
			return $unit;
419
		}
420
		return $maximum_unit;
421
	}
422
423
	/**
424
	 * Returns a time difference value in specified units.
425
	 *
426
	 * @param Integer $seconds
427
	 * @param String $units
428
	 * @return Integer|String $time_in_units
429
	 */
430
	protected function get_interval_in_units( $seconds, $units ) {
431
		switch ( $units ) {
432
			case 'years':
433
				return (int) ( $seconds / 60 / 60 / 24 / 365 );
434
			case 'months':
435
				return (int) ( $seconds / 60 / 60 / 24 / 30 );
436
			case 'days':
437
				return (int) ( $seconds / 60 / 60 / 24 + 1 );
438
			case 'hours':
439
				return (int) ( $seconds / 60 / 60 );
440
			case 'minutes':
441
				return (int) ( $seconds / 60 + 1 );
442
			default:
443
				return $seconds;
444
		}
445
	}
446
447
	/**
448
	 * Update
449
	 */
450
	function update( $new_instance, $old_instance ) {
451
		return $this->sanitize_instance( $new_instance );
452
	}
453
454
	/*
455
	 * Make sure that a number is within a certain range.
456
	 * If the number is too small it will become the possible lowest value.
457
	 * If the number is too large it will become the possible highest value.
458
	 *
459
	 * @param int $n The number to check.
460
	 * @param int $floor The lowest possible value.
461
	 * @param int $ceil The highest possible value.
462
	 */
463
	function sanitize_range( $n, $floor, $ceil ) {
464
		$n = (int) $n;
465
		if ( $n < $floor ) {
466
			$n = $floor;
467
		} elseif ( $n > $ceil ) {
468
			$n = $ceil;
469
		}
470
		return $n;
471
	}
472
473
	/*
474
	 * Sanitize an instance of this widget.
475
	 *
476
	 * Date ranges match the documentation for mktime in the php manual.
477
	 * @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters
478
	 *
479
	 * @uses Milestone_Widget::sanitize_range().
480
	 */
481
	function sanitize_instance( $dirty ) {
482
		$now = (int) current_time( 'timestamp' );
483
484
		$dirty = wp_parse_args( $dirty, array(
485
			'title'   => '',
486
			'event'   => __( 'The Big Day', 'jetpack' ),
487
			'unit'    => 'automatic',
488
			'type'    => 'until',
489
			'message' => __( 'The big day is here.', 'jetpack' ),
490
			'day'     => date( 'd', $now ),
491
			'month'   => date( 'm', $now ),
492
			'year'    => date( 'Y', $now ),
493
			'hour'    => 0,
494
			'min'     => 0,
495
		) );
496
497
		$allowed_tags = array(
498
			'a'      => array( 'title' => array(), 'href' => array(), 'target' => array() ),
499
			'em'     => array( 'title' => array() ),
500
			'strong' => array( 'title' => array() ),
501
		);
502
503
		$clean = array(
504
			'title'   => trim( strip_tags( stripslashes( $dirty['title'] ) ) ),
505
			'event'   => trim( strip_tags( stripslashes( $dirty['event'] ) ) ),
506
			'unit'    => $dirty['unit'],
507
			'type'    => $dirty['type'],
508
			'message' => wp_kses( $dirty['message'], $allowed_tags ),
509
			'year'    => $this->sanitize_range( $dirty['year'],  1901, 2037 ),
510
			'month'   => $this->sanitize_range( $dirty['month'], 1, 12 ),
511
			'hour'    => $this->sanitize_range( $dirty['hour'],  0, 23 ),
512
			'min'     => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ),
513
		);
514
515
		$clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) );
516
517
		return $clean;
518
	}
519
520
	/**
521
	 * Form
522
	 */
523
	function form( $instance ) {
524
		$instance = $this->sanitize_instance( $instance );
525
526
		$units = array(
527
			'automatic' => __( 'Automatic', 'jetpack' ),
528
			'months' => __( 'Months', 'jetpack' ),
529
			'days' => __( 'Days', 'jetpack' ),
530
			'hours' => __( 'Hours', 'jetpack' ),
531
		);
532
		?>
533
534
	<div class="milestone-widget">
535
        <p>
536
        	<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'jetpack' ); ?></label>
537
        	<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'] ); ?>" />
538
        </p>
539
540
        <p>
541
        	<label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Description', 'jetpack' ); ?></label>
542
        	<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'] ); ?>" />
543
        </p>
544
545
		<fieldset class="jp-ms-data-time">
546
			<legend><?php esc_html_e( 'Date', 'jetpack' ); ?></legend>
547
548
			<label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month', 'jetpack' ); ?></label>
549
			<select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php
550
				global $wp_locale;
551
				for ( $i = 1; $i < 13; $i++ ) {
552
					$monthnum = zeroise( $i, 2 );
553
					echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>';
554
				}
555
			?></select>
556
557
			<label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day', 'jetpack' ); ?></label>
558
			<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'] ); ?>">,
559
560
			<label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year', 'jetpack' ); ?></label>
561
			<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'] ); ?>">
562
		</fieldset>
563
564
		<fieldset class="jp-ms-data-time">
565
			<legend><?php esc_html_e( 'Time', 'jetpack' ); ?></legend>
566
567
			<label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour', 'jetpack' ); ?></label>
568
			<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'] ); ?>">
569
570
			<label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes', 'jetpack' ); ?></label>
571
572
			<span class="time-separator">:</span>
573
574
			<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'] ); ?>">
575
		</fieldset>
576
577
		<fieldset class="jp-ms-data-unit">
578
			<legend><?php esc_html_e( 'Time Unit', 'jetpack' ); ?></legend>
579
580
			<label for="<?php echo $this->get_field_id( 'unit' ); ?>" class="assistive-text">
581
				<?php _e( 'Time Unit', 'jetpack' ); ?>
582
			</label>
583
			<select id="<?php echo $this->get_field_id( 'unit' ); ?>" class="unit" name="<?php echo $this->get_field_name( 'unit' ); ?>">
584
			<?php
585
				foreach ( $units as $key => $unit ) {
586
					echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $instance['unit'], false ) . '>' . $unit . '</option>';
587
				}
588
			?></select>
589
		</fieldset>
590
591
		<ul class="milestone-type">
592
			<li>
593
				<label>
594
					<input
595
						<?php checked( $instance['type'], 'until' ); ?>
596
						name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>"
597
						type="radio"
598
						value="until"
599
					/>
600
					<?php esc_html_e( 'Until your milestone', 'jetpack' ); ?>
601
				</label>
602
			</li>
603
604
			<li>
605
				<label>
606
					<input
607
						<?php checked( $instance['type'], 'since' ); ?>
608
						name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>"
609
						type="radio"
610
						value="since"
611
					/>
612
					<?php esc_html_e( 'Since your milestone', 'jetpack' ); ?>
613
				</label>
614
			</li>
615
		</ul>
616
617
		<p class="milestone-message-wrapper">
618
			<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Milestone Reached Message', 'jetpack' ); ?></label>
619
			<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>
620
		</p>
621
	</div>
622
623
		<?php
624
    }
625
}
626