PluginProbe
Advanced Custom Fields (ACF®) / 5.9.2
Advanced Custom Fields (ACF®) v5.9.2
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / fields / class-acf-field-number.php
class-acf-field-number.php
304 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! class_exists('acf_field_number') ) :
4
5 class acf_field_number extends acf_field {
6
7
8 /*
9 * initialize
10 *
11 * This function will setup the field type data
12 *
13 * @type function
14 * @date 5/03/2014
15 * @since 5.0.0
16 *
17 * @param n/a
18 * @return n/a
19 */
20
21 function initialize() {
22
23 // vars
24 $this->name = 'number';
25 $this->label = __("Number",'acf');
26 $this->defaults = array(
27 'default_value' => '',
28 'min' => '',
29 'max' => '',
30 'step' => '',
31 'placeholder' => '',
32 'prepend' => '',
33 'append' => ''
34 );
35
36 }
37
38
39 /*
40 * render_field()
41 *
42 * Create the HTML interface for your field
43 *
44 * @param $field - an array holding all the field's data
45 *
46 * @type action
47 * @since 3.6
48 * @date 23/01/13
49 */
50
51 function render_field( $field ) {
52
53 // vars
54 $atts = array();
55 $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
56 $keys2 = array( 'readonly', 'disabled', 'required' );
57 $html = '';
58
59
60 // step
61 if( !$field['step'] ) {
62 $field['step'] = 'any';
63 }
64
65
66 // prepend
67 if( $field['prepend'] !== '' ) {
68
69 $field['class'] .= ' acf-is-prepended';
70 $html .= '<div class="acf-input-prepend">' . acf_esc_html($field['prepend']) . '</div>';
71
72 }
73
74
75 // append
76 if( $field['append'] !== '' ) {
77
78 $field['class'] .= ' acf-is-appended';
79 $html .= '<div class="acf-input-append">' . acf_esc_html($field['append']) . '</div>';
80
81 }
82
83
84 // atts (value="123")
85 foreach( $keys as $k ) {
86 if( isset($field[ $k ]) ) $atts[ $k ] = $field[ $k ];
87 }
88
89
90 // atts2 (disabled="disabled")
91 foreach( $keys2 as $k ) {
92 if( !empty($field[ $k ]) ) $atts[ $k ] = $k;
93 }
94
95
96 // remove empty atts
97 $atts = acf_clean_atts( $atts );
98
99
100 // render
101 $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
102
103
104 // return
105 echo $html;
106
107 }
108
109
110 /*
111 * render_field_settings()
112 *
113 * Create extra options for your field. This is rendered when editing a field.
114 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
115 *
116 * @type action
117 * @since 3.6
118 * @date 23/01/13
119 *
120 * @param $field - an array holding all the field's data
121 */
122
123 function render_field_settings( $field ) {
124
125 // default_value
126 acf_render_field_setting( $field, array(
127 'label' => __('Default Value','acf'),
128 'instructions' => __('Appears when creating a new post','acf'),
129 'type' => 'text',
130 'name' => 'default_value',
131 ));
132
133
134 // placeholder
135 acf_render_field_setting( $field, array(
136 'label' => __('Placeholder Text','acf'),
137 'instructions' => __('Appears within the input','acf'),
138 'type' => 'text',
139 'name' => 'placeholder',
140 ));
141
142
143 // prepend
144 acf_render_field_setting( $field, array(
145 'label' => __('Prepend','acf'),
146 'instructions' => __('Appears before the input','acf'),
147 'type' => 'text',
148 'name' => 'prepend',
149 ));
150
151
152 // append
153 acf_render_field_setting( $field, array(
154 'label' => __('Append','acf'),
155 'instructions' => __('Appears after the input','acf'),
156 'type' => 'text',
157 'name' => 'append',
158 ));
159
160
161 // min
162 acf_render_field_setting( $field, array(
163 'label' => __('Minimum Value','acf'),
164 'instructions' => '',
165 'type' => 'number',
166 'name' => 'min',
167 ));
168
169
170 // max
171 acf_render_field_setting( $field, array(
172 'label' => __('Maximum Value','acf'),
173 'instructions' => '',
174 'type' => 'number',
175 'name' => 'max',
176 ));
177
178
179 // max
180 acf_render_field_setting( $field, array(
181 'label' => __('Step Size','acf'),
182 'instructions' => '',
183 'type' => 'number',
184 'name' => 'step',
185 ));
186
187 }
188
189
190 /*
191 * validate_value
192 *
193 * description
194 *
195 * @type function
196 * @date 11/02/2014
197 * @since 5.0.0
198 *
199 * @param $post_id (int)
200 * @return $post_id (int)
201 */
202
203 function validate_value( $valid, $value, $field, $input ){
204
205 // remove ','
206 if( acf_str_exists(',', $value) ) {
207
208 $value = str_replace(',', '', $value);
209
210 }
211
212
213 // if value is not numeric...
214 if( !is_numeric($value) ) {
215
216 // allow blank to be saved
217 if( !empty($value) ) {
218
219 $valid = __('Value must be a number', 'acf');
220
221 }
222
223
224 // return early
225 return $valid;
226
227 }
228
229
230 // convert
231 $value = floatval($value);
232
233
234 // min
235 if( is_numeric($field['min']) && $value < floatval($field['min'])) {
236
237 $valid = sprintf(__('Value must be equal to or higher than %d', 'acf'), $field['min'] );
238
239 }
240
241
242 // max
243 if( is_numeric($field['max']) && $value > floatval($field['max']) ) {
244
245 $valid = sprintf(__('Value must be equal to or lower than %d', 'acf'), $field['max'] );
246
247 }
248
249
250 // return
251 return $valid;
252
253 }
254
255
256 /*
257 * update_value()
258 *
259 * This filter is appied to the $value before it is updated in the db
260 *
261 * @type filter
262 * @since 3.6
263 * @date 23/01/13
264 *
265 * @param $value - the value which will be saved in the database
266 * @param $field - the field array holding all the field options
267 * @param $post_id - the $post_id of which the value will be saved
268 *
269 * @return $value - the modified value
270 */
271
272 function update_value( $value, $post_id, $field ) {
273
274 // no formatting needed for empty value
275 if( empty($value) ) {
276
277 return $value;
278
279 }
280
281
282 // remove ','
283 if( acf_str_exists(',', $value) ) {
284
285 $value = str_replace(',', '', $value);
286
287 }
288
289
290 // return
291 return $value;
292
293 }
294
295
296 }
297
298
299 // initialize
300 acf_register_field_type( 'acf_field_number' );
301
302 endif; // class_exists check
303
304 ?>