PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/customize/class-wp-customize-date-time-control.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 289 lines | 154 code | 28 blank | 107 comment | 13 complexity | 6b22b82caa7784e6eb8d25f1d18ee8e0 MD5 | raw file
  1. <?php
  2. /**
  3. * Customize API: WP_Customize_Date_Time_Control class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.9.0
  8. */
  9. /**
  10. * Customize Date Time Control class.
  11. *
  12. * @since 4.9.0
  13. *
  14. * @see WP_Customize_Control
  15. */
  16. class WP_Customize_Date_Time_Control extends WP_Customize_Control {
  17. /**
  18. * Customize control type.
  19. *
  20. * @since 4.9.0
  21. * @var string
  22. */
  23. public $type = 'date_time';
  24. /**
  25. * Minimum Year.
  26. *
  27. * @since 4.9.0
  28. * @var int
  29. */
  30. public $min_year = 1000;
  31. /**
  32. * Maximum Year.
  33. *
  34. * @since 4.9.0
  35. * @var int
  36. */
  37. public $max_year = 9999;
  38. /**
  39. * Allow past date, if set to false user can only select future date.
  40. *
  41. * @since 4.9.0
  42. * @var bool
  43. */
  44. public $allow_past_date = true;
  45. /**
  46. * Whether hours, minutes, and meridian should be shown.
  47. *
  48. * @since 4.9.0
  49. * @var bool
  50. */
  51. public $include_time = true;
  52. /**
  53. * If set to false the control will appear in 24 hour format,
  54. * the value will still be saved in Y-m-d H:i:s format.
  55. *
  56. * @since 4.9.0
  57. * @var bool
  58. */
  59. public $twelve_hour_format = true;
  60. /**
  61. * Don't render the control's content - it's rendered with a JS template.
  62. *
  63. * @since 4.9.0
  64. */
  65. public function render_content() {}
  66. /**
  67. * Export data to JS.
  68. *
  69. * @since 4.9.0
  70. * @return array
  71. */
  72. public function json() {
  73. $data = parent::json();
  74. $data['maxYear'] = (int) $this->max_year;
  75. $data['minYear'] = (int) $this->min_year;
  76. $data['allowPastDate'] = (bool) $this->allow_past_date;
  77. $data['twelveHourFormat'] = (bool) $this->twelve_hour_format;
  78. $data['includeTime'] = (bool) $this->include_time;
  79. return $data;
  80. }
  81. /**
  82. * Renders a JS template for the content of date time control.
  83. *
  84. * @since 4.9.0
  85. */
  86. public function content_template() {
  87. $data = array_merge( $this->json(), $this->get_month_choices() );
  88. $timezone_info = $this->get_timezone_info();
  89. $date_format = get_option( 'date_format' );
  90. $date_format = preg_replace( '/(?<!\\\\)[Yyo]/', '%1$s', $date_format );
  91. $date_format = preg_replace( '/(?<!\\\\)[FmMn]/', '%2$s', $date_format );
  92. $date_format = preg_replace( '/(?<!\\\\)[jd]/', '%3$s', $date_format );
  93. // Fallback to ISO date format if year, month, or day are missing from the date format.
  94. if ( 1 !== substr_count( $date_format, '%1$s' ) || 1 !== substr_count( $date_format, '%2$s' ) || 1 !== substr_count( $date_format, '%3$s' ) ) {
  95. $date_format = '%1$s-%2$s-%3$s';
  96. }
  97. ?>
  98. <# _.defaults( data, <?php echo wp_json_encode( $data ); ?> ); #>
  99. <# var idPrefix = _.uniqueId( 'el' ) + '-'; #>
  100. <# if ( data.label ) { #>
  101. <span class="customize-control-title">
  102. {{ data.label }}
  103. </span>
  104. <# } #>
  105. <div class="customize-control-notifications-container"></div>
  106. <# if ( data.description ) { #>
  107. <span class="description customize-control-description">{{ data.description }}</span>
  108. <# } #>
  109. <div class="date-time-fields {{ data.includeTime ? 'includes-time' : '' }}">
  110. <fieldset class="day-row">
  111. <legend class="title-day {{ ! data.includeTime ? 'screen-reader-text' : '' }}"><?php esc_html_e( 'Date' ); ?></legend>
  112. <div class="day-fields clear">
  113. <?php ob_start(); ?>
  114. <label for="{{ idPrefix }}date-time-month" class="screen-reader-text"><?php esc_html_e( 'Month' ); ?></label>
  115. <select id="{{ idPrefix }}date-time-month" class="date-input month" data-component="month">
  116. <# _.each( data.month_choices, function( choice ) {
  117. if ( _.isObject( choice ) && ! _.isUndefined( choice.text ) && ! _.isUndefined( choice.value ) ) {
  118. text = choice.text;
  119. value = choice.value;
  120. }
  121. #>
  122. <option value="{{ value }}" >
  123. {{ text }}
  124. </option>
  125. <# } ); #>
  126. </select>
  127. <?php $month_field = trim( ob_get_clean() ); ?>
  128. <?php ob_start(); ?>
  129. <label for="{{ idPrefix }}date-time-day" class="screen-reader-text"><?php esc_html_e( 'Day' ); ?></label>
  130. <input id="{{ idPrefix }}date-time-day" type="number" size="2" autocomplete="off" class="date-input day" data-component="day" min="1" max="31" />
  131. <?php $day_field = trim( ob_get_clean() ); ?>
  132. <?php ob_start(); ?>
  133. <label for="{{ idPrefix }}date-time-year" class="screen-reader-text"><?php esc_html_e( 'Year' ); ?></label>
  134. <input id="{{ idPrefix }}date-time-year" type="number" size="4" autocomplete="off" class="date-input year" data-component="year" min="{{ data.minYear }}" max="{{ data.maxYear }}">
  135. <?php $year_field = trim( ob_get_clean() ); ?>
  136. <?php printf( $date_format, $year_field, $month_field, $day_field ); ?>
  137. </div>
  138. </fieldset>
  139. <# if ( data.includeTime ) { #>
  140. <fieldset class="time-row clear">
  141. <legend class="title-time"><?php esc_html_e( 'Time' ); ?></legend>
  142. <div class="time-fields clear">
  143. <label for="{{ idPrefix }}date-time-hour" class="screen-reader-text"><?php esc_html_e( 'Hour' ); ?></label>
  144. <# var maxHour = data.twelveHourFormat ? 12 : 23; #>
  145. <# var minHour = data.twelveHourFormat ? 1 : 0; #>
  146. <input id="{{ idPrefix }}date-time-hour" type="number" size="2" autocomplete="off" class="date-input hour" data-component="hour" min="{{ minHour }}" max="{{ maxHour }}">
  147. :
  148. <label for="{{ idPrefix }}date-time-minute" class="screen-reader-text"><?php esc_html_e( 'Minute' ); ?></label>
  149. <input id="{{ idPrefix }}date-time-minute" type="number" size="2" autocomplete="off" class="date-input minute" data-component="minute" min="0" max="59">
  150. <# if ( data.twelveHourFormat ) { #>
  151. <label for="{{ idPrefix }}date-time-meridian" class="screen-reader-text"><?php esc_html_e( 'Meridian' ); ?></label>
  152. <select id="{{ idPrefix }}date-time-meridian" class="date-input meridian" data-component="meridian">
  153. <option value="am"><?php esc_html_e( 'AM' ); ?></option>
  154. <option value="pm"><?php esc_html_e( 'PM' ); ?></option>
  155. </select>
  156. <# } #>
  157. <p><?php echo $timezone_info['description']; ?></p>
  158. </div>
  159. </fieldset>
  160. <# } #>
  161. </div>
  162. <?php
  163. }
  164. /**
  165. * Generate options for the month Select.
  166. *
  167. * Based on touch_time().
  168. *
  169. * @since 4.9.0
  170. *
  171. * @see touch_time()
  172. *
  173. * @global WP_Locale $wp_locale WordPress date and time locale object.
  174. *
  175. * @return array
  176. */
  177. public function get_month_choices() {
  178. global $wp_locale;
  179. $months = array();
  180. for ( $i = 1; $i < 13; $i++ ) {
  181. $month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
  182. /* translators: 1: Month number (01, 02, etc.), 2: Month abbreviation. */
  183. $months[ $i ]['text'] = sprintf( __( '%1$s-%2$s' ), $i, $month_text );
  184. $months[ $i ]['value'] = $i;
  185. }
  186. return array(
  187. 'month_choices' => $months,
  188. );
  189. }
  190. /**
  191. * Get timezone info.
  192. *
  193. * @since 4.9.0
  194. *
  195. * @return array {
  196. * Timezone info. All properties are optional.
  197. *
  198. * @type string $abbr Timezone abbreviation. Examples: PST or CEST.
  199. * @type string $description Human-readable timezone description as HTML.
  200. * }
  201. */
  202. public function get_timezone_info() {
  203. $tz_string = get_option( 'timezone_string' );
  204. $timezone_info = array();
  205. if ( $tz_string ) {
  206. try {
  207. $tz = new DateTimeZone( $tz_string );
  208. } catch ( Exception $e ) {
  209. $tz = '';
  210. }
  211. if ( $tz ) {
  212. $now = new DateTime( 'now', $tz );
  213. $formatted_gmt_offset = $this->format_gmt_offset( $tz->getOffset( $now ) / 3600 );
  214. $tz_name = str_replace( '_', ' ', $tz->getName() );
  215. $timezone_info['abbr'] = $now->format( 'T' );
  216. $timezone_info['description'] = sprintf(
  217. /* translators: 1: Timezone name, 2: Timezone abbreviation, 3: UTC abbreviation and offset, 4: UTC offset. */
  218. __( 'Your timezone is set to %1$s (%2$s), currently %3$s (Coordinated Universal Time %4$s).' ),
  219. $tz_name,
  220. '<abbr>' . $timezone_info['abbr'] . '</abbr>',
  221. '<abbr>UTC</abbr>' . $formatted_gmt_offset,
  222. $formatted_gmt_offset
  223. );
  224. } else {
  225. $timezone_info['description'] = '';
  226. }
  227. } else {
  228. $formatted_gmt_offset = $this->format_gmt_offset( (int) get_option( 'gmt_offset', 0 ) );
  229. $timezone_info['description'] = sprintf(
  230. /* translators: 1: UTC abbreviation and offset, 2: UTC offset. */
  231. __( 'Your timezone is set to %1$s (Coordinated Universal Time %2$s).' ),
  232. '<abbr>UTC</abbr>' . $formatted_gmt_offset,
  233. $formatted_gmt_offset
  234. );
  235. }
  236. return $timezone_info;
  237. }
  238. /**
  239. * Format GMT Offset.
  240. *
  241. * @since 4.9.0
  242. *
  243. * @see wp_timezone_choice()
  244. *
  245. * @param float $offset Offset in hours.
  246. * @return string Formatted offset.
  247. */
  248. public function format_gmt_offset( $offset ) {
  249. if ( 0 <= $offset ) {
  250. $formatted_offset = '+' . (string) $offset;
  251. } else {
  252. $formatted_offset = (string) $offset;
  253. }
  254. $formatted_offset = str_replace(
  255. array( '.25', '.5', '.75' ),
  256. array( ':15', ':30', ':45' ),
  257. $formatted_offset
  258. );
  259. return $formatted_offset;
  260. }
  261. }