Bez popisu

class-acf-field-range.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // default_value
  130. acf_render_field_setting(
  131. $field,
  132. array(
  133. 'label' => __( 'Default Value', 'acf' ),
  134. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  135. 'type' => 'number',
  136. 'name' => 'default_value',
  137. )
  138. );
  139. // min
  140. acf_render_field_setting(
  141. $field,
  142. array(
  143. 'label' => __( 'Minimum Value', 'acf' ),
  144. 'instructions' => '',
  145. 'type' => 'number',
  146. 'name' => 'min',
  147. 'placeholder' => '0',
  148. )
  149. );
  150. // max
  151. acf_render_field_setting(
  152. $field,
  153. array(
  154. 'label' => __( 'Maximum Value', 'acf' ),
  155. 'instructions' => '',
  156. 'type' => 'number',
  157. 'name' => 'max',
  158. 'placeholder' => '100',
  159. )
  160. );
  161. // step
  162. acf_render_field_setting(
  163. $field,
  164. array(
  165. 'label' => __( 'Step Size', 'acf' ),
  166. 'instructions' => '',
  167. 'type' => 'number',
  168. 'name' => 'step',
  169. 'placeholder' => '1',
  170. )
  171. );
  172. // prepend
  173. acf_render_field_setting(
  174. $field,
  175. array(
  176. 'label' => __( 'Prepend', 'acf' ),
  177. 'instructions' => __( 'Appears before the input', 'acf' ),
  178. 'type' => 'text',
  179. 'name' => 'prepend',
  180. )
  181. );
  182. // append
  183. acf_render_field_setting(
  184. $field,
  185. array(
  186. 'label' => __( 'Append', 'acf' ),
  187. 'instructions' => __( 'Appears after the input', 'acf' ),
  188. 'type' => 'text',
  189. 'name' => 'append',
  190. )
  191. );
  192. }
  193. /**
  194. * Return the schema array for the REST API.
  195. *
  196. * @param array $field
  197. * @return array
  198. */
  199. public function get_rest_schema( array $field ) {
  200. $schema = array(
  201. 'type' => array( 'number', 'null' ),
  202. 'required' => ! empty( $field['required'] ),
  203. 'minimum' => empty( $field['min'] ) ? 0 : (int) $field['min'],
  204. 'maximum' => empty( $field['max'] ) ? 100 : (int) $field['max'],
  205. );
  206. if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
  207. $schema['default'] = (int) $field['default_value'];
  208. }
  209. return $schema;
  210. }
  211. /**
  212. * Apply basic formatting to prepare the value for default REST output.
  213. *
  214. * @param mixed $value
  215. * @param string|int $post_id
  216. * @param array $field
  217. * @return mixed
  218. */
  219. public function format_value_for_rest( $value, $post_id, array $field ) {
  220. return acf_format_numerics( $value );
  221. }
  222. }
  223. // initialize
  224. acf_register_field_type( 'acf_field_range' );
  225. endif; // class_exists check