1
1
<?php
2
-
3
2
/**
4
- * A Javascript date-picker which submits dates as unix timestamps .
3
+ * Class file for Fieldmanager_Datepicker .
5
4
*
6
5
* @package Fieldmanager_Field
7
6
*/
7
+
8
+ /**
9
+ * A JavaScript date-picker which submits dates as Unix timestamps.
10
+ */
8
11
class Fieldmanager_Datepicker extends Fieldmanager_Field {
9
12
10
13
/**
11
- * @var boolean
12
14
* Collect time info or just date info? Defaults to just date info.
15
+ *
16
+ * @var bool
13
17
*/
14
- public $ use_time = False ;
18
+ public $ use_time = false ;
15
19
16
20
/**
17
- * @var boolean
18
- * If true, and $use_time == true, and $date_element = 'dropdown', will render an 'AM' and 'PM' dropdown
21
+ * If true, and $use_time == true, and $date_element = 'dropdown', will render an 'AM' and 'PM' dropdown.
22
+ *
23
+ * @var bool
19
24
*/
20
- public $ use_am_pm = True ;
25
+ public $ use_am_pm = true ;
21
26
22
27
/**
28
+ * PHP date format, only used for rendering already-saved dates.
29
+ *
30
+ * Use $js_opts['dateFormat'] for the date shown when a user selects an
31
+ * option. This option renders to '21 Apr 2013', and is fairly friendly to
32
+ * international users.
33
+ *
23
34
* @var string
24
- * PHP date format, only used for rendering already-saved dates. Use js_opts['dateFormat'] for the
25
- * date shown when a user selects an option. This option renders to '21 Apr 2013', and is fairly
26
- * friendly to international users.
27
35
*/
28
36
public $ date_format = 'j M Y ' ;
29
37
30
38
/**
31
- * @var boolean
39
+ * Whether to use the site's timezone setting when generating a timestamp.
40
+ *
32
41
* By default in WordPress, strtotime() assumes GMT. If $store_local_time is true, FM will use the
33
42
* site's timezone setting when generating the timestamp. Note that `date()` will return GMT times
34
43
* for the stamp no matter what, so if you store the local time, `date( 'H:i', $time )` will return
35
44
* the offset time. Use this option if the exact timestamp is important, e.g. to schedule a wp-cron
36
45
* event.
46
+ *
47
+ * @var bool
37
48
*/
38
49
public $ store_local_time = false ;
39
50
40
51
/**
41
- * @var array
42
- * Options to pass to the jQueryUI Datepicker. If you change dateFormat, be sure that it returns
43
- * a valid unix timestamp. Also, it's best to change js_opts['dateFormat'] and date_format together
44
- * for a consistent user experience.
52
+ * Options to pass to the jQuery UI Datepicker.
53
+ *
54
+ * If you change dateFormat, be sure that it returns a valid Unix timestamp.
55
+ * Also, it's best to change $js_opts['dateFormat'] and $date_format
56
+ * together for a consistent user experience.
57
+ *
58
+ * @see http://api.jqueryui.com/datepicker/.
59
+ * @see Fieldmanager_Datepicker::__construct() For the default options.
45
60
*
46
- * Default:
47
- * <code>
48
- * array(
49
- * 'showButtonPanel' => True,
50
- * 'showOtherMonths' => True,
51
- * 'selectOtherMonths' => True,
52
- * 'dateFormat' => 'd M yy',
53
- * );
54
- * </code>
55
- * @see http://api.jqueryui.com/datepicker/
61
+ * @var array
56
62
*/
57
63
public $ js_opts = array ();
58
64
59
65
/**
60
- * Construct default attributes and enqueue javascript
61
- * @param array $options
66
+ * Construct default attributes and enqueue JavaScript.
67
+ *
68
+ * @param string $label Field label.
69
+ * @param array $options Associative array of class property values. @see Fieldmanager_Field::__construct().
62
70
*/
63
71
public function __construct ( $ label = '' , $ options = array () ) {
64
72
fm_add_style ( 'fm-jquery-ui ' , 'css/jquery-ui/jquery-ui-1.10.2.custom.min.css ' );
@@ -67,9 +75,9 @@ public function __construct( $label = '', $options = array() ) {
67
75
68
76
if ( empty ( $ this ->js_opts ) ) {
69
77
$ this ->js_opts = array (
70
- 'showButtonPanel ' => True ,
71
- 'showOtherMonths ' => True ,
72
- 'selectOtherMonths ' => True ,
78
+ 'showButtonPanel ' => true ,
79
+ 'showOtherMonths ' => true ,
80
+ 'selectOtherMonths ' => true ,
73
81
'dateFormat ' => 'd M yy ' ,
74
82
);
75
83
}
@@ -78,7 +86,7 @@ public function __construct( $label = '', $options = array() ) {
78
86
/**
79
87
* Generate HTML for the form element itself. Generally should be just one tag, no wrappers.
80
88
*
81
- * @param mixed string[]|string the value of the element.
89
+ * @param mixed $value The value of the element.
82
90
* @return string HTML for the element.
83
91
*/
84
92
public function form_element ( $ value ) {
@@ -93,23 +101,26 @@ public function form_element( $value ) {
93
101
ob_start ();
94
102
include fieldmanager_get_template ( 'datepicker ' );
95
103
96
- // Reset the timestamp
104
+ // Reset the timestamp.
97
105
$ value = $ old_value ;
98
106
return ob_get_clean ();
99
107
}
100
108
101
109
/**
102
- * Convert date to timestamp
103
- * @param $value
104
- * @param $current_value
105
- * @return int unix timestamp
110
+ * Convert date to timestamp.
111
+ *
112
+ * @param mixed $value The new value for the field.
113
+ * @param mixed $current_value The current value for the field.
114
+ * @return int Unix timestamp.
106
115
*/
107
116
public function presave ( $ value , $ current_value = array () ) {
108
117
$ time_to_parse = sanitize_text_field ( $ value ['date ' ] );
109
118
if ( isset ( $ value ['hour ' ] ) && is_numeric ( $ value ['hour ' ] ) && $ this ->use_time ) {
110
119
$ hour = intval ( $ value ['hour ' ] );
111
120
$ minute = ( isset ( $ value ['minute ' ] ) && is_numeric ( $ value ['minute ' ] ) ) ? intval ( $ value ['minute ' ] ) : 0 ;
112
- if ( $ hour == 0 && $ this ->use_am_pm ) $ hour = 12 ;
121
+ if ( 0 === $ hour && $ this ->use_am_pm ) {
122
+ $ hour = 12 ;
123
+ }
113
124
$ time_to_parse .= ' ' . $ hour ;
114
125
$ time_to_parse .= ': ' . str_pad ( $ minute , 2 , '0 ' , STR_PAD_LEFT );
115
126
$ time_to_parse .= ' ' . sanitize_text_field ( $ value ['ampm ' ] );
@@ -122,27 +133,30 @@ public function presave( $value, $current_value = array() ) {
122
133
}
123
134
124
135
/**
125
- * Get hour for rendering in field
126
- * @param int $value unix timestamp
127
- * @return string value of hour
136
+ * Get hour for rendering in field.
137
+ *
138
+ * @param int $value Unix timestamp.
139
+ * @return string Value of hour.
128
140
*/
129
141
public function get_hour ( $ value ) {
130
- return !empty ( $ value ) ? date ( $ this ->use_am_pm ? 'g ' : 'G ' , $ value ) : '' ;
142
+ return ! empty ( $ value ) ? date ( $ this ->use_am_pm ? 'g ' : 'G ' , $ value ) : '' ;
131
143
}
132
144
133
145
/**
134
- * Get minute for rendering in field
135
- * @param int $value unix timestamp
136
- * @return string value of hour
146
+ * Get minute for rendering in field.
147
+ *
148
+ * @param int $value Unix timestamp.
149
+ * @return string Value of minute.
137
150
*/
138
151
public function get_minute ( $ value ) {
139
- return !empty ( $ value ) ? date ( 'i ' , $ value ) : '' ;
152
+ return ! empty ( $ value ) ? date ( 'i ' , $ value ) : '' ;
140
153
}
141
154
142
155
/**
143
- * Get am or pm for rendering in field
144
- * @param int $value unix timestamp
145
- * @return string 'am', 'pm', or ''
156
+ * Get 'am' or 'pm' for rendering in field.
157
+ *
158
+ * @param int $value Unix timestamp.
159
+ * @return string 'am', 'pm', or ''.
146
160
*/
147
161
public function get_am_pm ( $ value ) {
148
162
return ! empty ( $ value ) ? date ( 'a ' , $ value ) : '' ;
0 commit comments