No Description

class-milestone-widget.php 24KB

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