|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* ACF Number Field Class |
|
5
|
|
|
* |
|
6
|
|
|
* All the logic for this field type |
|
7
|
|
|
* |
|
8
|
|
|
* @class acf_field_number |
|
9
|
|
|
* @extends acf_field |
|
10
|
|
|
* @package ACF |
|
11
|
|
|
* @subpackage Fields |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
if( ! class_exists('acf_field_number') ) : |
|
15
|
|
|
|
|
16
|
|
|
class acf_field_number extends acf_field { |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/* |
|
20
|
|
|
* __construct |
|
21
|
|
|
* |
|
22
|
|
|
* This function will setup the field type data |
|
23
|
|
|
* |
|
24
|
|
|
* @type function |
|
25
|
|
|
* @date 5/03/2014 |
|
26
|
|
|
* @since 5.0.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @param n/a |
|
29
|
|
|
* @return n/a |
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
function __construct() { |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
// vars |
|
35
|
|
|
$this->name = 'number'; |
|
36
|
|
|
$this->label = __("Number",'acf'); |
|
|
|
|
|
|
37
|
|
|
$this->defaults = array( |
|
38
|
|
|
'default_value' => '', |
|
39
|
|
|
'min' => '', |
|
40
|
|
|
'max' => '', |
|
41
|
|
|
'step' => '', |
|
42
|
|
|
'placeholder' => '', |
|
43
|
|
|
'prepend' => '', |
|
44
|
|
|
'append' => '', |
|
45
|
|
|
'readonly' => 0, |
|
46
|
|
|
'disabled' => 0, |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// do not delete! |
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/* |
|
56
|
|
|
* render_field() |
|
57
|
|
|
* |
|
58
|
|
|
* Create the HTML interface for your field |
|
59
|
|
|
* |
|
60
|
|
|
* @param $field - an array holding all the field's data |
|
61
|
|
|
* |
|
62
|
|
|
* @type action |
|
63
|
|
|
* @since 3.6 |
|
64
|
|
|
* @date 23/01/13 |
|
65
|
|
|
*/ |
|
66
|
|
|
|
|
67
|
|
|
function render_field( $field ) { |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// vars |
|
70
|
|
|
$o = array( 'type', 'id', 'class', 'min', 'max', 'step', 'name', 'value', 'placeholder' ); |
|
71
|
|
|
$e = ''; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
// step |
|
75
|
|
|
if( !$field['step'] ) { |
|
76
|
|
|
|
|
77
|
|
|
$field['step'] = 'any'; |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// prepend |
|
83
|
|
View Code Duplication |
if( $field['prepend'] !== "" ) { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$field['class'] .= ' acf-is-prepended'; |
|
86
|
|
|
$e .= '<div class="acf-input-prepend">' . $field['prepend'] . '</div>'; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// append |
|
92
|
|
View Code Duplication |
if( $field['append'] !== "" ) { |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$field['class'] .= ' acf-is-appended'; |
|
95
|
|
|
$e .= '<div class="acf-input-append">' . $field['append'] . '</div>'; |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
// populate atts |
|
101
|
|
|
$atts = array(); |
|
102
|
|
|
foreach( $o as $k ) { |
|
103
|
|
|
|
|
104
|
|
|
$atts[ $k ] = $field[ $k ]; |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
// special atts |
|
110
|
|
View Code Duplication |
foreach( array( 'readonly', 'disabled' ) as $k ) { |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
if( $field[ $k ] ) { |
|
113
|
|
|
|
|
114
|
|
|
$atts[ $k ] = $k; |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
// render |
|
122
|
|
|
$e .= '<div class="acf-input-wrap">'; |
|
123
|
|
|
$e .= '<input ' . acf_esc_attr( $atts ) . ' />'; |
|
124
|
|
|
$e .= '</div>'; |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// return |
|
128
|
|
|
echo $e; |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/* |
|
134
|
|
|
* render_field_settings() |
|
135
|
|
|
* |
|
136
|
|
|
* Create extra options for your field. This is rendered when editing a field. |
|
137
|
|
|
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
|
138
|
|
|
* |
|
139
|
|
|
* @type action |
|
140
|
|
|
* @since 3.6 |
|
141
|
|
|
* @date 23/01/13 |
|
142
|
|
|
* |
|
143
|
|
|
* @param $field - an array holding all the field's data |
|
144
|
|
|
*/ |
|
145
|
|
|
|
|
146
|
|
|
function render_field_settings( $field ) { |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
// default_value |
|
149
|
|
|
acf_render_field_setting( $field, array( |
|
150
|
|
|
'label' => __('Default Value','acf'), |
|
151
|
|
|
'instructions' => __('Appears when creating a new post','acf'), |
|
152
|
|
|
'type' => 'text', |
|
153
|
|
|
'name' => 'default_value', |
|
154
|
|
|
)); |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
// placeholder |
|
158
|
|
|
acf_render_field_setting( $field, array( |
|
159
|
|
|
'label' => __('Placeholder Text','acf'), |
|
160
|
|
|
'instructions' => __('Appears within the input','acf'), |
|
161
|
|
|
'type' => 'text', |
|
162
|
|
|
'name' => 'placeholder', |
|
163
|
|
|
)); |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
// prepend |
|
167
|
|
|
acf_render_field_setting( $field, array( |
|
168
|
|
|
'label' => __('Prepend','acf'), |
|
169
|
|
|
'instructions' => __('Appears before the input','acf'), |
|
170
|
|
|
'type' => 'text', |
|
171
|
|
|
'name' => 'prepend', |
|
172
|
|
|
)); |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
// append |
|
176
|
|
|
acf_render_field_setting( $field, array( |
|
177
|
|
|
'label' => __('Append','acf'), |
|
178
|
|
|
'instructions' => __('Appears after the input','acf'), |
|
179
|
|
|
'type' => 'text', |
|
180
|
|
|
'name' => 'append', |
|
181
|
|
|
)); |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
// min |
|
185
|
|
|
acf_render_field_setting( $field, array( |
|
186
|
|
|
'label' => __('Minimum Value','acf'), |
|
187
|
|
|
'instructions' => '', |
|
188
|
|
|
'type' => 'number', |
|
189
|
|
|
'name' => 'min', |
|
190
|
|
|
)); |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
// max |
|
194
|
|
|
acf_render_field_setting( $field, array( |
|
195
|
|
|
'label' => __('Maximum Value','acf'), |
|
196
|
|
|
'instructions' => '', |
|
197
|
|
|
'type' => 'number', |
|
198
|
|
|
'name' => 'max', |
|
199
|
|
|
)); |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
// max |
|
203
|
|
|
acf_render_field_setting( $field, array( |
|
204
|
|
|
'label' => __('Step Size','acf'), |
|
205
|
|
|
'instructions' => '', |
|
206
|
|
|
'type' => 'number', |
|
207
|
|
|
'name' => 'step', |
|
208
|
|
|
)); |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
/* |
|
214
|
|
|
* validate_value |
|
215
|
|
|
* |
|
216
|
|
|
* description |
|
217
|
|
|
* |
|
218
|
|
|
* @type function |
|
219
|
|
|
* @date 11/02/2014 |
|
220
|
|
|
* @since 5.0.0 |
|
221
|
|
|
* |
|
222
|
|
|
* @param $post_id (int) |
|
223
|
|
|
* @return $post_id (int) |
|
224
|
|
|
*/ |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
function validate_value( $valid, $value, $field, $input ){ |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
// remove ',' |
|
229
|
|
|
if( acf_str_exists(',', $value) ) { |
|
230
|
|
|
|
|
231
|
|
|
$value = str_replace(',', '', $value); |
|
232
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
// if value is not numeric... |
|
237
|
|
|
if( !is_numeric($value) ) { |
|
238
|
|
|
|
|
239
|
|
|
// allow blank to be saved |
|
240
|
|
|
if( !empty($value) ) { |
|
241
|
|
|
|
|
242
|
|
|
$valid = __('Value must be a number', 'acf'); |
|
243
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
// return early |
|
248
|
|
|
return $valid; |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
// convert |
|
254
|
|
|
$value = floatval($value); |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
// min |
|
258
|
|
View Code Duplication |
if( is_numeric($field['min']) && $value < floatval($field['min'])) { |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
$valid = sprintf(__('Value must be equal to or higher than %d', 'acf'), $field['min'] ); |
|
261
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
// max |
|
266
|
|
View Code Duplication |
if( is_numeric($field['max']) && $value > floatval($field['max']) ) { |
|
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
$valid = sprintf(__('Value must be equal to or lower than %d', 'acf'), $field['max'] ); |
|
269
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
// return |
|
274
|
|
|
return $valid; |
|
275
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
/* |
|
280
|
|
|
* update_value() |
|
281
|
|
|
* |
|
282
|
|
|
* This filter is appied to the $value before it is updated in the db |
|
283
|
|
|
* |
|
284
|
|
|
* @type filter |
|
285
|
|
|
* @since 3.6 |
|
286
|
|
|
* @date 23/01/13 |
|
287
|
|
|
* |
|
288
|
|
|
* @param $value - the value which will be saved in the database |
|
289
|
|
|
* @param $field - the field array holding all the field options |
|
290
|
|
|
* @param $post_id - the $post_id of which the value will be saved |
|
291
|
|
|
* |
|
292
|
|
|
* @return $value - the modified value |
|
293
|
|
|
*/ |
|
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
function update_value( $value, $post_id, $field ) { |
|
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
// no formatting needed for empty value |
|
298
|
|
|
if( empty($value) ) { |
|
299
|
|
|
|
|
300
|
|
|
return $value; |
|
301
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
// remove ',' |
|
306
|
|
|
if( acf_str_exists(',', $value) ) { |
|
307
|
|
|
|
|
308
|
|
|
$value = str_replace(',', '', $value); |
|
309
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
// return |
|
314
|
|
|
return $value; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
new acf_field_number(); |
|
321
|
|
|
|
|
322
|
|
|
endif; |
|
323
|
|
|
|
|
324
|
|
|
?> |
|
|
|
|
|
|
325
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.