|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
require_once( PODS_DIR . 'classes/fields/datetime.php' ); |
|
3
|
|
|
/** |
|
4
|
|
|
* @package Pods\Fields |
|
5
|
|
|
*/ |
|
6
|
|
|
class PodsField_Date extends PodsField_DateTime { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Field Type Group |
|
10
|
|
|
* |
|
11
|
|
|
* @var string |
|
12
|
|
|
* @since 2.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
public static $group = 'Date / Time'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Field Type Identifier |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
* @since 2.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
public static $type = 'date'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Field Type Label |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
* @since 2.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
public static $label = 'Date'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Field Type Preparation |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
* @since 2.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
public static $prepare = '%s'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Storage format. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
* @since 2.7 |
|
45
|
|
|
*/ |
|
46
|
|
|
public static $storage_format = 'Y-m-d'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The default empty value (database) |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
* @since 2.7 |
|
53
|
|
|
*/ |
|
54
|
|
|
public static $empty_value = '0000-00-00'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Do things like register/enqueue scripts and stylesheets |
|
58
|
|
|
* |
|
59
|
|
|
* @since 2.0 |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct () { |
|
62
|
|
|
static::$label = __( 'Date', 'pods' ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add options and set defaults to |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
* |
|
70
|
|
|
* @since 2.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
public function options () { |
|
73
|
|
|
$options = array( |
|
74
|
|
|
static::$type . '_repeatable' => array( |
|
75
|
|
|
'label' => __( 'Repeatable Field', 'pods' ), |
|
76
|
|
|
'default' => 0, |
|
77
|
|
|
'type' => 'boolean', |
|
78
|
|
|
'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
|
79
|
|
|
'boolean_yes_label' => '', |
|
80
|
|
|
'dependency' => true, |
|
81
|
|
|
'developer_mode' => true |
|
82
|
|
|
), |
|
83
|
|
|
static::$type . '_type' => array( |
|
84
|
|
|
'label' => __( 'Date Format Type', 'pods' ), |
|
85
|
|
|
'default' => 'format', // Backwards compatibility |
|
86
|
|
|
'type' => 'pick', |
|
87
|
|
|
'help' => __( 'WordPress Default is the format used in Settings, General under "Date Format".', 'pods' ) . '<br>' |
|
88
|
|
|
. __( 'Predefined Format will allow you to select from a list of commonly used date formats.', 'pods' ) . '<br>' |
|
89
|
|
|
. __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ), |
|
90
|
|
|
'data' => array( |
|
91
|
|
|
'wp' => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'date_format' ) ), |
|
92
|
|
|
'format' => __( 'Predefined format', 'pods' ), |
|
93
|
|
|
'custom' => __( 'Custom format', 'pods' ), |
|
94
|
|
|
), |
|
95
|
|
|
'dependency' => true |
|
96
|
|
|
), |
|
97
|
|
|
static::$type . '_format_custom' => array( |
|
98
|
|
|
'label' => __( 'Date format for display', 'pods' ), |
|
99
|
|
|
'depends-on' => array( static::$type . '_type' => 'custom' ), |
|
100
|
|
|
'default' => '', |
|
101
|
|
|
'type' => 'text', |
|
102
|
|
|
'help' => '<a href="http://php.net/manual/function.date.php" target="_blank">' . __( 'PHP date documentation', 'pods' ) . '</a>', |
|
103
|
|
|
), |
|
104
|
|
|
static::$type . '_format_custom_js' => array( |
|
105
|
|
|
'label' => __( 'Date format for input', 'pods' ), |
|
106
|
|
|
'depends-on' => array( static::$type . '_type' => 'custom' ), |
|
107
|
|
|
'default' => '', |
|
108
|
|
|
'type' => 'text', |
|
109
|
|
|
'help' => '<a href="https://api.jqueryui.com/datepicker/" target="_blank">' . __( 'jQuery UI datepicker documentation', 'pods' ) . '</a>' |
|
110
|
|
|
. '<br>' . __( 'Leave empty to auto-generate from PHP format.', 'pods' ), |
|
111
|
|
|
), |
|
112
|
|
|
static::$type . '_format' => array( |
|
113
|
|
|
'label' => __( 'Date Format', 'pods' ), |
|
114
|
|
|
'depends-on' => array( static::$type . '_type' => 'format' ), |
|
115
|
|
|
'default' => 'mdy', |
|
116
|
|
|
'type' => 'pick', |
|
117
|
|
|
'data' => array( |
|
118
|
|
|
'mdy' => date_i18n( 'm/d/Y' ), |
|
119
|
|
|
'mdy_dash' => date_i18n( 'm-d-Y' ), |
|
120
|
|
|
'mdy_dot' => date_i18n( 'm.d.Y' ), |
|
121
|
|
|
'ymd_slash' => date_i18n( 'Y/m/d' ), |
|
122
|
|
|
'ymd_dash' => date_i18n( 'Y-m-d' ), |
|
123
|
|
|
'ymd_dot' => date_i18n( 'Y.m.d' ), |
|
124
|
|
|
'fjy' => date_i18n( 'F j, Y' ), |
|
125
|
|
|
'fjsy' => date_i18n( 'F jS, Y' ), |
|
126
|
|
|
'y' => date_i18n( 'Y' ), |
|
127
|
|
|
), |
|
128
|
|
|
'dependency' => true, |
|
129
|
|
|
), |
|
130
|
|
|
static::$type . '_allow_empty' => array( |
|
131
|
|
|
'label' => __( 'Allow empty value?', 'pods' ), |
|
132
|
|
|
'default' => 1, |
|
133
|
|
|
'type' => 'boolean' |
|
134
|
|
|
), |
|
135
|
|
|
static::$type . '_html5' => array( |
|
136
|
|
|
'label' => __( 'Enable HTML5 Input Field?', 'pods' ), |
|
137
|
|
|
'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ), |
|
138
|
|
|
'type' => 'boolean' |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
// Check if PHP DateTime::createFromFormat exists for additional supported formats |
|
143
|
|
View Code Duplication |
if ( method_exists( 'DateTime', 'createFromFormat' ) || apply_filters( 'pods_form_ui_field_datetime_custom_formatter', false ) ) { |
|
144
|
|
|
$options[ static::$type . '_format' ][ 'data' ] = array_merge( |
|
145
|
|
|
$options[ static::$type . '_format' ][ 'data' ], |
|
146
|
|
|
array( |
|
147
|
|
|
'dmy' => date_i18n( 'd/m/Y' ), |
|
148
|
|
|
'dmy_dash' => date_i18n( 'd-m-Y' ), |
|
149
|
|
|
'dmy_dot' => date_i18n( 'd.m.Y' ), |
|
150
|
|
|
'dMy' => date_i18n( 'd/M/Y' ), |
|
151
|
|
|
'dMy_dash' => date_i18n( 'd-M-Y' ) |
|
152
|
|
|
) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$options[ static::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_date_format_options', $options[ static::$type . '_format' ][ 'data' ] ); |
|
157
|
|
|
$options[ static::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_date_format_default', $options[ static::$type . '_format' ][ 'default' ] ); |
|
158
|
|
|
|
|
159
|
|
|
return $options; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Define the current field's schema for DB table storage |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $options |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
* @since 2.0 |
|
169
|
|
|
*/ |
|
170
|
|
|
public function schema ( $options = null ) { |
|
171
|
|
|
$schema = 'DATE NOT NULL default "0000-00-00"'; |
|
172
|
|
|
|
|
173
|
|
|
return $schema; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Build date/time format string based on options |
|
178
|
|
|
* |
|
179
|
|
|
* @param array $options |
|
180
|
|
|
* @param bool $js Return format for jQuery UI? |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
* @since 2.0 |
|
184
|
|
|
*/ |
|
185
|
|
|
public function format ( $options, $js = false ) { |
|
186
|
|
|
// @see datetime field. |
|
187
|
|
|
return $this->format_date( $options, $js ); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.