| 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 |
|
| 21 |
|
| 22 |
// do not delete! |
| 23 |
parent::__construct(); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* create_field() |
| 29 |
* |
| 30 |
* Create the HTML interface for your field |
| 31 |
* |
| 32 |
* @param $field - an array holding all the field's data |
| 33 |
* |
| 34 |
* @type action |
| 35 |
* @since 3.6 |
| 36 |
* @date 23/01/13 |
| 37 |
*/ |
| 38 |
|
| 39 |
function create_field( $field ) |
| 40 |
{ |
| 41 |
echo '<input type="number" step="any" value="' . esc_attr( $field['value'] ) . '" id="' . esc_attr( $field['id'] ) . '" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['name'] ) . '" />'; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/* |
| 46 |
* create_options() |
| 47 |
* |
| 48 |
* Create extra options for your field. This is rendered when editing a field. |
| 49 |
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 50 |
* |
| 51 |
* @type action |
| 52 |
* @since 3.6 |
| 53 |
* @date 23/01/13 |
| 54 |
* |
| 55 |
* @param $field - an array holding all the field's data |
| 56 |
*/ |
| 57 |
|
| 58 |
function create_options( $field ) |
| 59 |
{ |
| 60 |
// vars |
| 61 |
$defaults = array( |
| 62 |
'default_value' => '', |
| 63 |
); |
| 64 |
|
| 65 |
$field = array_merge($defaults, $field); |
| 66 |
$key = $field['name']; |
| 67 |
|
| 68 |
?> |
| 69 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 70 |
<td class="label"> |
| 71 |
<label><?php _e("Default Value",'acf'); ?></label> |
| 72 |
</td> |
| 73 |
<td> |
| 74 |
<?php |
| 75 |
|
| 76 |
do_action('acf/create_field', array( |
| 77 |
'type' => 'text', |
| 78 |
'name' => 'fields['.$key.'][default_value]', |
| 79 |
'value' => $field['default_value'], |
| 80 |
)); |
| 81 |
|
| 82 |
?> |
| 83 |
</td> |
| 84 |
</tr> |
| 85 |
<?php |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/* |
| 90 |
* update_value() |
| 91 |
* |
| 92 |
* This filter is appied to the $value before it is updated in the db |
| 93 |
* |
| 94 |
* @type filter |
| 95 |
* @since 3.6 |
| 96 |
* @date 23/01/13 |
| 97 |
* |
| 98 |
* @param $value - the value which will be saved in the database |
| 99 |
* @param $field - the field array holding all the field options |
| 100 |
* @param $post_id - the $post_id of which the value will be saved |
| 101 |
* |
| 102 |
* @return $value - the modified value |
| 103 |
*/ |
| 104 |
|
| 105 |
function update_value( $value, $post_id, $field ) |
| 106 |
{ |
| 107 |
// remove ',' |
| 108 |
$value = str_replace(',', '', $value); |
| 109 |
|
| 110 |
|
| 111 |
// convert to float. This removes any chars |
| 112 |
$value = floatval( $value ); |
| 113 |
|
| 114 |
|
| 115 |
// convert back to string. This alows decimals to save |
| 116 |
$value = (string) $value; |
| 117 |
|
| 118 |
|
| 119 |
return $value; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
new acf_field_number(); |
| 126 |
|
| 127 |
?> |