79 * @return array |
79 * @return array |
80 */ |
80 */ |
81 public function json() { |
81 public function json() { |
82 $data = parent::json(); |
82 $data = parent::json(); |
83 |
83 |
84 $data['maxYear'] = intval( $this->max_year ); |
84 $data['maxYear'] = intval( $this->max_year ); |
85 $data['minYear'] = intval( $this->min_year ); |
85 $data['minYear'] = intval( $this->min_year ); |
86 $data['allowPastDate'] = (bool) $this->allow_past_date; |
86 $data['allowPastDate'] = (bool) $this->allow_past_date; |
87 $data['twelveHourFormat'] = (bool) $this->twelve_hour_format; |
87 $data['twelveHourFormat'] = (bool) $this->twelve_hour_format; |
88 $data['includeTime'] = (bool) $this->include_time; |
88 $data['includeTime'] = (bool) $this->include_time; |
89 |
89 |
90 return $data; |
90 return $data; |
91 } |
91 } |
92 |
92 |
93 /** |
93 /** |
94 * Renders a JS template for the content of date time control. |
94 * Renders a JS template for the content of date time control. |
95 * |
95 * |
96 * @since 4.9.0 |
96 * @since 4.9.0 |
97 */ |
97 */ |
98 public function content_template() { |
98 public function content_template() { |
99 $data = array_merge( $this->json(), $this->get_month_choices() ); |
99 $data = array_merge( $this->json(), $this->get_month_choices() ); |
100 $timezone_info = $this->get_timezone_info(); |
100 $timezone_info = $this->get_timezone_info(); |
101 |
101 |
102 $date_format = get_option( 'date_format' ); |
102 $date_format = get_option( 'date_format' ); |
103 $date_format = preg_replace( '/(?<!\\\\)[Yyo]/', '%1$s', $date_format ); |
103 $date_format = preg_replace( '/(?<!\\\\)[Yyo]/', '%1$s', $date_format ); |
104 $date_format = preg_replace( '/(?<!\\\\)[FmMn]/', '%2$s', $date_format ); |
104 $date_format = preg_replace( '/(?<!\\\\)[FmMn]/', '%2$s', $date_format ); |
187 * Based on touch_time(). |
187 * Based on touch_time(). |
188 * |
188 * |
189 * @since 4.9.0 |
189 * @since 4.9.0 |
190 * @see touch_time() |
190 * @see touch_time() |
191 * |
191 * |
|
192 * @global WP_Locale $wp_locale |
|
193 * |
192 * @return array |
194 * @return array |
193 */ |
195 */ |
194 public function get_month_choices() { |
196 public function get_month_choices() { |
195 global $wp_locale; |
197 global $wp_locale; |
196 $months = array(); |
198 $months = array(); |
197 for ( $i = 1; $i < 13; $i++ ) { |
199 for ( $i = 1; $i < 13; $i++ ) { |
198 $month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ); |
200 $month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ); |
199 |
201 |
200 /* translators: 1: month number (01, 02, etc.), 2: month abbreviation */ |
202 /* translators: 1: month number (01, 02, etc.), 2: month abbreviation */ |
201 $months[ $i ]['text'] = sprintf( __( '%1$s-%2$s' ), $i, $month_text ); |
203 $months[ $i ]['text'] = sprintf( __( '%1$s-%2$s' ), $i, $month_text ); |
202 $months[ $i ]['value'] = $i; |
204 $months[ $i ]['value'] = $i; |
203 } |
205 } |
204 return array( |
206 return array( |
205 'month_choices' => $months, |
207 'month_choices' => $months, |
206 ); |
208 ); |
212 * @since 4.9.0 |
214 * @since 4.9.0 |
213 * |
215 * |
214 * @return array abbr and description. |
216 * @return array abbr and description. |
215 */ |
217 */ |
216 public function get_timezone_info() { |
218 public function get_timezone_info() { |
217 $tz_string = get_option( 'timezone_string' ); |
219 $tz_string = get_option( 'timezone_string' ); |
218 $timezone_info = array(); |
220 $timezone_info = array(); |
219 |
221 |
220 if ( $tz_string ) { |
222 if ( $tz_string ) { |
221 try { |
223 try { |
222 $tz = new DateTimezone( $tz_string ); |
224 $tz = new DateTimezone( $tz_string ); |
223 } catch ( Exception $e ) { |
225 } catch ( Exception $e ) { |
224 $tz = ''; |
226 $tz = ''; |
225 } |
227 } |
226 |
228 |
227 if ( $tz ) { |
229 if ( $tz ) { |
228 $now = new DateTime( 'now', $tz ); |
230 $now = new DateTime( 'now', $tz ); |
229 $formatted_gmt_offset = sprintf( 'UTC%s', $this->format_gmt_offset( $tz->getOffset( $now ) / 3600 ) ); |
231 $formatted_gmt_offset = sprintf( 'UTC%s', $this->format_gmt_offset( $tz->getOffset( $now ) / 3600 ) ); |
230 $tz_name = str_replace( '_', ' ', $tz->getName() ); |
232 $tz_name = str_replace( '_', ' ', $tz->getName() ); |
231 $timezone_info['abbr'] = $now->format( 'T' ); |
233 $timezone_info['abbr'] = $now->format( 'T' ); |
232 |
234 |
233 /* translators: 1: timezone name, 2: timezone abbreviation, 3: gmt offset */ |
235 /* translators: 1: timezone name, 2: timezone abbreviation, 3: gmt offset */ |
234 $timezone_info['description'] = sprintf( __( 'Timezone is %1$s (%2$s), currently %3$s.' ), $tz_name, $timezone_info['abbr'], $formatted_gmt_offset ); |
236 $timezone_info['description'] = sprintf( __( 'Timezone is %1$s (%2$s), currently %3$s.' ), $tz_name, $timezone_info['abbr'], $formatted_gmt_offset ); |
235 } else { |
237 } else { |
236 $timezone_info['description'] = ''; |
238 $timezone_info['description'] = ''; |
237 } |
239 } |
238 } else { |
240 } else { |
239 $formatted_gmt_offset = $this->format_gmt_offset( intval( get_option( 'gmt_offset', 0 ) ) ); |
241 $formatted_gmt_offset = $this->format_gmt_offset( intval( get_option( 'gmt_offset', 0 ) ) ); |
240 $timezone_info['abbr'] = sprintf( 'UTC%s', $formatted_gmt_offset ); |
242 $timezone_info['abbr'] = sprintf( 'UTC%s', $formatted_gmt_offset ); |
241 |
243 |
242 /* translators: %s: UTC offset */ |
244 /* translators: %s: UTC offset */ |
243 $timezone_info['description'] = sprintf( __( 'Timezone is %s.' ), $timezone_info['abbr'] ); |
245 $timezone_info['description'] = sprintf( __( 'Timezone is %s.' ), $timezone_info['abbr'] ); |
244 } |
246 } |