[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/customize/ -> class-wp-customize-date-time-control.php (source)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7