| 1 |
<?php |
| 2 |
defined('ABSPATH') || die(); |
| 3 |
|
| 4 |
class HashFormFieldTime extends HashFormFieldType { |
| 5 |
|
| 6 |
protected $type = 'time'; |
| 7 |
|
| 8 |
protected function field_settings_for_type() { |
| 9 |
return array( |
| 10 |
'clear_on_focus' => true, |
| 11 |
'invalid' => true, |
| 12 |
); |
| 13 |
} |
| 14 |
|
| 15 |
protected function extra_field_default_opts() { |
| 16 |
return array( |
| 17 |
'step' => '60', |
| 18 |
'min_time' => '00:00', |
| 19 |
'max_time' => '23:59', |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
protected function input_html() { |
| 24 |
$field = $this->get_field(); |
| 25 |
$step = isset($field['step']) ? intval($field['step']) : '60'; |
| 26 |
$step = $step ? $step : 60; |
| 27 |
?> |
| 28 |
<input type="text" class="hf-timepicker" data-step="<?php echo absint($step); ?>" data-min-time="<?php echo isset($field['min_time']) ? esc_attr($field['min_time']) : '00:00'; ?>" data-max-time="<?php echo isset($field['max_time']) ? esc_attr($field['max_time']) : '23:59'; ?>" <?php $this->field_attrs(); ?>> |
| 29 |
<?php |
| 30 |
} |
| 31 |
|
| 32 |
protected function prepare_esc_value() { |
| 33 |
$field = $this->get_field(); |
| 34 |
$value = isset($field['default_value']) ? $field['default_value'] : ''; |
| 35 |
if (is_array($value)) { |
| 36 |
$value = implode(', ', $value); |
| 37 |
} |
| 38 |
|
| 39 |
if (strpos($value, '<') !== false) { |
| 40 |
$value = htmlentities($value); |
| 41 |
} |
| 42 |
|
| 43 |
if (!$value) { |
| 44 |
return $value; |
| 45 |
} |
| 46 |
|
| 47 |
$time_value = explode(":", $value); |
| 48 |
$hour = absint($time_value[0]); |
| 49 |
$hour = $hour % 24; |
| 50 |
$ampm = ($hour < 12 ? "am" : "pm"); |
| 51 |
|
| 52 |
$hour = $hour % 12; |
| 53 |
$hour = $hour ? $hour : 12; |
| 54 |
|
| 55 |
$minute = isset($time_value[1]) ? absint($time_value[1]) : 0; |
| 56 |
$minute = $minute % 60; |
| 57 |
|
| 58 |
if ($minute < 10) { |
| 59 |
$minute = '0' . absint($minute); |
| 60 |
} |
| 61 |
|
| 62 |
return $hour . ':' . $minute . $ampm; |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|