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