class-acf-field-accordion.php
5 months ago
class-acf-field-button-group.php
5 months ago
class-acf-field-checkbox.php
2 months ago
class-acf-field-color_picker.php
5 months ago
class-acf-field-date_picker.php
5 months ago
class-acf-field-date_time_picker.php
5 months ago
class-acf-field-email.php
5 months ago
class-acf-field-file.php
5 months ago
class-acf-field-google-map.php
5 months ago
class-acf-field-group.php
5 months ago
class-acf-field-icon_picker.php
6 months ago
class-acf-field-image.php
4 weeks ago
class-acf-field-link.php
5 months ago
class-acf-field-message.php
6 months ago
class-acf-field-number.php
5 months ago
class-acf-field-oembed.php
2 months ago
class-acf-field-output.php
6 months ago
class-acf-field-page_link.php
4 weeks ago
class-acf-field-password.php
5 months ago
class-acf-field-post_object.php
4 weeks ago
class-acf-field-radio.php
2 months ago
class-acf-field-range.php
5 months ago
class-acf-field-relationship.php
4 weeks ago
class-acf-field-select.php
2 months ago
class-acf-field-separator.php
6 months ago
class-acf-field-tab.php
6 months ago
class-acf-field-taxonomy.php
2 months ago
class-acf-field-text.php
5 months ago
class-acf-field-textarea.php
5 months ago
class-acf-field-time_picker.php
5 months ago
class-acf-field-true_false.php
5 months ago
class-acf-field-url.php
5 months ago
class-acf-field-user.php
2 months ago
class-acf-field-wysiwyg.php
5 months ago
class-acf-field.php
5 months ago
index.php
2 years ago
class-acf-field-range.php
293 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'acf_field_range' ) ) : |
| 13 | |
| 14 | class acf_field_range extends acf_field_number { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * This function will setup the field type data |
| 19 | * |
| 20 | * @type function |
| 21 | * @date 5/03/2014 |
| 22 | * @since 5.0.0 |
| 23 | * |
| 24 | * @param n/a |
| 25 | * @return n/a |
| 26 | */ |
| 27 | function initialize() { |
| 28 | |
| 29 | // vars |
| 30 | $this->name = 'range'; |
| 31 | $this->label = __( 'Range', 'acf' ); |
| 32 | $this->description = __( 'An input for selecting a numerical value within a specified range using a range slider element.', 'acf' ); |
| 33 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-range.png'; |
| 34 | $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/range/', 'docs', 'field-type-selection' ); |
| 35 | $this->defaults = array( |
| 36 | 'default_value' => '', |
| 37 | 'min' => '', |
| 38 | 'max' => '', |
| 39 | 'step' => '', |
| 40 | 'prepend' => '', |
| 41 | 'append' => '', |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Create the HTML interface for your field |
| 48 | * |
| 49 | * @param $field - an array holding all the field's data |
| 50 | * |
| 51 | * @type action |
| 52 | * @since 3.6 |
| 53 | * @date 23/01/13 |
| 54 | */ |
| 55 | function render_field( $field ) { |
| 56 | |
| 57 | // vars |
| 58 | $atts = array(); |
| 59 | $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step' ); |
| 60 | $keys2 = array( 'readonly', 'disabled', 'required' ); |
| 61 | |
| 62 | // step |
| 63 | if ( ! $field['step'] ) { |
| 64 | $field['step'] = 1; |
| 65 | } |
| 66 | |
| 67 | // min / max |
| 68 | if ( ! $field['min'] ) { |
| 69 | $field['min'] = 0; |
| 70 | } |
| 71 | if ( ! $field['max'] ) { |
| 72 | $field['max'] = 100; |
| 73 | } |
| 74 | |
| 75 | // allow for prev 'non numeric' value |
| 76 | if ( ! is_numeric( $field['value'] ) ) { |
| 77 | $field['value'] = 0; |
| 78 | } |
| 79 | |
| 80 | // constrain within max and min |
| 81 | $field['value'] = max( $field['value'], $field['min'] ); |
| 82 | $field['value'] = min( $field['value'], $field['max'] ); |
| 83 | |
| 84 | // atts (value="123") |
| 85 | foreach ( $keys as $k ) { |
| 86 | if ( isset( $field[ $k ] ) ) { |
| 87 | $atts[ $k ] = $field[ $k ]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // atts2 (disabled="disabled") |
| 92 | foreach ( $keys2 as $k ) { |
| 93 | if ( ! empty( $field[ $k ] ) ) { |
| 94 | $atts[ $k ] = $k; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // remove empty atts |
| 99 | $atts = acf_clean_atts( $atts ); |
| 100 | |
| 101 | // open |
| 102 | $html = '<div class="acf-range-wrap">'; |
| 103 | |
| 104 | // prepend |
| 105 | if ( $field['prepend'] !== '' ) { |
| 106 | $html .= '<div class="acf-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>'; |
| 107 | } |
| 108 | |
| 109 | // range |
| 110 | $html .= acf_get_text_input( $atts ); |
| 111 | |
| 112 | // Calculate input width based on the largest possible input character length. |
| 113 | // Also take into account the step size for decimal steps minus - 1.5 chars for leading "0.". |
| 114 | $len = max( |
| 115 | strlen( strval( $field['min'] ) ), |
| 116 | strlen( strval( $field['max'] ) ) |
| 117 | ); |
| 118 | if ( floatval( $atts['step'] ) < 1 ) { |
| 119 | $len += strlen( strval( $field['step'] ) ) - 1.5; |
| 120 | } |
| 121 | |
| 122 | // input |
| 123 | $html .= acf_get_text_input( |
| 124 | array( |
| 125 | 'type' => 'number', |
| 126 | 'id' => $atts['id'] . '-alt', |
| 127 | 'value' => $atts['value'], |
| 128 | 'step' => $atts['step'], |
| 129 | // 'min' => $atts['min'], // removed to avoid browser validation errors |
| 130 | // 'max' => $atts['max'], |
| 131 | 'style' => 'width: ' . ( 1.8 + $len * 0.7 ) . 'em;', |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | // append |
| 136 | if ( $field['append'] !== '' ) { |
| 137 | $html .= '<div class="acf-append">' . acf_esc_html( $field['append'] ) . '</div>'; |
| 138 | } |
| 139 | |
| 140 | // close |
| 141 | $html .= '</div>'; |
| 142 | |
| 143 | // return |
| 144 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput -- Only populated with already escaped HTML. |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Create extra options for your field. This is rendered when editing a field. |
| 150 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 151 | * |
| 152 | * @type action |
| 153 | * @since 3.6 |
| 154 | * @date 23/01/13 |
| 155 | * |
| 156 | * @param $field - an array holding all the field's data |
| 157 | */ |
| 158 | function render_field_settings( $field ) { |
| 159 | acf_render_field_setting( |
| 160 | $field, |
| 161 | array( |
| 162 | 'label' => __( 'Default Value', 'acf' ), |
| 163 | 'instructions' => __( 'Appears when creating a new post', 'acf' ), |
| 164 | 'type' => 'number', |
| 165 | 'name' => 'default_value', |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Renders the field settings used in the "Validation" tab. |
| 172 | * |
| 173 | * @since 6.0 |
| 174 | * |
| 175 | * @param array $field The field settings array. |
| 176 | * @return void |
| 177 | */ |
| 178 | function render_field_validation_settings( $field ) { |
| 179 | acf_render_field_setting( |
| 180 | $field, |
| 181 | array( |
| 182 | 'label' => __( 'Minimum Value', 'acf' ), |
| 183 | 'instructions' => '', |
| 184 | 'type' => 'number', |
| 185 | 'name' => 'min', |
| 186 | 'placeholder' => '0', |
| 187 | ) |
| 188 | ); |
| 189 | |
| 190 | acf_render_field_setting( |
| 191 | $field, |
| 192 | array( |
| 193 | 'label' => __( 'Maximum Value', 'acf' ), |
| 194 | 'instructions' => '', |
| 195 | 'type' => 'number', |
| 196 | 'name' => 'max', |
| 197 | 'placeholder' => '100', |
| 198 | ) |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Renders the field settings used in the "Presentation" tab. |
| 204 | * |
| 205 | * @since 6.0 |
| 206 | * |
| 207 | * @param array $field The field settings array. |
| 208 | * @return void |
| 209 | */ |
| 210 | function render_field_presentation_settings( $field ) { |
| 211 | |
| 212 | acf_render_field_setting( |
| 213 | $field, |
| 214 | array( |
| 215 | 'label' => __( 'Step Size', 'acf' ), |
| 216 | 'instructions' => '', |
| 217 | 'type' => 'number', |
| 218 | 'name' => 'step', |
| 219 | 'placeholder' => '1', |
| 220 | ) |
| 221 | ); |
| 222 | |
| 223 | acf_render_field_setting( |
| 224 | $field, |
| 225 | array( |
| 226 | 'label' => __( 'Prepend', 'acf' ), |
| 227 | 'instructions' => __( 'Appears before the input', 'acf' ), |
| 228 | 'type' => 'text', |
| 229 | 'name' => 'prepend', |
| 230 | ) |
| 231 | ); |
| 232 | |
| 233 | acf_render_field_setting( |
| 234 | $field, |
| 235 | array( |
| 236 | 'label' => __( 'Append', 'acf' ), |
| 237 | 'instructions' => __( 'Appears after the input', 'acf' ), |
| 238 | 'type' => 'text', |
| 239 | 'name' => 'append', |
| 240 | ) |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Return the schema array for the REST API. |
| 246 | * |
| 247 | * @param array $field |
| 248 | * @return array |
| 249 | */ |
| 250 | public function get_rest_schema( array $field ) { |
| 251 | $schema = array( |
| 252 | 'type' => array( 'number', 'null' ), |
| 253 | 'required' => ! empty( $field['required'] ), |
| 254 | 'minimum' => empty( $field['min'] ) ? 0 : (int) $field['min'], |
| 255 | 'maximum' => empty( $field['max'] ) ? 100 : (int) $field['max'], |
| 256 | ); |
| 257 | |
| 258 | if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) { |
| 259 | $schema['default'] = (int) $field['default_value']; |
| 260 | } |
| 261 | |
| 262 | return $schema; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Apply basic formatting to prepare the value for default REST output. |
| 267 | * |
| 268 | * @param mixed $value |
| 269 | * @param string|integer $post_id |
| 270 | * @param array $field |
| 271 | * @return mixed |
| 272 | */ |
| 273 | public function format_value_for_rest( $value, $post_id, array $field ) { |
| 274 | return acf_format_numerics( $value ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 279 | * |
| 280 | * @since 6.8 |
| 281 | * |
| 282 | * @return string[] |
| 283 | */ |
| 284 | public function get_jsonld_output_types(): array { |
| 285 | return array( 'Number', 'Integer' ); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
| 290 | // initialize |
| 291 | acf_register_field_type( 'acf_field_range' ); |
| 292 | endif; // class_exists check |
| 293 |