PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.1-beta5
Secure Custom Fields v6.4.1-beta5
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-number.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-clone.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-flexible-content.php 1 year ago class-acf-field-gallery.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-repeater.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-number.php
324 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_number' ) ) :
4
5 class acf_field_number extends acf_field {
6
7
8
9 /**
10 * This function will setup the field type data
11 *
12 * @type function
13 * @date 5/03/2014
14 * @since ACF 5.0.0
15 *
16 * @param n/a
17 * @return n/a
18 */
19 function initialize() {
20
21 // vars
22 $this->name = 'number';
23 $this->label = __( 'Number', 'secure-custom-fields' );
24 $this->description = __( 'An input limited to numerical values.', 'secure-custom-fields' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-number.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/number/';
27 $this->defaults = array(
28 'default_value' => '',
29 'min' => '',
30 'max' => '',
31 'step' => '',
32 'placeholder' => '',
33 'prepend' => '',
34 'append' => '',
35 );
36 }
37
38
39 /**
40 * Create the HTML interface for your field
41 *
42 * @param $field - an array holding all the field's data
43 *
44 * @type action
45 * @since ACF 3.6
46 * @date 23/01/13
47 */
48 function render_field( $field ) {
49
50 // vars
51 $atts = array();
52 $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
53 $keys2 = array( 'readonly', 'disabled', 'required' );
54 $html = '';
55
56 // step
57 if ( ! $field['step'] ) {
58 $field['step'] = 'any';
59 }
60
61 // prepend
62 if ( $field['prepend'] !== '' ) {
63 $field['class'] .= ' acf-is-prepended';
64 $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
65 }
66
67 // append
68 if ( $field['append'] !== '' ) {
69 $field['class'] .= ' acf-is-appended';
70 $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
71 }
72
73 // atts (value="123")
74 foreach ( $keys as $k ) {
75 if ( isset( $field[ $k ] ) ) {
76 $atts[ $k ] = $field[ $k ];
77 }
78 }
79
80 // atts2 (disabled="disabled")
81 foreach ( $keys2 as $k ) {
82 if ( ! empty( $field[ $k ] ) ) {
83 $atts[ $k ] = $k;
84 }
85 }
86
87 // remove empty atts
88 $atts = acf_clean_atts( $atts );
89
90 // render
91 $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
92
93 // return
94 echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped by individual html functions above.
95 }
96
97
98 /**
99 * Create extra options for your field. This is rendered when editing a field.
100 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
101 *
102 * @type action
103 * @since ACF 3.6
104 * @date 23/01/13
105 *
106 * @param $field - an array holding all the field's data
107 */
108 function render_field_settings( $field ) {
109
110 // default_value
111 acf_render_field_setting(
112 $field,
113 array(
114 'label' => __( 'Default Value', 'secure-custom-fields' ),
115 'instructions' => __( 'Appears when creating a new post', 'secure-custom-fields' ),
116 'type' => 'text',
117 'name' => 'default_value',
118 )
119 );
120 }
121
122 /**
123 * Renders the field settings used in the "Validation" tab.
124 *
125 * @since ACF 6.0
126 *
127 * @param array $field The field settings array.
128 * @return void
129 */
130 function render_field_validation_settings( $field ) {
131 acf_render_field_setting(
132 $field,
133 array(
134 'label' => __( 'Minimum Value', 'secure-custom-fields' ),
135 'instructions' => '',
136 'type' => 'number',
137 'name' => 'min',
138 )
139 );
140
141 acf_render_field_setting(
142 $field,
143 array(
144 'label' => __( 'Maximum Value', 'secure-custom-fields' ),
145 'instructions' => '',
146 'type' => 'number',
147 'name' => 'max',
148 )
149 );
150 }
151
152 /**
153 * Renders the field settings used in the "Presentation" tab.
154 *
155 * @since ACF 6.0
156 *
157 * @param array $field The field settings array.
158 * @return void
159 */
160 function render_field_presentation_settings( $field ) {
161 acf_render_field_setting(
162 $field,
163 array(
164 'label' => __( 'Placeholder Text', 'secure-custom-fields' ),
165 'instructions' => __( 'Appears within the input', 'secure-custom-fields' ),
166 'type' => 'text',
167 'name' => 'placeholder',
168 )
169 );
170
171 acf_render_field_setting(
172 $field,
173 array(
174 'label' => __( 'Step Size', 'secure-custom-fields' ),
175 'instructions' => '',
176 'type' => 'number',
177 'name' => 'step',
178 )
179 );
180
181 acf_render_field_setting(
182 $field,
183 array(
184 'label' => __( 'Prepend', 'secure-custom-fields' ),
185 'instructions' => __( 'Appears before the input', 'secure-custom-fields' ),
186 'type' => 'text',
187 'name' => 'prepend',
188 )
189 );
190
191 acf_render_field_setting(
192 $field,
193 array(
194 'label' => __( 'Append', 'secure-custom-fields' ),
195 'instructions' => __( 'Appears after the input', 'secure-custom-fields' ),
196 'type' => 'text',
197 'name' => 'append',
198 )
199 );
200 }
201
202 /**
203 * description
204 *
205 * @type function
206 * @date 11/02/2014
207 * @since ACF 5.0.0
208 *
209 * @param $post_id (int)
210 * @return $post_id (int)
211 */
212 function validate_value( $valid, $value, $field, $input ) {
213
214 // remove ','
215 if ( acf_str_exists( ',', $value ) ) {
216 $value = str_replace( ',', '', $value );
217 }
218
219 // if value is not numeric...
220 if ( ! is_numeric( $value ) ) {
221
222 // allow blank to be saved
223 if ( ! empty( $value ) ) {
224 $valid = __( 'Value must be a number', 'secure-custom-fields' );
225 }
226
227 // return early
228 return $valid;
229 }
230
231 // convert
232 $value = floatval( $value );
233
234 // min
235 if ( is_numeric( $field['min'] ) && $value < floatval( $field['min'] ) ) {
236 /* translators: %d: the minimum value */
237 $valid = sprintf( __( 'Value must be equal to or higher than %d', 'secure-custom-fields' ), $field['min'] );
238 }
239
240 // max
241 if ( is_numeric( $field['max'] ) && $value > floatval( $field['max'] ) ) {
242 /* translators: %d: the maximum value */
243 $valid = sprintf( __( 'Value must be equal to or lower than %d', 'secure-custom-fields' ), $field['max'] );
244 }
245
246 // return
247 return $valid;
248 }
249
250
251 /**
252 * This filter is appied to the $value before it is updated in the db
253 *
254 * @type filter
255 * @since ACF 3.6
256 * @date 23/01/13
257 *
258 * @param $value - the value which will be saved in the database
259 * @param $field - the field array holding all the field options
260 * @param $post_id - the post_id of which the value will be saved
261 *
262 * @return $value - the modified value
263 */
264 function update_value( $value, $post_id, $field ) {
265
266 // no formatting needed for empty value
267 if ( empty( $value ) ) {
268 return $value;
269 }
270
271 // remove ','
272 if ( acf_str_exists( ',', $value ) ) {
273 $value = str_replace( ',', '', $value );
274 }
275
276 // return
277 return $value;
278 }
279
280 /**
281 * Return the schema array for the REST API.
282 *
283 * @param array $field
284 * @return array
285 */
286 public function get_rest_schema( array $field ) {
287 $schema = array(
288 'type' => array( 'number', 'null' ),
289 'required' => ! empty( $field['required'] ),
290 );
291
292 if ( ! empty( $field['min'] ) ) {
293 $schema['minimum'] = (float) $field['min'];
294 }
295
296 if ( ! empty( $field['max'] ) ) {
297 $schema['maximum'] = (float) $field['max'];
298 }
299
300 if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
301 $schema['default'] = (float) $field['default_value'];
302 }
303
304 return $schema;
305 }
306
307 /**
308 * Apply basic formatting to prepare the value for default REST output.
309 *
310 * @param mixed $value
311 * @param string|integer $post_id
312 * @param array $field
313 * @return mixed
314 */
315 public function format_value_for_rest( $value, $post_id, array $field ) {
316 return acf_format_numerics( $value );
317 }
318 }
319
320
321 // initialize
322 acf_register_field_type( 'acf_field_number' );
323 endif; // class_exists check
324