[ Index ]

PHP Cross Reference of WordPress

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 int
  32       */
  33      public $min_year = 1000;
  34  
  35      /**
  36       * Maximum Year.
  37       *
  38       * @since 4.9.0
  39       * @var int
  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 bool
  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 bool
  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 bool
  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']          = (int) $this->max_year;
  85          $data['minYear']          = (int) $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       *
 191       * @see touch_time()
 192       *
 193       * @global WP_Locale $wp_locale WordPress date and time locale object.
 194       *
 195       * @return array
 196       */
 197  	public function get_month_choices() {
 198          global $wp_locale;
 199          $months = array();
 200          for ( $i = 1; $i < 13; $i++ ) {
 201              $month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
 202  
 203              /* translators: 1: Month number (01, 02, etc.), 2: Month abbreviation. */
 204              $months[ $i ]['text']  = sprintf( __( '%1$s-%2$s' ), $i, $month_text );
 205              $months[ $i ]['value'] = $i;
 206          }
 207          return array(
 208              'month_choices' => $months,
 209          );
 210      }
 211  
 212      /**
 213       * Get timezone info.
 214       *
 215       * @since 4.9.0
 216       *
 217       * @return array {
 218       *     Timezone info. All properties are optional.
 219       *
 220       *     @type string $abbr        Timezone abbreviation. Examples: PST or CEST.
 221       *     @type string $description Human-readable timezone description as HTML.
 222       * }
 223       */
 224  	public function get_timezone_info() {
 225          $tz_string     = get_option( 'timezone_string' );
 226          $timezone_info = array();
 227  
 228          if ( $tz_string ) {
 229              try {
 230                  $tz = new DateTimeZone( $tz_string );
 231              } catch ( Exception $e ) {
 232                  $tz = '';
 233              }
 234  
 235              if ( $tz ) {
 236                  $now                   = new DateTime( 'now', $tz );
 237                  $formatted_gmt_offset  = $this->format_gmt_offset( $tz->getOffset( $now ) / 3600 );
 238                  $tz_name               = str_replace( '_', ' ', $tz->getName() );
 239                  $timezone_info['abbr'] = $now->format( 'T' );
 240  
 241                  $timezone_info['description'] = sprintf(
 242                      /* translators: 1: Timezone name, 2: Timezone abbreviation, 3: UTC abbreviation and offset, 4: UTC offset. */
 243                      __( 'Your timezone is set to %1$s (%2$s), currently %3$s (Coordinated Universal Time %4$s).' ),
 244                      $tz_name,
 245                      '<abbr>' . $timezone_info['abbr'] . '</abbr>',
 246                      '<abbr>UTC</abbr>' . $formatted_gmt_offset,
 247                      $formatted_gmt_offset
 248                  );
 249              } else {
 250                  $timezone_info['description'] = '';
 251              }
 252          } else {
 253              $formatted_gmt_offset = $this->format_gmt_offset( (int) get_option( 'gmt_offset', 0 ) );
 254  
 255              $timezone_info['description'] = sprintf(
 256                  /* translators: 1: UTC abbreviation and offset, 2: UTC offset. */
 257                  __( 'Your timezone is set to %1$s (Coordinated Universal Time %2$s).' ),
 258                  '<abbr>UTC</abbr>' . $formatted_gmt_offset,
 259                  $formatted_gmt_offset
 260              );
 261          }
 262  
 263          return $timezone_info;
 264      }
 265  
 266      /**
 267       * Format GMT Offset.
 268       *
 269       * @since 4.9.0
 270       *
 271       * @see wp_timezone_choice()
 272       *
 273       * @param float $offset Offset in hours.
 274       * @return string Formatted offset.
 275       */
 276  	public function format_gmt_offset( $offset ) {
 277          if ( 0 <= $offset ) {
 278              $formatted_offset = '+' . (string) $offset;
 279          } else {
 280              $formatted_offset = (string) $offset;
 281          }
 282          $formatted_offset = str_replace(
 283              array( '.25', '.5', '.75' ),
 284              array( ':15', ':30', ':45' ),
 285              $formatted_offset
 286          );
 287          return $formatted_offset;
 288      }
 289  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1