PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.8
Pods – Custom Content Types and Fields v3.3.8
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / time.php
pods / classes / fields Last commit date
avatar.php 4 months ago boolean.php 4 months ago code.php 4 months ago color.php 4 months ago comment.php 4 months ago currency.php 4 months ago date.php 4 months ago datetime.php 4 months ago email.php 4 months ago file.php 4 months ago heading.php 4 months ago html.php 4 months ago link.php 4 months ago number.php 4 months ago oembed.php 4 months ago paragraph.php 4 months ago password.php 4 months ago phone.php 4 months ago pick.php 4 months ago slug.php 4 months ago taxonomy.php 4 months ago text.php 4 months ago time.php 4 months ago website.php 4 months ago wysiwyg.php 4 months ago
time.php
250 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Fields
10 */
11 class PodsField_Time extends PodsField_DateTime {
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $group = 'Date / Time';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $type = 'time';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $label = 'Time';
27
28 /**
29 * {@inheritdoc}
30 */
31 public static $prepare = '%s';
32
33 /**
34 * Storage format.
35 *
36 * @var string
37 * @since 2.7.0
38 */
39 public static $storage_format = 'H:i:s';
40
41 /**
42 * The default empty value (database)
43 *
44 * @var string
45 * @since 2.7.0
46 */
47 public static $empty_value = '';
48
49 /**
50 * {@inheritdoc}
51 */
52 public function setup() {
53
54 static::$group = __( 'Date / Time', 'pods' );
55 static::$label = __( 'Time', 'pods' );
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function options() {
62
63 $options = [
64 static::$type . '_type' => [
65 'label' => __( 'Time Format Type', 'pods' ),
66 'default' => '12',
67 // Backwards compatibility
68 'type' => 'pick',
69 'help' => __( 'WordPress Default is the format used in Settings, General under "Time Format".', 'pods' ) . '<br>' . __( '12/24 hour will allow you to select from a list of commonly used time formats.', 'pods' ) . '<br>' . __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ),
70 'data' => [
71 'wp' => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'time_format' ) ),
72 '12' => __( '12 hour', 'pods' ),
73 '24' => __( '24 hour', 'pods' ),
74 'custom' => __( 'Custom format', 'pods' ),
75 ],
76 'pick_format_single' => 'dropdown',
77 'pick_show_select_text' => 0,
78 'dependency' => true,
79 ],
80 static::$type . '_format_custom' => [
81 'label' => __( 'Time format for display', 'pods' ),
82 'depends-on' => [ static::$type . '_type' => 'custom' ],
83 'default' => '',
84 'type' => 'text',
85 'help' => sprintf(
86 '<a href="http://php.net/manual/function.date.php" target="_blank" rel="noopener noreferrer">%s</a>',
87 esc_html__( 'PHP date documentation', 'pods' )
88 ),
89 ],
90 static::$type . '_format_custom_js' => [
91 'label' => __( 'Time format for input', 'pods' ),
92 'depends-on' => [ static::$type . '_type' => 'custom' ],
93 'default' => '',
94 'type' => 'text',
95 'help' => sprintf(
96 '<a href="http://trentrichardson.com/examples/timepicker/#tp-formatting" target="_blank" rel="noopener noreferrer">%1$s</a><br />%2$s',
97 esc_html__( 'jQuery UI timepicker documentation', 'pods' ),
98 esc_html__( 'Leave empty to auto-generate from PHP format.', 'pods' )
99 ),
100 ],
101 static::$type . '_format' => [
102 'label' => __( 'Time Format', 'pods' ),
103 'depends-on' => [ static::$type . '_type' => '12' ],
104 'default' => 'h_mma',
105 'type' => 'pick',
106 'data' => [
107 'h_mm_A' => date_i18n( 'g:i A' ),
108 'h_mm_ss_A' => date_i18n( 'g:i:s A' ),
109 'hh_mm_A' => date_i18n( 'h:i A' ),
110 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
111 'h_mma' => date_i18n( 'g:ia' ),
112 'hh_mma' => date_i18n( 'h:ia' ),
113 'h_mm' => date_i18n( 'g:i' ),
114 'h_mm_ss' => date_i18n( 'g:i:s' ),
115 'hh_mm' => date_i18n( 'h:i' ),
116 'hh_mm_ss' => date_i18n( 'h:i:s' ),
117 ],
118 'pick_format_single' => 'dropdown',
119 'pick_show_select_text' => 0,
120 'dependency' => true,
121 ],
122 static::$type . '_format_24' => [
123 'label' => __( 'Time Format', 'pods' ),
124 'depends-on' => [ static::$type . '_type' => '24' ],
125 'default' => 'hh_mm',
126 'type' => 'pick',
127 'data' => [
128 'hh_mm' => date_i18n( 'H:i' ),
129 'hh_mm_ss' => date_i18n( 'H:i:s' ),
130 ],
131 'pick_format_single' => 'dropdown',
132 'pick_show_select_text' => 0,
133 ],
134 static::$type . '_allow_empty' => [
135 'label' => __( 'Allow empty value', 'pods' ),
136 'default' => 1,
137 'type' => 'boolean',
138 ],
139 static::$type . '_html5' => [
140 'label' => __( 'Enable HTML5 Input Field', 'pods' ),
141 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
142 'type' => 'boolean',
143 ],
144 ];
145
146 $options[ static::$type . '_type' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ static::$type . '_type' ]['default'] );
147 $options[ static::$type . '_format' ]['data'] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ static::$type . '_format' ]['data'] );
148 $options[ static::$type . '_format' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ static::$type . '_format' ]['default'] );
149 $options[ static::$type . '_format_24' ]['data'] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ static::$type . '_format_24' ]['data'] );
150 $options[ static::$type . '_format_24' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ static::$type . '_format_24' ]['default'] );
151
152 return $options;
153 }
154
155 /**
156 * {@inheritdoc}
157 */
158 public function schema( $options = null ) {
159
160 $schema = 'TIME NOT NULL default \'00:00:00\'';
161
162 return $schema;
163 }
164
165 /**
166 * {@inheritdoc}
167 */
168 public function is_empty( $value = null ) {
169
170 $value = trim( $value );
171
172 return empty( $value );
173 }
174
175 /**
176 * {@inheritdoc}
177 */
178 public function format_display( $options, $js = false ) {
179
180 if ( $js && 'custom' === pods_v( static::$type . '_type', $options, 'format' ) ) {
181 $format = $this->format_datetime( $options, $js );
182
183 return $this->convert_format( $format, [ 'source' => 'jquery_ui', 'type' => 'time' ] );
184 }
185
186 return parent::format_display( $options, $js );
187 }
188
189 /**
190 * {@inheritdoc}
191 */
192 public function format_datetime( $options, $js = false ) {
193
194 return $this->format_time( $options, $js );
195 }
196
197 /**
198 * Build time format string based on options
199 *
200 * @param array $options Field options.
201 * @param bool $js Return formatted from jQuery UI format? (only for custom formats).
202 *
203 * @return string
204 * @since 2.7.0
205 */
206 public function format_time( $options, $js = false ) {
207
208 switch ( (string) pods_v( static::$type . '_type', $options, '12', true ) ) {
209 case '12':
210 $time_format = $this->get_time_formats( $js );
211
212 $format = $time_format[ pods_v( static::$type . '_format', $options, 'hh_mm', true ) ];
213
214 break;
215 case '24':
216 $time_format_24 = $this->get_time_formats_24( $js );
217
218 $format = $time_format_24[ pods_v( static::$type . '_format_24', $options, 'hh_mm', true ) ];
219
220 break;
221 case 'custom':
222 if ( ! $js ) {
223 $format = pods_v( static::$type . '_format_custom', $options, '' );
224 } else {
225 $format = pods_v( static::$type . '_format_custom_js', $options, '' );
226
227 if ( empty( $format ) ) {
228 $format = pods_v( static::$type . '_format_custom', $options, '' );
229
230 if ( $js ) {
231 $format = $this->convert_format( $format, [ 'source' => 'php', 'type' => 'time' ] );
232 }
233 }
234 }
235
236 break;
237 default:
238 $format = get_option( 'time_format' );
239
240 if ( $js ) {
241 $format = $this->convert_format( $format, [ 'source' => 'php', 'type' => 'time' ] );
242 }
243
244 break;
245 }//end switch
246
247 return $format;
248 }
249 }
250