WP_Customize_Date_Time_Control
云策文档标注
概述
WP_Customize_Date_Time_Control 是 WordPress 自定义器中的一个日期时间控件类,继承自 WP_Customize_Control,用于在主题或插件中提供日期和时间选择功能。它支持多种配置选项,如年份范围、时间格式和时区信息,并通过 JavaScript 模板渲染内容。
关键要点
- 继承自 WP_Customize_Control,类型为 'date_time',用于自定义器中的日期时间输入。
- 提供属性如 min_year、max_year、allow_past_date、include_time 和 twelve_hour_format 来定制控件行为。
- 核心方法包括 json() 用于导出数据到 JavaScript,content_template() 用于渲染 JS 模板,以及辅助方法如 get_month_choices() 和 get_timezone_info()。
- 控件内容通过 JavaScript 模板渲染,render_content() 方法为空,不直接输出 HTML。
- 支持时区信息显示和格式化,包括 GMT 偏移量和时区描述。
代码示例
class WP_Customize_Date_Time_Control extends WP_Customize_Control {
public $type = 'date_time';
public $min_year = 1000;
public $max_year = 9999;
public $allow_past_date = true;
public $include_time = true;
public $twelve_hour_format = true;
public function render_content() {}
public function json() {
$data = parent::json();
$data['maxYear'] = (int) $this->max_year;
$data['minYear'] = (int) $this->min_year;
$data['allowPastDate'] = (bool) $this->allow_past_date;
$data['twelveHourFormat'] = (bool) $this->twelve_hour_format;
$data['includeTime'] = (bool) $this->include_time;
return $data;
}
public function content_template() {
// JS 模板代码,用于渲染控件界面
}
public function get_month_choices() {
// 生成月份选项
}
public function get_timezone_info() {
// 获取时区信息
}
public function format_gmt_offset($offset) {
// 格式化 GMT 偏移量
}
}注意事项
- 控件依赖于 JavaScript 模板进行渲染,确保自定义器脚本正确加载。
- 时区信息基于 WordPress 设置,可通过 get_option('timezone_string') 或 gmt_offset 获取。
- 属性如 twelve_hour_format 仅影响显示格式,保存的值仍为 Y-m-d H:i:s 格式。
- 自 WordPress 4.9.0 版本引入,使用时需确保兼容性。
原文内容
Customize Date Time Control class.
Description
See also
Methods
| Name | Description |
|---|---|
| WP_Customize_Date_Time_Control::content_template | Renders a JS template for the content of date time control. |
| WP_Customize_Date_Time_Control::format_gmt_offset | Format GMT Offset. |
| WP_Customize_Date_Time_Control::get_month_choices | Generate options for the month Select. |
| WP_Customize_Date_Time_Control::get_timezone_info | Get timezone info. |
| WP_Customize_Date_Time_Control::json | Export data to JS. |
| WP_Customize_Date_Time_Control::render_content | Don’t render the control’s content – it’s rendered with a JS template. |
Source
class WP_Customize_Date_Time_Control extends WP_Customize_Control {
/**
* Customize control type.
*
* @since 4.9.0
* @var string
*/
public $type = 'date_time';
/**
* Minimum Year.
*
* @since 4.9.0
* @var int
*/
public $min_year = 1000;
/**
* Maximum Year.
*
* @since 4.9.0
* @var int
*/
public $max_year = 9999;
/**
* Allow past date, if set to false user can only select future date.
*
* @since 4.9.0
* @var bool
*/
public $allow_past_date = true;
/**
* Whether hours, minutes, and meridian should be shown.
*
* @since 4.9.0
* @var bool
*/
public $include_time = true;
/**
* If set to false the control will appear in 24 hour format,
* the value will still be saved in Y-m-d H:i:s format.
*
* @since 4.9.0
* @var bool
*/
public $twelve_hour_format = true;
/**
* Don't render the control's content - it's rendered with a JS template.
*
* @since 4.9.0
*/
public function render_content() {}
/**
* Export data to JS.
*
* @since 4.9.0
* @return array
*/
public function json() {
$data = parent::json();
$data['maxYear'] = (int) $this->max_year;
$data['minYear'] = (int) $this->min_year;
$data['allowPastDate'] = (bool) $this->allow_past_date;
$data['twelveHourFormat'] = (bool) $this->twelve_hour_format;
$data['includeTime'] = (bool) $this->include_time;
return $data;
}
/**
* Renders a JS template for the content of date time control.
*
* @since 4.9.0
*/
public function content_template() {
$data = array_merge( $this->json(), $this->get_month_choices() );
$timezone_info = $this->get_timezone_info();
$date_format = get_option( 'date_format' );
$date_format = preg_replace( '/(?<!--\\)[Yyo]/', '%1$s', $date_format );
$date_format = preg_replace( '/(?<!\\)[FmMn]/', '%2$s', $date_format );
$date_format = preg_replace( '/(?<!\\)[jd]/', '%3$s', $date_format );
// Fallback to ISO date format if year, month, or day are missing from the date format.
if ( 1 !== substr_count( $date_format, '%1$s' ) || 1 !== substr_count( $date_format, '%2$s' ) || 1 !== substr_count( $date_format, '%3$s' ) ) {
$date_format = '%1$s-%2$s-%3$s';
}
?-->
<# _.defaults( data, ); #>
<# var idPrefix = _.uniqueId( 'el' ) + '-'; #>
<# if ( data.label ) { #>
<span class="customize-control-title">
{{ data.label }}
</span>
<# } #>
<div class="customize-control-notifications-container"></div>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<# } #>
<div class="date-time-fields {{ data.includeTime ? 'includes-time' : '' }}">
<fieldset class="day-row">
<legend class="title-day {{ ! data.includeTime ? 'screen-reader-text' : '' }}"></legend>
<div class="day-fields clear">
<label for="{{ idPrefix }}date-time-month" class="screen-reader-text">
</label>
<select id="{{ idPrefix }}date-time-month" class="date-input month" data-component="month">
<# _.each( data.month_choices, function( choice ) {
if ( _.isObject( choice ) && ! _.isUndefined( choice.text ) && ! _.isUndefined( choice.value ) ) {
text = choice.text;
value = choice.value;
}
#>
<option value="{{ value }}" >
{{ text }}
</option>
<# } ); #>
</select>
<label for="{{ idPrefix }}date-time-day" class="screen-reader-text">
</label>
<input id="{{ idPrefix }}date-time-day" type="number" size="2" autocomplete="off" class="date-input day tiny-text" data-component="day" min="1" max="31" />
<label for="{{ idPrefix }}date-time-year" class="screen-reader-text">
</label>
<input id="{{ idPrefix }}date-time-year" type="number" size="4" autocomplete="off" class="date-input year tiny-text" data-component="year" min="{{ data.minYear }}" max="{{ data.maxYear }}">
</div>
</fieldset>
<# if ( data.includeTime ) { #>
<fieldset class="time-row clear">
<legend class="title-time"></legend>
<div class="time-fields clear">
<label for="{{ idPrefix }}date-time-hour" class="screen-reader-text">
</label>
<# var maxHour = data.twelveHourFormat ? 12 : 23; #>
<# var minHour = data.twelveHourFormat ? 1 : 0; #>
<input id="{{ idPrefix }}date-time-hour" type="number" size="2" autocomplete="off" class="date-input hour tiny-text" data-component="hour" min="{{ minHour }}" max="{{ maxHour }}">
:
<label for="{{ idPrefix }}date-time-minute" class="screen-reader-text">
</label>
<input id="{{ idPrefix }}date-time-minute" type="number" size="2" autocomplete="off" class="date-input minute tiny-text" data-component="minute" min="0" max="59">
<# if ( data.twelveHourFormat ) { #>
<label for="{{ idPrefix }}date-time-meridian" class="screen-reader-text">
</label>
<select id="{{ idPrefix }}date-time-meridian" class="date-input meridian" data-component="meridian">
<option value="am"></option>
<option value="pm"></option>
</select>
<# } #>
<p></p>
</div>
</fieldset>
<# } #>
</div>
get_month_abbrev( $wp_locale->get_month( $i ) );
/* translators: 1: Month number (01, 02, etc.), 2: Month abbreviation. */
$months[ $i ]['text'] = sprintf( __( '%1$s-%2$s' ), $i, $month_text );
$months[ $i ]['value'] = $i;
}
return array(
'month_choices' => $months,
);
}
/**
* Get timezone info.
*
* @since 4.9.0
*
* @return array {
* Timezone info. All properties are optional.
*
* @type string $abbr Timezone abbreviation. Examples: PST or CEST.
* @type string $description Human-readable timezone description as HTML.
* }
*/
public function get_timezone_info() {
$tz_string = get_option( 'timezone_string' );
$timezone_info = array();
if ( $tz_string ) {
try {
$tz = new DateTimeZone( $tz_string );
} catch ( Exception $e ) {
$tz = '';
}
if ( $tz ) {
$now = new DateTime( 'now', $tz );
$formatted_gmt_offset = $this->format_gmt_offset( $tz->getOffset( $now ) / HOUR_IN_SECONDS );
$tz_name = str_replace( '_', ' ', $tz->getName() );
$timezone_info['abbr'] = $now->format( 'T' );
$timezone_info['description'] = sprintf(
/* translators: 1: Timezone name, 2: Timezone abbreviation, 3: UTC abbreviation and offset, 4: UTC offset. */
__( 'Your timezone is set to %1$s (%2$s), currently %3$s (Coordinated Universal Time %4$s).' ),
$tz_name,
'<abbr>' . $timezone_info['abbr'] . '</abbr>',
'<abbr>UTC</abbr>' . $formatted_gmt_offset,
$formatted_gmt_offset
);
} else {
$timezone_info['description'] = '';
}
} else {
$formatted_gmt_offset = $this->format_gmt_offset( (int) get_option( 'gmt_offset', 0 ) );
$timezone_info['description'] = sprintf(
/* translators: 1: UTC abbreviation and offset, 2: UTC offset. */
__( 'Your timezone is set to %1$s (Coordinated Universal Time %2$s).' ),
'<abbr>UTC</abbr>' . $formatted_gmt_offset,
$formatted_gmt_offset
);
}
return $timezone_info;
}
/**
* Format GMT Offset.
*
* @since 4.9.0
*
* @see wp_timezone_choice()
*
* @param float $offset Offset in hours.
* @return string Formatted offset.
*/
public function format_gmt_offset( $offset ) {
if ( 0 <= $offset ) {
$formatted_offset = '+' . (string) $offset;
} else {
$formatted_offset = (string) $offset;
}
$formatted_offset = str_replace(
array( '.25', '.5', '.75' ),
array( ':15', ':30', ':45' ),
$formatted_offset
);
return $formatted_offset;
}
}
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |