PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 282 lines | 154 code | 28 blank | 100 comment | 13 complexity | 1e6579c5bcd44fb43be75149ee1c683e 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 integer
  29. */
  30. public $min_year = 1000;
  31. /**
  32. * Maximum Year.
  33. *
  34. * @since 4.9.0
  35. * @var integer
  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 boolean
  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 boolean
  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 boolean
  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'] = intval( $this->max_year );
  75. $data['minYear'] = intval( $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. * @see touch_time()
  171. *
  172. * @global WP_Locale $wp_locale WordPress date and time locale object.
  173. *
  174. * @return array
  175. */
  176. public function get_month_choices() {
  177. global $wp_locale;
  178. $months = array();
  179. for ( $i = 1; $i < 13; $i++ ) {
  180. $month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
  181. /* translators: 1: Month number (01, 02, etc.), 2: Month abbreviation. */
  182. $months[ $i ]['text'] = sprintf( __( '%1$s-%2$s' ), $i, $month_text );
  183. $months[ $i ]['value'] = $i;
  184. }
  185. return array(
  186. 'month_choices' => $months,
  187. );
  188. }
  189. /**
  190. * Get timezone info.
  191. *
  192. * @since 4.9.0
  193. *
  194. * @return array abbr and description.
  195. */
  196. public function get_timezone_info() {
  197. $tz_string = get_option( 'timezone_string' );
  198. $timezone_info = array();
  199. if ( $tz_string ) {
  200. try {
  201. $tz = new DateTimezone( $tz_string );
  202. } catch ( Exception $e ) {
  203. $tz = '';
  204. }
  205. if ( $tz ) {
  206. $now = new DateTime( 'now', $tz );
  207. $formatted_gmt_offset = $this->format_gmt_offset( $tz->getOffset( $now ) / 3600 );
  208. $tz_name = str_replace( '_', ' ', $tz->getName() );
  209. $timezone_info['abbr'] = $now->format( 'T' );
  210. $timezone_info['description'] = sprintf(
  211. /* translators: 1: Timezone name, 2: Timezone abbreviation, 3: UTC abbreviation and offset, 4: UTC offset. */
  212. __( 'Your timezone is set to %1$s (%2$s), currently %3$s (Coordinated Universal Time %4$s).' ),
  213. $tz_name,
  214. '<abbr>' . $timezone_info['abbr'] . '</abbr>',
  215. '<abbr>UTC</abbr>' . $formatted_gmt_offset,
  216. $formatted_gmt_offset
  217. );
  218. } else {
  219. $timezone_info['description'] = '';
  220. }
  221. } else {
  222. $formatted_gmt_offset = $this->format_gmt_offset( intval( get_option( 'gmt_offset', 0 ) ) );
  223. $timezone_info['description'] = sprintf(
  224. /* translators: 1: UTC abbreviation and offset, 2: UTC offset. */
  225. __( 'Your timezone is set to %1$s (Coordinated Universal Time %2$s).' ),
  226. '<abbr>UTC</abbr>' . $formatted_gmt_offset,
  227. $formatted_gmt_offset
  228. );
  229. }
  230. return $timezone_info;
  231. }
  232. /**
  233. * Format GMT Offset.
  234. *
  235. * @since 4.9.0
  236. * @see wp_timezone_choice()
  237. *
  238. * @param float $offset Offset in hours.
  239. * @return string Formatted offset.
  240. */
  241. public function format_gmt_offset( $offset ) {
  242. if ( 0 <= $offset ) {
  243. $formatted_offset = '+' . (string) $offset;
  244. } else {
  245. $formatted_offset = (string) $offset;
  246. }
  247. $formatted_offset = str_replace(
  248. array( '.25', '.5', '.75' ),
  249. array( ':15', ':30', ':45' ),
  250. $formatted_offset
  251. );
  252. return $formatted_offset;
  253. }
  254. }