class-acf-field-number.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. if ( ! class_exists( 'acf_field_number' ) ) :
  3. class acf_field_number extends acf_field {
  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 = 'number';
  19. $this->label = __( 'Number', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'min' => '',
  23. 'max' => '',
  24. 'step' => '',
  25. 'placeholder' => '',
  26. 'prepend' => '',
  27. 'append' => '',
  28. );
  29. }
  30. /*
  31. * render_field()
  32. *
  33. * Create the HTML interface for your field
  34. *
  35. * @param $field - an array holding all the field's data
  36. *
  37. * @type action
  38. * @since 3.6
  39. * @date 23/01/13
  40. */
  41. function render_field( $field ) {
  42. // vars
  43. $atts = array();
  44. $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
  45. $keys2 = array( 'readonly', 'disabled', 'required' );
  46. $html = '';
  47. // step
  48. if ( ! $field['step'] ) {
  49. $field['step'] = 'any';
  50. }
  51. // prepend
  52. if ( $field['prepend'] !== '' ) {
  53. $field['class'] .= ' acf-is-prepended';
  54. $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
  55. }
  56. // append
  57. if ( $field['append'] !== '' ) {
  58. $field['class'] .= ' acf-is-appended';
  59. $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
  60. }
  61. // atts (value="123")
  62. foreach ( $keys as $k ) {
  63. if ( isset( $field[ $k ] ) ) {
  64. $atts[ $k ] = $field[ $k ];
  65. }
  66. }
  67. // atts2 (disabled="disabled")
  68. foreach ( $keys2 as $k ) {
  69. if ( ! empty( $field[ $k ] ) ) {
  70. $atts[ $k ] = $k;
  71. }
  72. }
  73. // remove empty atts
  74. $atts = acf_clean_atts( $atts );
  75. // render
  76. $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
  77. // return
  78. echo $html;
  79. }
  80. /*
  81. * render_field_settings()
  82. *
  83. * Create extra options for your field. This is rendered when editing a field.
  84. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  85. *
  86. * @type action
  87. * @since 3.6
  88. * @date 23/01/13
  89. *
  90. * @param $field - an array holding all the field's data
  91. */
  92. function render_field_settings( $field ) {
  93. // default_value
  94. acf_render_field_setting(
  95. $field,
  96. array(
  97. 'label' => __( 'Default Value', 'acf' ),
  98. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  99. 'type' => 'text',
  100. 'name' => 'default_value',
  101. )
  102. );
  103. }
  104. /**
  105. * Renders the field settings used in the "Validation" tab.
  106. *
  107. * @since 6.0
  108. *
  109. * @param array $field The field settings array.
  110. * @return void
  111. */
  112. function render_field_validation_settings( $field ) {
  113. acf_render_field_setting(
  114. $field,
  115. array(
  116. 'label' => __( 'Minimum Value', 'acf' ),
  117. 'instructions' => '',
  118. 'type' => 'number',
  119. 'name' => 'min',
  120. )
  121. );
  122. acf_render_field_setting(
  123. $field,
  124. array(
  125. 'label' => __( 'Maximum Value', 'acf' ),
  126. 'instructions' => '',
  127. 'type' => 'number',
  128. 'name' => 'max',
  129. )
  130. );
  131. }
  132. /**
  133. * Renders the field settings used in the "Presentation" tab.
  134. *
  135. * @since 6.0
  136. *
  137. * @param array $field The field settings array.
  138. * @return void
  139. */
  140. function render_field_presentation_settings( $field ) {
  141. acf_render_field_setting(
  142. $field,
  143. array(
  144. 'label' => __( 'Placeholder Text', 'acf' ),
  145. 'instructions' => __( 'Appears within the input', 'acf' ),
  146. 'type' => 'text',
  147. 'name' => 'placeholder',
  148. )
  149. );
  150. acf_render_field_setting(
  151. $field,
  152. array(
  153. 'label' => __( 'Step Size', 'acf' ),
  154. 'instructions' => '',
  155. 'type' => 'number',
  156. 'name' => 'step',
  157. )
  158. );
  159. acf_render_field_setting(
  160. $field,
  161. array(
  162. 'label' => __( 'Prepend', 'acf' ),
  163. 'instructions' => __( 'Appears before the input', 'acf' ),
  164. 'type' => 'text',
  165. 'name' => 'prepend',
  166. )
  167. );
  168. acf_render_field_setting(
  169. $field,
  170. array(
  171. 'label' => __( 'Append', 'acf' ),
  172. 'instructions' => __( 'Appears after the input', 'acf' ),
  173. 'type' => 'text',
  174. 'name' => 'append',
  175. )
  176. );
  177. }
  178. /*
  179. * validate_value
  180. *
  181. * description
  182. *
  183. * @type function
  184. * @date 11/02/2014
  185. * @since 5.0.0
  186. *
  187. * @param $post_id (int)
  188. * @return $post_id (int)
  189. */
  190. function validate_value( $valid, $value, $field, $input ) {
  191. // remove ','
  192. if ( acf_str_exists( ',', $value ) ) {
  193. $value = str_replace( ',', '', $value );
  194. }
  195. // if value is not numeric...
  196. if ( ! is_numeric( $value ) ) {
  197. // allow blank to be saved
  198. if ( ! empty( $value ) ) {
  199. $valid = __( 'Value must be a number', 'acf' );
  200. }
  201. // return early
  202. return $valid;
  203. }
  204. // convert
  205. $value = floatval( $value );
  206. // min
  207. if ( is_numeric( $field['min'] ) && $value < floatval( $field['min'] ) ) {
  208. $valid = sprintf( __( 'Value must be equal to or higher than %d', 'acf' ), $field['min'] );
  209. }
  210. // max
  211. if ( is_numeric( $field['max'] ) && $value > floatval( $field['max'] ) ) {
  212. $valid = sprintf( __( 'Value must be equal to or lower than %d', 'acf' ), $field['max'] );
  213. }
  214. // return
  215. return $valid;
  216. }
  217. /*
  218. * update_value()
  219. *
  220. * This filter is appied to the $value before it is updated in the db
  221. *
  222. * @type filter
  223. * @since 3.6
  224. * @date 23/01/13
  225. *
  226. * @param $value - the value which will be saved in the database
  227. * @param $field - the field array holding all the field options
  228. * @param $post_id - the $post_id of which the value will be saved
  229. *
  230. * @return $value - the modified value
  231. */
  232. function update_value( $value, $post_id, $field ) {
  233. // no formatting needed for empty value
  234. if ( empty( $value ) ) {
  235. return $value;
  236. }
  237. // remove ','
  238. if ( acf_str_exists( ',', $value ) ) {
  239. $value = str_replace( ',', '', $value );
  240. }
  241. // return
  242. return $value;
  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. );
  255. if ( ! empty( $field['min'] ) ) {
  256. $schema['minimum'] = (float) $field['min'];
  257. }
  258. if ( ! empty( $field['max'] ) ) {
  259. $schema['maximum'] = (float) $field['max'];
  260. }
  261. if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
  262. $schema['default'] = (float) $field['default_value'];
  263. }
  264. return $schema;
  265. }
  266. /**
  267. * Apply basic formatting to prepare the value for default REST output.
  268. *
  269. * @param mixed $value
  270. * @param string|int $post_id
  271. * @param array $field
  272. * @return mixed
  273. */
  274. public function format_value_for_rest( $value, $post_id, array $field ) {
  275. return acf_format_numerics( $value );
  276. }
  277. }
  278. // initialize
  279. acf_register_field_type( 'acf_field_number' );
  280. endif; // class_exists check