/core/fields/number.php

  1. <?php 
  2.  
  3. class acf_field_number extends acf_field 
  4.  
  5. /** 
  6. * __construct 
  7. * 
  8. * Set name / label needed for actions / filters 
  9. * 
  10. * @since 3.6 
  11. * @date 23/01/13 
  12. */ 
  13.  
  14. function __construct() 
  15. // vars 
  16. $this->name = 'number'; 
  17. $this->label = __("Number", 'acf'); 
  18. $this->defaults = array( 
  19. 'default_value' => '',  
  20. 'min' => '',  
  21. 'max' => '',  
  22. 'step' => '',  
  23. 'placeholder' => '',  
  24. 'prepend' => '',  
  25. 'append' => '' 
  26. ); 
  27.  
  28.  
  29. // do not delete! 
  30. parent::__construct(); 
  31.  
  32.  
  33. /** 
  34. * create_field() 
  35. * 
  36. * Create the HTML interface for your field 
  37. * 
  38. * @param $field - an array holding all the field's data 
  39. * 
  40. * @type action 
  41. * @since 3.6 
  42. * @date 23/01/13 
  43. */ 
  44.  
  45. function create_field( $field ) 
  46. // vars 
  47. $o = array( 'id', 'class', 'min', 'max', 'step', 'name', 'value', 'placeholder' ); 
  48. $e = ''; 
  49.  
  50.  
  51. // step 
  52. if( !$field['step'] ) 
  53. $field['step'] = 'any'; 
  54.  
  55. // prepend 
  56. if( $field['prepend'] !== "" ) 
  57. $field['class'] .= ' acf-is-prepended'; 
  58. $e .= '<div class="acf-input-prepend">' . $field['prepend'] . '</div>'; 
  59.  
  60.  
  61. // append 
  62. if( $field['append'] !== "" ) 
  63. $field['class'] .= ' acf-is-appended'; 
  64. $e .= '<div class="acf-input-append">' . $field['append'] . '</div>'; 
  65.  
  66.  
  67. $e .= '<div class="acf-input-wrap">'; 
  68. $e .= '<input type="number"'; 
  69.  
  70. foreach( $o as $k ) 
  71. $e .= ' ' . $k . '="' . esc_attr( $field[ $k ] ) . '"';  
  72.  
  73. $e .= ' />'; 
  74. $e .= '</div>'; 
  75.  
  76.  
  77. // return 
  78. echo $e; 
  79.  
  80.  
  81.  
  82. /** 
  83. * create_options() 
  84. * 
  85. * Create extra options for your field. This is rendered when editing a field. 
  86. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  87. * 
  88. * @type action 
  89. * @since 3.6 
  90. * @date 23/01/13 
  91. * 
  92. * @param $field - an array holding all the field's data 
  93. */ 
  94.  
  95. function create_options( $field ) 
  96. // vars 
  97. $key = $field['name']; 
  98.  
  99. ?> 
  100. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  101. <td class="label"> 
  102. <label><?php _e("Default Value", 'acf'); ?></label> 
  103. <p><?php _e("Appears when creating a new post", 'acf') ?></p> 
  104. </td> 
  105. <td> 
  106. <?php 
  107.  
  108. do_action('acf/create_field', array( 
  109. 'type' => 'number',  
  110. 'name' => 'fields['.$key.'][default_value]',  
  111. 'value' => $field['default_value'],  
  112. )); 
  113.  
  114. ?> 
  115. </td> 
  116. </tr> 
  117. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  118. <td class="label"> 
  119. <label><?php _e("Placeholder Text", 'acf'); ?></label> 
  120. <p><?php _e("Appears within the input", 'acf') ?></p> 
  121. </td> 
  122. <td> 
  123. <?php  
  124. do_action('acf/create_field', array( 
  125. 'type' => 'text',  
  126. 'name' => 'fields[' .$key.'][placeholder]',  
  127. 'value' => $field['placeholder'],  
  128. )); 
  129. ?> 
  130. </td> 
  131. </tr> 
  132. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  133. <td class="label"> 
  134. <label><?php _e("Prepend", 'acf'); ?></label> 
  135. <p><?php _e("Appears before the input", 'acf') ?></p> 
  136. </td> 
  137. <td> 
  138. <?php  
  139. do_action('acf/create_field', array( 
  140. 'type' => 'text',  
  141. 'name' => 'fields[' .$key.'][prepend]',  
  142. 'value' => $field['prepend'],  
  143. )); 
  144. ?> 
  145. </td> 
  146. </tr> 
  147. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  148. <td class="label"> 
  149. <label><?php _e("Append", 'acf'); ?></label> 
  150. <p><?php _e("Appears after the input", 'acf') ?></p> 
  151. </td> 
  152. <td> 
  153. <?php  
  154. do_action('acf/create_field', array( 
  155. 'type' => 'text',  
  156. 'name' => 'fields[' .$key.'][append]',  
  157. 'value' => $field['append'],  
  158. )); 
  159. ?> 
  160. </td> 
  161. </tr> 
  162. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  163. <td class="label"> 
  164. <label><?php _e("Minimum Value", 'acf'); ?></label> 
  165. </td> 
  166. <td> 
  167. <?php 
  168.  
  169. do_action('acf/create_field', array( 
  170. 'type' => 'number',  
  171. 'name' => 'fields['.$key.'][min]',  
  172. 'value' => $field['min'],  
  173. )); 
  174.  
  175. ?> 
  176. </td> 
  177. </tr> 
  178. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  179. <td class="label"> 
  180. <label><?php _e("Maximum Value", 'acf'); ?></label> 
  181. </td> 
  182. <td> 
  183. <?php 
  184.  
  185. do_action('acf/create_field', array( 
  186. 'type' => 'number',  
  187. 'name' => 'fields['.$key.'][max]',  
  188. 'value' => $field['max'],  
  189. )); 
  190.  
  191. ?> 
  192. </td> 
  193. </tr> 
  194. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  195. <td class="label"> 
  196. <label><?php _e("Step Size", 'acf'); ?></label> 
  197. </td> 
  198. <td> 
  199. <?php 
  200.  
  201. do_action('acf/create_field', array( 
  202. 'type' => 'number',  
  203. 'name' => 'fields['.$key.'][step]',  
  204. 'value' => $field['step'],  
  205. )); 
  206.  
  207. ?> 
  208. </td> 
  209. </tr> 
  210. <?php 
  211.  
  212.  
  213. /** 
  214. * update_value() 
  215. * 
  216. * This filter is appied to the $value before it is updated in the db 
  217. * 
  218. * @type filter 
  219. * @since 3.6 
  220. * @date 23/01/13 
  221. * 
  222. * @param $value - the value which will be saved in the database 
  223. * @param $field - the field array holding all the field options 
  224. * @param $post_id - the $post_id of which the value will be saved 
  225. * 
  226. * @return $value - the modified value 
  227. */ 
  228.  
  229. function update_value( $value, $post_id, $field ) 
  230. // no formatting needed for empty value 
  231. if( empty($value) ) { 
  232.  
  233. return $value; 
  234.  
  235.  
  236.  
  237. // remove ', ' 
  238. $value = str_replace(', ', '', $value); 
  239.  
  240.  
  241. // convert to float. This removes any chars 
  242. $value = floatval( $value ); 
  243.  
  244.  
  245. // convert back to string. This alows decimals to save 
  246. $value = (string) $value; 
  247.  
  248.  
  249. return $value; 
  250.  
  251.  
  252.  
  253. new acf_field_number(); 
  254.  
  255. ?> 
.