Milestone_Widget

The WordPress Core Milestone Widget class.

Defined (1)

The class is defined in the following location(s).

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