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