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