-
Notifications
You must be signed in to change notification settings - Fork 27
/
date-time-picker-v4.php
479 lines (437 loc) · 16.7 KB
/
date-time-picker-v4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
if ( ! class_exists( 'ACFFieldDateTimePicker' ) ) :
class ACFFieldDateTimePicker extends acf_field {
// vars
var $settings // will hold info such as dir / path
, $defaults // will hold default field options
, $domain // holds the language domain
, $lang;
/**
* __construct
*
* Set name / label needed for actions / filters
*
* @since 3.6
* @date 23/01/13
*/
function __construct() {
// vars
$this->name = 'date_time_picker';
$this->label = __( 'Date and Time Picker' );
$this->category = __( 'jQuery', 'acf-field-date-time-picker' ); // Basic, Content, Choice, etc
$this->defaults = array(
'label' => __( 'Choose Time', 'acf-field-date-time-picker' ),
'time_format' => 'h:mm tt',
'show_date' => 'true',
'date_format' => 'm/d/y',
'show_week_number' => 'false',
'picker' => 'slider',
'save_as_timestamp' => 'true',
'get_as_timestamp' => 'false',
);
// do not delete!
parent::__construct();
// settings
$this->settings = array(
'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ), // @codingStandardsIgnoreLine
'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ), // @codingStandardsIgnoreLine
'version' => ACFFIELDDATETIMEPICKER_VERSION,
);
}
/**
* create_options()
*
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field - an array holding all the field's data
*/
function create_options( $field ) {
$field = array_merge( $this->defaults, $field );
$key = $field['name'];
?>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_choice">
<td class="label">
<label for=""><?php _e( 'Date and Time Picker?', 'acf-field-date-time-picker' ); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'radio',
'name' => 'fields[' . $key . '][show_date]',
'value' => $field['show_date'],
'layout' => 'horizontal',
'choices' => array(
'true' => __( 'Date and Time Picker', 'acf-field-date-time-picker' ),
'false' => __( 'Time Picker', 'acf-field-date-time-picker' ),
),
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_dateformat">
<td class="label">
<label><?php _e( 'Date Format', 'acf-field-date-time-picker' ); ?></label>
<p class="description"><?php printf( __( 'eg. mm/dd/yy. read more about <a href="%s" target="_blank">formatting date</a>', 'acf-field-date-time-picker' ), 'http://docs.jquery.com/UI/Datepicker/formatDate' ); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'text',
'name' => 'fields[' . $key . '][date_format]',
'value' => $field['date_format'],
));
/*
do_action('acf/create_field', array(
'type' => 'select',
'name' => 'fields['.$key.'][date_format]',
'value' => $field['date_format'],
'choices' => array(
'm/d/y' => 'm/d/y (5/27/13)'
, 'mm/dd/yy' => 'mm/dd/yy (05/27/2013)'
, 'yy/mm/dd' => 'yy/mm/dd (2013/05/27)'
, 'yy-mm-dd' => 'yy-mm-dd (2013-05-27)'
, 'dd.mm.yy' => 'dd.mm.yy (27.05.2013)'
, 'dd-mm-yy' => 'dd-mm-yy (27-05-2013)'
, 'yy-M-dd' => 'yy-M-dd (2013-May-27)'
)
));
*/
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_timeformat">
<td class="label">
<label><?php _e( 'Time Format', 'acf-field-date-time-picker' ); ?></label>
<p class="description"><?php printf( __( 'eg. hh:mm. read more about <a href="%s" target="_blank">formatting time</a>', 'acf-field-date-time-picker' ), 'http://trentrichardson.com/examples/timepicker/#tp-formatting' ); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'text',
'name' => 'fields[' . $key . '][time_format]',
'value' => $field['time_format'],
));
/*
do_action('acf/create_field', array(
'type' => 'select',
'name' => 'fields['.$key.'][time_format]',
'value' => $field['time_format'],
'choices' => array(
'h:mm tt' => 'h:mm tt (9:59 am)'
, 'hh:mm tt' => 'hh:mm tt (09:59 am)'
, 'H:mm' => 'H:mm (9:59)'
, 'HH:mm' => 'HH:mm (09:59)'
)
));
*/
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number">
<td class="label">
<label for=""><?php _e( 'Display Week Number?', 'acf-field-date-time-picker' ); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'radio',
'name' => 'fields[' . $key . '][show_week_number]',
'value' => $field['show_week_number'],
'layout' => 'horizontal',
'choices' => array(
'true' => __( 'Yes', 'acf-field-date-time-picker' ),
'false' => __( 'No', 'acf-field-date-time-picker' ),
),
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number">
<td class="label">
<label for=""><?php _e( 'Time Picker style?', 'acf-field-date-time-picker' ); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'radio',
'name' => 'fields[' . $key . '][picker]',
'value' => $field['picker'],
'layout' => 'horizontal',
'choices' => array(
'slider' => __( 'Slider', 'acf-field-date-time-picker' ),
'select' => __( 'Dropdown', 'acf-field-date-time-picker' ),
),
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number">
<td class="label">
<label for=""><?php _e( 'Save as timestamp?', 'acf-field-date-time-picker' ); ?></label>
<p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "No" if you need a date and time format not supported by <a href="%s" target="_blank">strtotime</a>', 'acf-field-date-time-picker' ), 'http://php.net/manual/en/function.strtotime.php' ); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'radio',
'name' => 'fields[' . $key . '][save_as_timestamp]',
'value' => $field['save_as_timestamp'],
'layout' => 'horizontal',
'choices' => array(
'true' => __( 'Yes', 'acf-field-date-time-picker' ),
'false' => __( 'No', 'acf-field-date-time-picker' ),
),
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number">
<td class="label">
<label for=""><?php _e( 'Get field as timestamp?', 'acf-field-date-time-picker' ); ?></label>
<p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "Yes" if you need get the date and time field as a timestamp using <a href="%1$s" target="_blank">the_field()</a> or <a href="%2$s" target="_blank">get_field()</a> ', 'acf-field-date-time-picker' ), 'http://www.advancedcustomfields.com/resources/functions/the_field/',
'http://www.advancedcustomfields.com/resources/functions/get_field/'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array( // @codingStandardsIgnoreLine
'type' => 'radio',
'name' => 'fields[' . $key . '][get_as_timestamp]',
'value' => $field['get_as_timestamp'],
'layout' => 'horizontal',
'choices' => array(
'true' => __( 'Yes', 'acf-field-date-time-picker' ),
'false' => __( 'No', 'acf-field-date-time-picker' ),
),
));
?>
</td>
</tr>
<?php
}
/**
* create_field()
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function create_field( $field ) {
if ( 'true' !== $field['show_date'] ) {
echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-time_format="' . $field['time_format'] . '" title="' . $field['label'] . '" />';
} else {
echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-date_format="' . $field['date_format'] . '" data-time_format="' . $field['time_format'] . '" data-show_week_number="' . $field['show_week_number'] . '" title="' . $field['label'] . '" />';
}
}
//function load_field_defaults( $field ) { return $field; }
function format_value( $value, $post_id, $field ) {
$field = array_merge( $this->defaults, $field );
if ( '' != $value && 'true' == $field['save_as_timestamp'] && $this->isValidTimeStamp( $value ) ) {
if ( 'true' == $field['show_date'] ) {
$value = date_i18n( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value );
} else {
$value = date_i18n( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value );
}
}
return $value;
}
function format_value_for_api( $value, $post_id, $field ) {
$field = array_merge( $this->defaults, $field );
if ( '' != $value && 'true' == $field['save_as_timestamp'] && 'true' != $field['get_as_timestamp'] && $this->isValidTimeStamp( $value ) ) {
if ( 'true' == $field['show_date'] ) {
$value = date_i18n( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value );
} else {
$value = date_i18n( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value );
}
}
return $value;
}
function js_to_php_dateformat( $date_format ) {
$chars = array(
// Day
'dd' => 'd',
'd' => 'j',
'DD' => 'l',
'D' => 'D',
'o' => 'z',
// Month
'mm' => 'm',
'm' => 'n',
'MM' => 'F',
'M' => 'M',
// Year
'yy' => 'Y',
'y' => 'y',
);
return strtr( (string) $date_format, $chars );
}
function js_to_php_timeformat( $time_format ) {
$chars = array(
//hour
'HH' => 'H',
'H' => 'G',
'hh' => 'h',
'h' => 'g',
//minute
'mm' => 'i',
'm' => 'i',
//second
'ss' => 's',
's' => 's',
//am/pm
'TT' => 'A',
'T' => 'A',
'tt' => 'a',
't' => 'a',
);
return strtr( (string) $time_format, $chars );
}
function isValidTimeStamp( $timestamp ) {
return ( (string) (int) $timestamp === (string) $timestamp );
}
/**
* update_value()
*
* This filter is appied to the $value before it is updated in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
function update_value( $value, $post_id, $field ) {
$field = array_merge( $this->defaults, $field );
if ( '' != $value && 'true' == $field['save_as_timestamp'] ) {
if ( preg_match( '/^dd?\//', $field['date_format'] ) ) { //if start with dd/ or d/ (not supported by strtotime())
$value = str_replace( '/', '-', $value );
}
$value = strtotime( $value );
}
return $value;
}
/**
* input_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
* Use this action to add css + javascript to assist your create_field() action.
*
* $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
* @type action
* @since 3.6
* @date 23/01/13
*/
function input_admin_enqueue_scripts() {
global $wp_locale;
$has_locale = false;
$js_locale = $this->get_js_locale( get_locale() );
wp_enqueue_script('jquery-ui-timepicker', $this->settings['dir'] . 'js/jquery-ui-timepicker-addon.js', array(
'acf-input',
'jquery-ui-slider',
), $this->settings['version'], true);
if ( file_exists( $this->settings['path'] . '/js/localization/jquery-ui-timepicker-' . $js_locale . '.js' ) ) {
wp_enqueue_script('timepicker-localization', $this->settings['dir'] . 'js/localization/jquery-ui-timepicker-' . $js_locale . '.js', array(
'jquery-ui-timepicker'
), $this->settings['version'], true);
wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array(
'timepicker-localization'
), $this->settings['version'], true);
$has_locale = true;
} else {
wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array(
'jquery-ui-timepicker'
), $this->settings['version'], true);
}
if ( ! $has_locale && 'en' != $js_locale ) {
$timepicker_locale = array(
'closeText' => __( 'Done', 'acf-field-date-time-picker' ),
'currentText' => __( 'Today', 'acf-field-date-time-picker' ),
'prevText' => __( 'Prev', 'acf-field-date-time-picker' ),
'nextText' => __( 'Next', 'acf-field-date-time-picker' ),
'monthStatus' => __( 'Show a different month', 'acf-field-date-time-picker' ),
'weekHeader' => __( 'Wk', 'acf-field-date-time-picker' ),
'timeText' => __( 'Time', 'acf-field-date-time-picker' ),
'hourText' => __( 'Hour', 'acf-field-date-time-picker' ),
'minuteText' => __( 'Minute', 'acf-field-date-time-picker' ),
'secondText' => __( 'Second', 'acf-field-date-time-picker' ),
'millisecText' => __( 'Millisecond', 'acf-field-date-time-picker' ),
'timezoneText' => __( 'Time Zone', 'acf-field-date-time-picker' ),
'isRTL' => $wp_locale->is_rtl(),
);
}
$timepicker_wp_locale = array(
'monthNames' => $this->strip_array_indices( $wp_locale->month ),
'monthNamesShort' => $this->strip_array_indices( $wp_locale->month_abbrev ),
'dayNames' => $this->strip_array_indices( $wp_locale->weekday ),
'dayNamesShort' => $this->strip_array_indices( $wp_locale->weekday_abbrev ),
'dayNamesMin' => $this->strip_array_indices( $wp_locale->weekday_initial ),
'showMonthAfterYear' => false,
'showWeek' => false,
'firstDay' => get_option( 'start_of_week' ),
);
$l10n = (isset( $timepicker_locale )) ? array_merge( $timepicker_wp_locale, $timepicker_locale ) : $timepicker_wp_locale;
wp_localize_script( 'timepicker', 'timepicker_objectL10n', $l10n );
wp_enqueue_style('jquery-style', $this->settings['dir'] . 'css/jquery-ui.css', array(
'acf-datepicker'
), $this->settings['version']);
wp_enqueue_style('timepicker', $this->settings['dir'] . 'css/jquery-ui-timepicker-addon.css', array(
'jquery-style'
), $this->settings['version']);
}
/**
* helper function, see: http://www.renegadetechconsulting.com/tutorials/jquery-datepicker-and-wordpress-i18n
* @param array $ArrayToStrip
* @return array
*/
function strip_array_indices( $array_to_strip ) {
foreach ( $array_to_strip as $obj_array_item ) {
$new_array[] = $obj_array_item;
}
return $new_array;
}
function get_js_locale( $locale ) {
$dir_path = $this->settings['path'] . 'js/localization/';
$exclude_list = array( '.', '..' );
$languages = $this->ps_preg_filter( '/jquery-ui-timepicker-(.*?)\.js/', '$1', array_diff( scandir( $dir_path ), $exclude_list ) );
$locale = strtolower( str_replace( '_', '-', $locale ) );
if ( false !== strpos( $locale, '-' ) ) {
$l = explode( '-', $locale );
switch ( $l[0] ) {
case 'en':
case 'fr':
case 'de':
$pattern = array( '/' . $l[0] . '/' );
break;
default:
$pattern = array( '/' . $l[1] . '/' );
break;
}
} else {
$pattern = array( '/' . $locale . '/' );
}
$res = $this->ps_preg_filter( $pattern, '$0', $languages, -1, $count );
return ($count) ? implode( '', $res ) : 'en';
}
function ps_preg_filter( $pattern, $replace, $subject, $limit = -1, &$count = 0 ) {
if ( function_exists( 'preg_filter' ) ) {
return preg_filter( $pattern, $replace, $subject, $limit, $count );
} else {
return array_diff( preg_replace( $pattern, $replace, $subject, $limit, $count ), $subject );
}
}
}
// create field
new ACFFieldDateTimePicker();
endif;