milestone.php 21 KB

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