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