Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_Milestone   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 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: 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;
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...
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
		}
66
	}
67
68
	public static function enqueue_template() {
69
		wp_enqueue_script( 'milestone', self::$url . 'milestone.js', array( 'jquery' ), '20160520', true );
70
	}
71
72
	public static function styles_template() {
73
		global $themecolors;
74
		$colors = wp_parse_args( $themecolors, array(
75
			'bg'     => 'ffffff',
76
			'border' => 'cccccc',
77
			'text'   => '333333',
78
		) );
79
?>
80
<style>
81
.milestone-widget {
82
	margin-bottom: 1em;
83
}
84
.milestone-content {
85
	line-height: 2;
86
	margin-top: 5px;
87
	max-width: 100%;
88
	padding: 0;
89
	text-align: center;
90
}
91
.milestone-header {
92
	background-color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
93
	color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
94
	line-height: 1.3;
95
	margin: 0;
96
	padding: .8em;
97
}
98
.milestone-header .event,
99
.milestone-header .date {
100
	display: block;
101
}
102
.milestone-header .event {
103
	font-size: 120%;
104
}
105
.milestone-countdown .difference {
106
	display: block;
107
	font-size: 500%;
108
	font-weight: bold;
109
	line-height: 1.2;
110
}
111
.milestone-countdown,
112
.milestone-message {
113
	background-color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>;
114
	border: 1px solid <?php echo self::sanitize_color_hex( $colors['border'] ); ?>;
115
	border-top: 0;
116
	color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>;
117
	padding-bottom: 1em;
118
}
119
.milestone-message {
120
	padding-top: 1em
121
}
122
</style>
123
<?php
124
	}
125
126
	/**
127
	 * Ensure that a string representing a color in hexadecimal
128
	 * notation is safe for use in css and database saves.
129
	 *
130
	 * @param string Color in hexadecimal notation. "#" may or may not be prepended to the string.
131
	 * @return string Color in hexadecimal notation on success - the string "transparent" otherwise.
132
	 */
133
	public static function sanitize_color_hex( $hex, $prefix = '#' ) {
134
		$hex = trim( $hex );
135
136
		/* Strip recognized prefixes. */
137
		if ( 0 === strpos( $hex, '#' ) ) {
138
			$hex = substr( $hex, 1 );
139
		} elseif ( 0 === strpos( $hex, '%23' ) ) {
140
			$hex = substr( $hex, 3 );
141
		}
142
143
		if ( 0 !== preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
144
			return $prefix . $hex;
145
		}
146
147
		return 'transparent';
148
	}
149
150
	/**
151
	 * Localize Front-end Script.
152
	 *
153
	 * Print the javascript configuration array only if the
154
	 * current template has an instance of the widget that
155
	 * is still counting down. In all other cases, this
156
	 * function will dequeue milestone.js.
157
	 *
158
	 * Hooks into the "wp_footer" action.
159
	 */
160
	function localize_script() {
161
		if ( empty( self::$config_js['instances'] ) ) {
162
			wp_dequeue_script( 'milestone' );
163
			return;
164
		}
165
		self::$config_js['labels'] = self::$labels;
166
		wp_localize_script( 'milestone', 'MilestoneConfig', self::$config_js );
167
	}
168
169
    /**
170
     * Widget
171
     */
172
    function widget( $args, $instance ) {
173
		$instance = $this->sanitize_instance( $instance );
174
175
		$milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] );
176
		$now  = (int) current_time( 'timestamp' );
177
		$diff = (int) floor( $milestone - $now );
178
179
		$number = 0;
0 ignored issues
show
Unused Code introduced by
$number 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...
180
		$label  = '';
0 ignored issues
show
Unused Code introduced by
$label 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...
181
182
		if ( 63113852 < $diff ) { // more than 2 years - show in years, one decimal point
183
			$number = round( $diff / 60 / 60 / 24 / 365, 1 );
184
			$label  = self::$labels['years'];
185
		} else if ( 7775999 < $diff ) { // fewer than 2 years - show in months
186
			$number = floor( $diff / 60 / 60 / 24 / 30 );
187
			$label  = ( 1 == $number ) ? self::$labels['month'] : self::$labels['months'];
188
		} else if ( 86399 < $diff ) { // fewer than 3 months - show in days
189
			$number = floor( $diff / 60 / 60 / 24 ) + 1;
190
			$label  = ( 1 == $number ) ? self::$labels['day'] : self::$labels['days'];
191
		} else if ( 3599 < $diff ) { // less than 1 day - show in hours
192
			$number = floor( $diff / 60 / 60 );
193
			$label  = ( 1 == $number ) ? self::$labels['hour'] : self::$labels['hours'];
194
		} else if ( 59 < $diff ) { // less than 1 hour - show in minutes
195
			$number = floor( $diff / 60 ) + 1;
196
			$label = ( 1 == $number ) ? self::$labels['minute'] : self::$labels['minutes'];
197
		} else { // less than 1 minute - show in seconds
198
			$number = $diff;
199
			$label = ( 1 == $number ) ? self::$labels['second'] : self::$labels['seconds'] ;
200
		}
