class-acf-field-range.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. if ( ! class_exists( 'acf_field_range' ) ) :
  3. class acf_field_range extends acf_field_number {
  4. /*
  5. * initialize
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'range';
  19. $this->label = __( 'Range', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'min' => '',
  23. 'max' => '',
  24. 'step' => '',
  25. 'prepend' => '',
  26. 'append' => '',
  27. );
  28. }
  29. /*
  30. * render_field()
  31. *
  32. * Create the HTML interface for your field
  33. *
  34. * @param $field - an array holding all the field's data
  35. *
  36. * @type action
  37. * @since 3.6
  38. * @date 23/01/13
  39. */
  40. function render_field( $field ) {
  41. // vars
  42. $atts = array();
  43. $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step' );
  44. $keys2 = array( 'readonly', 'disabled', 'required' );
  45. $html = '';
  46. // step
  47. if ( ! $field['step'] ) {
  48. $field['step'] = 1;
  49. }
  50. // min / max
  51. if ( ! $field['min'] ) {
  52. $field['min'] = 0;
  53. }
  54. if ( ! $field['max'] ) {
  55. $field['max'] = 100;
  56. }
  57. // allow for prev 'non numeric' value
  58. if ( ! is_numeric( $field['value'] ) ) {
  59. $field['value'] = 0;
  60. }
  61. // constrain within max and min
  62. $field['value'] = max( $field['value'], $field['min'] );
  63. $field['value'] = min( $field['value'], $field['max'] );
  64. // atts (value="123")
  65. foreach ( $keys as $k ) {
  66. if ( isset( $field[ $k ] ) ) {
  67. $atts[ $k ] = $field[ $k ];
  68. }
  69. }
  70. // atts2 (disabled="disabled")
  71. foreach ( $keys2 as $k ) {
  72. if ( ! empty( $field[ $k ] ) ) {
  73. $atts[ $k ] = $k;
  74. }
  75. }
  76. // remove empty atts
  77. $atts = acf_clean_atts( $atts );
  78. // open
  79. $html .= '<div class="acf-range-wrap">';
  80. // prepend
  81. if ( $field['prepend'] !== '' ) {
  82. $html .= '<div class="acf-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
  83. }
  84. // range
  85. $html .= acf_get_text_input( $atts );
  86. // Calculate input width based on the largest possible input character length.
  87. // Also take into account the step size for decimal steps minus - 1.5 chars for leading "0.".
  88. $len = max(
  89. strlen( strval( $field['min'] ) ),
  90. strlen( strval( $field['max'] ) )
  91. );
  92. if ( floatval( $atts['step'] ) < 1 ) {
  93. $len += strlen( strval( $field['step'] ) ) - 1.5;
  94. }
  95. // input
  96. $html .= acf_get_text_input(
  97. array(
  98. 'type' => 'number',
  99. 'id' => $atts['id'] . '-alt',
  100. 'value' => $atts['value'],
  101. 'step' => $atts['step'],
  102. // 'min' => $atts['min'], // removed to avoid browser validation errors
  103. // 'max' => $atts['max'],
  104. 'style' => 'width: ' . ( 1.8 + $len * 0.7 ) . 'em;',
  105. )
  106. );
  107. // append
  108. if ( $field['append'] !== '' ) {
  109. $html .= '<div class="acf-append">' . acf_esc_html( $field['append'] ) . '</div>';
  110. }
  111. // close
  112. $html .= '</div>';
  113. // return
  114. echo $html;
  115. }
  116. /*
  117. * render_field_settings()
  118. *
  119. * Create extra options for your field. This is rendered when editing a field.
  120. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  121. *
  122. * @type action
  123. * @since 3.6
  124. * @date 23/01/13
  125. *
  126. * @param $field - an array holding all the field's data
  127. */
  128. function render_field_settings( $field ) {
  129. acf_render_field_setting(
  130. $field,
  131. array(
  132. 'label' => __( 'Default Value', 'acf' ),
  133. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  134. 'type' => 'number',
  135. 'name' => 'default_value',
  136. )
  137. );
  138. }
  139. /**
  140. * Renders the field settings used in the "Validation" tab.
  141. *
  142. * @since 6.0
  143. *
  144. * @param array $field The field settings array.
  145. * @return void
  146. */
  147. function render_field_validation_settings( $field ) {
  148. acf_render_field_setting(
  149. $field,
  150. array(
  151. 'label' => __( 'Minimum Value', 'acf' ),
  152. 'instructions' => '',
  153. 'type' => 'number',
  154. 'name' => 'min',
  155. 'placeholder' => '0',
  156. )
  157. );
  158. acf_render_field_setting(
  159. $field,
  160. array(
  161. 'label' => __( 'Maximum Value', 'acf' ),
  162. 'instructions' => '',
  163. 'type' => 'number',
  164. 'name' => 'max',
  165. 'placeholder' => '100',
  166. )
  167. );
  168. }
  169. /**
  170. * Renders the field settings used in the "Presentation" tab.
  171. *
  172. * @since 6.0
  173. *
  174. * @param array $field The field settings array.
  175. * @return void
  176. */
  177. function render_field_presentation_settings( $field ) {
  178. acf_render_field_setting(
  179. $field,
  180. array(
  181. 'label' => __( 'Step Size', 'acf' ),
  182. 'instructions' => '',
  183. 'type' => 'number',
  184. 'name' => 'step',
  185. 'placeholder' => '1',
  186. )
  187. );
  188. acf_render_field_setting(
  189. $field,
  190. array(
  191. 'label' => __( 'Prepend', 'acf' ),
  192. 'instructions' => __( 'Appears before the input', 'acf' ),
  193. 'type' => 'text',
  194. 'name' => 'prepend',
  195. )
  196. );
  197. acf_render_field_setting(
  198. $field,
  199. array(
  200. 'label' => __( 'Append', 'acf' ),
  201. 'instructions' => __( 'Appears after the input', 'acf' ),
  202. 'type' => 'text',
  203. 'name' => 'append',
  204. )
  205. );
  206. }
  207. /**
  208. * Return the schema array for the REST API.
  209. *
  210. * @param array $field
  211. * @return array
  212. */
  213. public function get_rest_schema( array $field ) {
  214. $schema = array(
  215. 'type' => array( 'number', 'null' ),
  216. 'required' => ! empty( $field['required'] ),
  217. 'minimum' => empty( $field['min'] ) ? 0 : (int) $field['min'],
  218. 'maximum' => empty( $field['max'] ) ? 100 : (int) $field['max'],
  219. );
  220. if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
  221. $schema['default'] = (int) $field['default_value'];
  222. }
  223. return $schema;
  224. }
  225. /**
  226. * Apply basic formatting to prepare the value for default REST output.
  227. *
  228. * @param mixed $value
  229. * @param string|int $post_id
  230. * @param array $field
  231. * @return mixed
  232. */
  233. public function format_value_for_rest( $value, $post_id, array $field ) {
  234. return acf_format_numerics( $value );
  235. }
  236. }
  237. // initialize
  238. acf_register_field_type( 'acf_field_range' );
  239. endif; // class_exists check