Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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; |
||
| 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 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
||
| 175 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
| 176 | if ( ! empty( $title ) ) { |
||
| 177 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 178 | } |
||
| 179 | |||
| 180 | $data = $this->get_widget_data( $instance ); |
||
| 181 | |||
| 182 | self::$config_js['instances'][] = array( |
||
| 183 | 'id' => $args['widget_id'], |
||
| 184 | 'message' => $data['message'], |
||
| 185 | 'refresh' => $data['refresh'] |
||
| 186 | ); |
||
| 187 | |||
| 188 | echo '<div class="milestone-content">'; |
||
| 189 | |||
| 190 | echo '<div class="milestone-header">'; |
||
| 191 | echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>'; |
||
| 192 | echo '<span class="date">' . esc_html( date_i18n( get_option( 'date_format' ), $data['milestone'] ) ) . '</span>'; |
||
| 193 | echo '</div>'; |
||
| 194 | |||
| 195 | echo $data['message']; |
||
| 196 | |||
| 197 | echo '</div><!--milestone-content-->'; |
||
| 198 | |||
| 199 | echo $args['after_widget']; |
||
| 200 | |||
| 201 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 202 | do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' ); |
||
| 203 | } |
||
| 204 | |||
| 205 | function get_widget_data( $instance ) { |
||
| 206 | $data = array(); |
||
| 207 | |||
| 208 | $instance = $this->sanitize_instance( $instance ); |
||
| 209 | |||
| 210 | $milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] ); |
||
| 211 | $now = (int) current_time( 'timestamp' ); |
||
| 212 | $type = $instance['type']; |
||
| 213 | |||
| 214 | if ( 'since' === $type ) { |
||
| 215 | $diff = (int) floor( $now - $milestone ); |
||
| 216 | } else { |
||
| 217 | $diff = (int) floor( $milestone - $now ); |
||
| 218 | } |
||
| 219 | |||
| 220 | $data['diff'] = $diff; |
||
| 221 | $data['unit'] = $this->get_unit( $diff, $instance['unit'] ); |
||
| 222 | |||
| 223 | // Setting the refresh counter to equal the number of seconds it takes to flip a unit |
||
| 224 | $refresh_intervals = array( |
||
| 225 | 0, // should be YEAR_IN_SECONDS, but doing setTimeout for a year doesn't seem to be logical |
||
| 226 | 0, // same goes for MONTH_IN_SECONDS, |
||
| 227 | DAY_IN_SECONDS, |
||
| 228 | HOUR_IN_SECONDS, |
||
| 229 | MINUTE_IN_SECONDS, |
||
| 230 | 1 |
||
| 231 | ); |
||
| 232 | |||
| 233 | $data['refresh'] = $refresh_intervals[ array_search( $data['unit'], $this->available_units ) ]; |
||
| 234 | $data['milestone'] = $milestone; |
||
| 235 | |||
| 236 | if ( ( 1 > $diff ) && ( 'until' === $type ) ) { |
||
| 237 | $data['message'] = '<div class="milestone-message">' . $instance['message'] . '</div>'; |
||
| 238 | $data['refresh'] = 0; // No need to refresh, the milestone has been reached |
||
| 239 | } else { |
||
| 240 | $interval_text = $this->get_interval_in_units( $diff, $data['unit'] ); |
||
| 241 | $interval = intval( $interval_text ); |
||
| 242 | |||
| 243 | if ( 'since' === $type ) { |
||
| 244 | |||
| 245 | View Code Duplication | switch ( $data['unit'] ) { |
|
| 246 | case 'years': |
||
| 247 | $data['message'] = sprintf( |
||
| 248 | _n( |
||
| 249 | '<span class="difference">%s</span> <span class="label">year ago.</span>', |
||
| 250 | '<span class="difference">%s</span> <span class="label">years ago.</span>', |
||
| 251 | $interval, |
||
| 252 | 'jetpack' |
||
| 253 | ), |
||
| 254 | $interval_text |
||
| 255 | ); |
||
| 256 | break; |
||
| 257 | case 'months': |
||
| 258 | $data['message'] = sprintf( |
||
| 259 | _n( |
||
| 260 | '<span class="difference">%s</span> <span class="label">month ago.</span>', |
||
| 261 | '<span class="difference">%s</span> <span class="label">months ago.</span>', |
||
| 262 | $interval, |
||
| 263 | 'jetpack' |
||
| 264 | ), |
||
| 265 | $interval_text |
||
| 266 | ); |
||
| 267 | break; |
||
| 268 | case 'days': |
||
| 269 | $data['message'] = sprintf( |
||
| 270 | _n( |
||
| 271 | '<span class="difference">%s</span> <span class="label">day ago.</span>', |
||
| 272 | '<span class="difference">%s</span> <span class="label">days ago.</span>', |
||
| 273 | $interval, |
||
| 274 | 'jetpack' |
||
| 275 | ), |
||
| 276 | $interval_text |
||
| 277 | ); |
||
| 278 | break; |
||
| 279 | case 'hours': |
||
| 280 | $data['message'] = sprintf( |
||
| 281 | _n( |
||
| 282 | '<span class="difference">%s</span> <span class="label">hour ago.</span>', |
||
| 283 | '<span class="difference">%s</span> <span class="label">hours ago.</span>', |
||
| 284 | $interval, |
||
| 285 | 'jetpack' |
||
| 286 | ), |
||
| 287 | $interval_text |
||
| 288 | ); |
||
| 289 | break; |
||
| 290 | case 'minutes': |
||
| 291 | $data['message'] = sprintf( |
||
| 292 | _n( |
||
| 293 | '<span class="difference">%s</span> <span class="label">minute ago.</span>', |
||
| 294 | '<span class="difference">%s</span> <span class="label">minutes ago.</span>', |
||
| 295 | $interval, |
||
| 296 | 'jetpack' |
||
| 297 | ), |
||
| 298 | $interval_text |
||
| 299 | ); |
||
| 300 | break; |
||
| 301 | case 'seconds': |
||
| 302 | $data['message'] = sprintf( |
||
| 303 | _n( |
||
| 304 | '<span class="difference">%s</span> <span class="label">second ago.</span>', |
||
| 305 | '<span class="difference">%s</span> <span class="label">seconds ago.</span>', |
||
| 306 | $interval, |
||
| 307 | 'jetpack' |
||
| 308 | ), |
||
| 309 | $interval_text |
||
| 310 | ); |
||
| 311 | break; |
||
| 312 | } |
||
| 313 | } else { |
||
| 314 | View Code Duplication | switch ( $this->get_unit( $diff, $instance['unit'] ) ) { |
|
| 315 | case 'years': |
||
| 316 | $data['message'] = sprintf( |
||
| 317 | _n( |
||
| 318 | '<span class="difference">%s</span> <span class="label">year to go.</span>', |
||
| 319 | '<span class="difference">%s</span> <span class="label">years to go.</span>', |
||
| 320 | $interval, |
||
| 321 | 'jetpack' |
||
| 322 | ), |
||
| 323 | $interval_text |
||
| 324 | ); |
||
| 325 | break; |
||
| 326 | case 'months': |
||
| 327 | $data['message'] = sprintf( |
||
| 328 | _n( |
||
| 329 | '<span class="difference">%s</span> <span class="label">month to go.</span>', |
||
| 330 | '<span class="difference">%s</span> <span class="label">months to go.</span>', |
||
| 331 | $interval, |
||
| 332 | 'jetpack' |
||
| 333 | ), |
||
| 334 | $interval_text |
||
| 335 | ); |
||
| 336 | break; |
||
| 337 | case 'days': |
||
| 338 | $data['message'] = sprintf( |
||
| 339 | _n( |
||
| 340 | '<span class="difference">%s</span> <span class="label">day to go.</span>', |
||
| 341 | '<span class="difference">%s</span> <span class="label">days to go.</span>', |
||
| 342 | $interval, |
||
| 343 | 'jetpack' |
||
| 344 | ), |
||
| 345 | $interval_text |
||
| 346 | ); |
||
| 347 | break; |
||
| 348 | case 'hours': |
||
| 349 | $data['message'] = sprintf( |
||
| 350 | _n( |
||
| 351 | '<span class="difference">%s</span> <span class="label">hour to go.</span>', |
||
| 352 | '<span class="difference">%s</span> <span class="label">hours to go.</span>', |
||
| 353 | $interval, |
||
| 354 | 'jetpack' |
||
| 355 | ), |
||
| 356 | $interval_text |
||
| 357 | ); |
||
| 358 | break; |
||
| 359 | case 'minutes': |
||
| 360 | $data['message'] = sprintf( |
||
| 361 | _n( |
||
| 362 | '<span class="difference">%s</span> <span class="label">minute to go.</span>', |
||
| 363 | '<span class="difference">%s</span> <span class="label">minutes to go.</span>', |
||
| 364 | $interval, |
||
| 365 | 'jetpack' |
||
| 366 | ), |
||
| 367 | $interval_text |
||
| 368 | ); |
||
| 369 | break; |
||
| 370 | case 'seconds': |
||
| 371 | $data['message'] = sprintf( |
||
| 372 | _n( |
||
| 373 | '<span class="difference">%s</span> <span class="label">second to go.</span>', |
||
| 374 | '<span class="difference">%s</span> <span class="label">seconds to go.</span>', |
||
| 375 | $interval, |
||
| 376 | 'jetpack' |
||
| 377 | ), |
||
| 378 | $interval_text |
||
| 379 | ); |
||
| 380 | break; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | $data['message'] = '<div class="milestone-countdown">' . $data['message'] . '</div>'; |
||
| 384 | } |
||
| 385 | |||
| 386 | return $data; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return the largest possible time unit that the difference will be displayed in. |
||
| 391 | * |
||
| 392 | * @param Integer $seconds the interval in seconds |
||
| 393 | * @param String $maximum_unit the maximum unit that will be used. Optional. |
||
| 394 | * @return String $calculated_unit |
||
| 395 | */ |
||
| 396 | protected function get_unit( $seconds, $maximum_unit = 'automatic' ) { |
||
| 397 | $unit = ''; |
||
|
0 ignored issues
–
show
|
|||
| 398 | |||
| 399 | if ( $seconds >= YEAR_IN_SECONDS * 2 ) { |
||
| 400 | // more than 2 years - show in years, one decimal point |
||
| 401 | $unit = 'years'; |
||
| 402 | |||
| 403 | } else if ( $seconds >= YEAR_IN_SECONDS ) { |
||
| 404 | if ( 'years' === $maximum_unit ) { |
||
| 405 | $unit = 'years'; |
||
| 406 | } else { |
||
| 407 | // automatic mode - showing months even if it's between one and two years |
||
| 408 | $unit = 'months'; |
||
| 409 | } |
||
| 410 | |||
| 411 | } else if ( $seconds >= MONTH_IN_SECONDS * 3 ) { |
||
| 412 | // fewer than 2 years - show in months |
||
| 413 | $unit = 'months'; |
||
| 414 | |||
| 415 | } else if ( $seconds >= MONTH_IN_SECONDS ) { |
||
| 416 | if ( 'months' === $maximum_unit ) { |
||
| 417 | $unit = 'months'; |
||
| 418 | } else { |
||
| 419 | // automatic mode - showing days even if it's between one and three months |
||
| 420 | $unit = 'days'; |
||
| 421 | } |
||
| 422 | |||
| 423 | } else if ( $seconds >= DAY_IN_SECONDS - 1 ) { |
||
| 424 | // fewer than a month - show in days |
||
| 425 | $unit = 'days'; |
||
| 426 | |||
| 427 | } else if ( $seconds >= HOUR_IN_SECONDS - 1 ) { |
||
| 428 | // less than 1 day - show in hours |
||
| 429 | $unit = 'hours'; |
||
| 430 | |||
| 431 | } else if ( $seconds >= MINUTE_IN_SECONDS - 1 ) { |
||
| 432 | // less than 1 hour - show in minutes |
||
| 433 | $unit = 'minutes'; |
||
| 434 | |||
| 435 | } else { |
||
| 436 | // less than 1 minute - show in seconds |
||
| 437 | $unit = 'seconds'; |
||
| 438 | } |
||
| 439 | |||
| 440 | $maximum_unit_index = array_search( $maximum_unit, $this->available_units ); |
||
| 441 | $unit_index = array_search( $unit, $this->available_units ); |
||
| 442 | |||
| 443 | if ( |
||
| 444 | false === $maximum_unit_index // the maximum unit parameter is automatic |
||
| 445 | || $unit_index > $maximum_unit_index // there is not enough seconds for even one maximum time unit |
||
| 446 | ) { |
||
| 447 | return $unit; |
||
| 448 | } |
||
| 449 | return $maximum_unit; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Returns a time difference value in specified units. |
||
| 454 | * |
||
| 455 | * @param Integer $seconds |
||
| 456 | * @param String $units |
||
| 457 | * @return Integer|String $time_in_units |
||
| 458 | */ |
||
| 459 | protected function get_interval_in_units( $seconds, $units ) { |
||
| 460 | switch ( $units ) { |
||
| 461 | case 'years': |
||
| 462 | $years = $seconds / YEAR_IN_SECONDS; |
||
| 463 | $decimals = abs( round( $years, 1 ) - round( $years ) ) > 0 ? 1 : 0; |
||
| 464 | return number_format_i18n( $years, $decimals ); |
||
| 465 | case 'months': |
||
| 466 | return (int) ( $seconds / 60 / 60 / 24 / 30 ); |
||
| 467 | case 'days': |
||
| 468 | return (int) ( $seconds / 60 / 60 / 24 + 1 ); |
||
| 469 | case 'hours': |
||
| 470 | return (int) ( $seconds / 60 / 60 ); |
||
| 471 | case 'minutes': |
||
| 472 | return (int) ( $seconds / 60 + 1 ); |
||
| 473 | default: |
||
| 474 | return $seconds; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Update |
||
| 480 | */ |
||
| 481 | function update( $new_instance, $old_instance ) { |
||
| 482 | return $this->sanitize_instance( $new_instance ); |
||
| 483 | } |
||
| 484 | |||
| 485 | /* |
||
| 486 | * Make sure that a number is within a certain range. |
||
| 487 | * If the number is too small it will become the possible lowest value. |
||
| 488 | * If the number is too large it will become the possible highest value. |
||
| 489 | * |
||
| 490 | * @param int $n The number to check. |
||
| 491 | * @param int $floor The lowest possible value. |
||
| 492 | * @param int $ceil The highest possible value. |
||
| 493 | */ |
||
| 494 | function sanitize_range( $n, $floor, $ceil ) { |
||
| 495 | $n = (int) $n; |
||
| 496 | if ( $n < $floor ) { |
||
| 497 | $n = $floor; |
||
| 498 | } elseif ( $n > $ceil ) { |
||
| 499 | $n = $ceil; |
||
| 500 | } |
||
| 501 | return $n; |
||
| 502 | } |
||
| 503 | |||
| 504 | /* |
||
| 505 | * Sanitize an instance of this widget. |
||
| 506 | * |
||
| 507 | * Date ranges match the documentation for mktime in the php manual. |
||
| 508 | * @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters |
||
| 509 | * |
||
| 510 | * @uses Milestone_Widget::sanitize_range(). |
||
| 511 | */ |
||
| 512 | function sanitize_instance( $dirty ) { |
||
| 513 | $now = (int) current_time( 'timestamp' ); |
||
| 514 | |||
| 515 | $dirty = wp_parse_args( $dirty, array( |
||
| 516 | 'title' => '', |
||
| 517 | 'event' => __( 'The Big Day', 'jetpack' ), |
||
| 518 | 'unit' => 'automatic', |
||
| 519 | 'type' => 'until', |
||
| 520 | 'message' => __( 'The big day is here.', 'jetpack' ), |
||
| 521 | 'day' => date( 'd', $now ), |
||
| 522 | 'month' => date( 'm', $now ), |
||
| 523 | 'year' => date( 'Y', $now ), |
||
| 524 | 'hour' => 0, |
||
| 525 | 'min' => 0, |
||
| 526 | ) ); |
||
| 527 | |||
| 528 | $allowed_tags = array( |
||
| 529 | 'a' => array( 'title' => array(), 'href' => array(), 'target' => array() ), |
||
| 530 | 'em' => array( 'title' => array() ), |
||
| 531 | 'strong' => array( 'title' => array() ), |
||
| 532 | ); |
||
| 533 | |||
| 534 | $clean = array( |
||
| 535 | 'title' => trim( strip_tags( stripslashes( $dirty['title'] ) ) ), |
||
| 536 | 'event' => trim( strip_tags( stripslashes( $dirty['event'] ) ) ), |
||
| 537 | 'unit' => $dirty['unit'], |
||
| 538 | 'type' => $dirty['type'], |
||
| 539 | 'message' => wp_kses( $dirty['message'], $allowed_tags ), |
||
| 540 | 'year' => $this->sanitize_range( $dirty['year'], 1901, 2037 ), |
||
| 541 | 'month' => $this->sanitize_range( $dirty['month'], 1, 12 ), |
||
| 542 | 'hour' => $this->sanitize_range( $dirty['hour'], 0, 23 ), |
||
| 543 | 'min' => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ), |
||
| 544 | ); |
||
| 545 | |||
| 546 | $clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) ); |
||
| 547 | |||
| 548 | return $clean; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Form |
||
| 553 | */ |
||
| 554 | function form( $instance ) { |
||
| 555 | $instance = $this->sanitize_instance( $instance ); |
||
| 556 | |||
| 557 | $units = array( |
||
| 558 | 'automatic' => _x( 'Automatic', 'Milestone widget: mode in which the date unit is determined automatically', 'jetpack' ), |
||
| 559 | 'years' => _x( 'Years', 'Milestone widget: mode in which the date unit is set to years', 'jetpack' ), |
||
| 560 | 'months' => _x( 'Months', 'Milestone widget: mode in which the date unit is set to months', 'jetpack' ), |
||
| 561 | 'days' => _x( 'Days', 'Milestone widget: mode in which the date unit is set to days', 'jetpack' ), |
||
| 562 | 'hours' => _x( 'Hours', 'Milestone widget: mode in which the date unit is set to hours', 'jetpack' ), |
||
| 563 | ); |
||
| 564 | ?> |
||
| 565 | |||
| 566 | <div class="milestone-widget"> |
||
| 567 | <p> |
||
| 568 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'jetpack' ); ?></label> |
||
| 569 | <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'] ); ?>" /> |
||
| 570 | </p> |
||
| 571 | |||
| 572 | <p> |
||
| 573 | <label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Description', 'jetpack' ); ?></label> |
||
| 574 | <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'] ); ?>" /> |
||
| 575 | </p> |
||
| 576 | |||
| 577 | <fieldset class="jp-ms-data-time"> |
||
| 578 | <legend><?php esc_html_e( 'Date', 'jetpack' ); ?></legend> |
||
| 579 | |||
| 580 | <label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month', 'jetpack' ); ?></label> |
||
| 581 | <select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php |
||
| 582 | global $wp_locale; |
||
| 583 | for ( $i = 1; $i < 13; $i++ ) { |
||
| 584 | $monthnum = zeroise( $i, 2 ); |
||
| 585 | echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>'; |
||
| 586 | } |
||
| 587 | ?></select> |
||
| 588 | |||
| 589 | <label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day', 'jetpack' ); ?></label> |
||
| 590 | <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'] ); ?>">, |
||
| 591 | |||
| 592 | <label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year', 'jetpack' ); ?></label> |
||
| 593 | <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'] ); ?>"> |
||
| 594 | </fieldset> |
||
| 595 | |||
| 596 | <fieldset class="jp-ms-data-time"> |
||
| 597 | <legend><?php esc_html_e( 'Time', 'jetpack' ); ?></legend> |
||
| 598 | |||
| 599 | <label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour', 'jetpack' ); ?></label> |
||
| 600 | <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'] ); ?>"> |
||
| 601 | |||
| 602 | <label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes', 'jetpack' ); ?></label> |
||
| 603 | |||
| 604 | <span class="time-separator">:</span> |
||
| 605 | |||
| 606 | <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'] ); ?>"> |
||
| 607 | </fieldset> |
||
| 608 | |||
| 609 | <fieldset class="jp-ms-data-unit"> |
||
| 610 | <legend><?php esc_html_e( 'Time Unit', 'jetpack' ); ?></legend> |
||
| 611 | |||
| 612 | <label for="<?php echo $this->get_field_id( 'unit' ); ?>" class="assistive-text"> |
||
| 613 | <?php _e( 'Time Unit', 'jetpack' ); ?> |
||
| 614 | </label> |
||
| 615 | <select id="<?php echo $this->get_field_id( 'unit' ); ?>" class="unit" name="<?php echo $this->get_field_name( 'unit' ); ?>"> |
||
| 616 | <?php |
||
| 617 | foreach ( $units as $key => $unit ) { |
||
| 618 | echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $instance['unit'], false ) . '>' . $unit . '</option>'; |
||
| 619 | } |
||
| 620 | ?></select> |
||
| 621 | </fieldset> |
||
| 622 | |||
| 623 | <ul class="milestone-type"> |
||
| 624 | <li> |
||
| 625 | <label> |
||
| 626 | <input |
||
| 627 | <?php checked( $instance['type'], 'until' ); ?> |
||
| 628 | name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>" |
||
| 629 | type="radio" |
||
| 630 | value="until" |
||
| 631 | /> |
||
| 632 | <?php esc_html_e( 'Until your milestone', 'jetpack' ); ?> |
||
| 633 | </label> |
||
| 634 | </li> |
||
| 635 | |||
| 636 | <li> |
||
| 637 | <label> |
||
| 638 | <input |
||
| 639 | <?php checked( $instance['type'], 'since' ); ?> |
||
| 640 | name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>" |
||
| 641 | type="radio" |
||
| 642 | value="since" |
||
| 643 | /> |
||
| 644 | <?php esc_html_e( 'Since your milestone', 'jetpack' ); ?> |
||
| 645 | </label> |
||
| 646 | </li> |
||
| 647 | </ul> |
||
| 648 | |||
| 649 | <p class="milestone-message-wrapper"> |
||
| 650 | <label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e( 'Milestone Reached Message', 'jetpack' ); ?></label> |
||
| 651 | <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> |
||
| 652 | </p> |
||
| 653 | </div> |
||
| 654 | |||
| 655 | <?php |
||
| 656 | } |
||
| 657 | } |
||
| 658 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.