201
202
		echo $args['before_widget'];
203
204
		$title = apply_filters( 'widget_title', $instance['title'] );
205
		if ( ! empty( $title ) ) {
206
			echo $args['before_title'] . $title . $args['after_title'];
207
		}
208
209
		echo '<div class="milestone-content">';
210
211
		echo '<div class="milestone-header">';
212
		echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>';
213
		echo '<span class="date">' . esc_html( date_i18n( __( 'F jS, Y', 'jetpack' ), $milestone ) ) . '</span>';
214
		echo '</div>';
215
216
		if ( 1 > $diff ) {
217
			/* Milestone has past. */
218
			echo '<div class="milestone-message">' . $instance['message'] . '</div>';
219
		} else {
220
			/* Countdown to the milestone. */
221
			echo '<div class="milestone-countdown">' . sprintf( __( '%1$s %2$s to go.', 'jetpack' ),
222
				'<span class="difference">' . esc_html( $number ) . '</span>',
223
				'<span class="label">' . esc_html( $label ) . '</span>'
224
			) . '</div>';
225
226
			self::$config_js['instances'][] = array(
227
				'id'      => $args['widget_id'],
228
				'diff'    => $diff,
229
				'message' => $instance['message'],
230
			);
231
		}
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
    /**
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
		} elseif ( $n > $ceil ) {
262
			$n = $ceil;
263
		}
264
		return $n;
265
	}
266
267
	/*
268
	 * Sanitize an instance of this widget.
269
	 *
270
	 * Date ranges match the documentation for mktime in the php manual.
271
	 * @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters
272
	 *
273
	 * @uses Milestone_Widget::sanitize_range().
274
	 */
275
	function sanitize_instance( $dirty ) {
276
		$now = (int) current_time( 'timestamp' );
277
278
		$dirty = wp_parse_args( $dirty, array(
279
			'title'   => '',
280
			'event'   => __( 'The Big Day', 'jetpack' ),
281
			'message' => __( 'The big day is here.', 'jetpack' ),
282
			'day'     => date( 'd', $now ),
283
			'month'   => date( 'm', $now ),
284
			'year'    => date( 'Y', $now ),
285
			'hour'    => 0,
286
			'min'     => 0,
287
		) );
288
289
		$allowed_tags = array(
290
			'a'      => array( 'title' => array(), 'href' => array() ),
291
			'em'     => array( 'title' => array() ),
292
			'strong' => array( 'title' => array() ),
293
		);
294
295
		$clean = array(
296
			'title'   => trim( strip_tags( stripslashes( $dirty['title'] ) ) ),
297
			'event'   => trim( strip_tags( stripslashes( $dirty['event'] ) ) ),
298
			'message' => wp_kses( $dirty['message'], $allowed_tags ),
299
			'year'    => $this->sanitize_range( $dirty['year'],  1901, 2037 ),
300
			'month'   => $this->sanitize_range( $dirty['month'], 1, 12 ),
301
			'hour'    => $this->sanitize_range( $dirty['hour'],  0, 23 ),
302
			'min'     => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ),
303
		);
304
305
		$clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) );
306
307
		return $clean;
308
	}
309
310
    /**
311
     * Form
312
     */
313
    function form( $instance ) {
314
		$instance = $this->sanitize_instance( $instance );
315
        ?>
316
317
	<div class="milestone-widget">
318
        <p>
319
        	<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'jetpack' ); ?></label>
320
        	<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'] ); ?>" />
321
        </p>
322
323
        <p>
324
        	<label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Event', 'jetpack' ); ?></label>
325
        	<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'] ); ?>" />
326
        </p>
327
328
		<fieldset class="jp-ms-data-time">
329
			<legend><?php esc_html_e( 'Date', 'jetpack' ); ?></legend>
330
331
			<label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month', 'jetpack' ); ?></label>
332
			<select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php
333
				global $wp_locale;
334
				for ( $i = 1; $i < 13; $i++ ) {
335
					$monthnum = zeroise( $i, 2 );
336
					echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>';
337
				}
338
			?></select>
339
340
			<label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day', 'jetpack' ); ?></label>
341
			<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'] ); ?>">,
342
343
			<label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year', 'jetpack' ); ?></label>
344
			<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'] ); ?>">
345
		</fieldset>
346
347
		<fieldset class="jp-ms-data-time">
348
			<legend><?php esc_html_e( 'Time', 'jetpack' ); ?></legend>
349
350
			<label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour', 'jetpack' ); ?></label>
351
			<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'] ); ?>">
352
353
			<label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes', 'jetpack' ); ?></label>
354
355
			<span class="time-separator">:</span>
356
357
			<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'] ); ?>">
358
		</fieldset>
359
360
		<p>
361
			<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Message', 'jetpack' ); ?></label>
362
			<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>
363
		</p>
364
	</div>
365
366
		<?php
367
    }
368
}
369