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