123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- if ( ! class_exists( 'acf_field_number' ) ) :
- class acf_field_number extends acf_field {
- /*
- * initialize
- *
- * This function will setup the field type data
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function initialize() {
- // vars
- $this->name = 'number';
- $this->label = __( 'Number', 'acf' );
- $this->defaults = array(
- 'default_value' => '',
- 'min' => '',
- 'max' => '',
- 'step' => '',
- 'placeholder' => '',
- 'prepend' => '',
- 'append' => '',
- );
- }
- /*
- * render_field()
- *
- * Create the HTML interface for your field
- *
- * @param $field - an array holding all the field's data
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- */
- function render_field( $field ) {
- // vars
- $atts = array();
- $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
- $keys2 = array( 'readonly', 'disabled', 'required' );
- $html = '';
- // step
- if ( ! $field['step'] ) {
- $field['step'] = 'any';
- }
- // prepend
- if ( $field['prepend'] !== '' ) {
- $field['class'] .= ' acf-is-prepended';
- $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
- }
- // append
- if ( $field['append'] !== '' ) {
- $field['class'] .= ' acf-is-appended';
- $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
- }
- // atts (value="123")
- foreach ( $keys as $k ) {
- if ( isset( $field[ $k ] ) ) {
- $atts[ $k ] = $field[ $k ];
- }
- }
- // atts2 (disabled="disabled")
- foreach ( $keys2 as $k ) {
- if ( ! empty( $field[ $k ] ) ) {
- $atts[ $k ] = $k;
- }
- }
- // remove empty atts
- $atts = acf_clean_atts( $atts );
- // render
- $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
- // return
- echo $html;
- }
- /*
- * render_field_settings()
- *
- * Create extra options for your field. This is rendered when editing a field.
- * The value of $field['name'] can be used (like bellow) to save extra data to the $field
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field - an array holding all the field's data
- */
- function render_field_settings( $field ) {
- // default_value
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Default Value', 'acf' ),
- 'instructions' => __( 'Appears when creating a new post', 'acf' ),
- 'type' => 'text',
- 'name' => 'default_value',
- )
- );
- }
- /**
- * Renders the field settings used in the "Validation" tab.
- *
- * @since 6.0
- *
- * @param array $field The field settings array.
- * @return void
- */
- function render_field_validation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Minimum Value', 'acf' ),
- 'instructions' => '',
- 'type' => 'number',
- 'name' => 'min',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Maximum Value', 'acf' ),
- 'instructions' => '',
- 'type' => 'number',
- 'name' => 'max',
- )
- );
- }
- /**
- * Renders the field settings used in the "Presentation" tab.
- *
- * @since 6.0
- *
- * @param array $field The field settings array.
- * @return void
- */
- function render_field_presentation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Placeholder Text', 'acf' ),
- 'instructions' => __( 'Appears within the input', 'acf' ),
- 'type' => 'text',
- 'name' => 'placeholder',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Step Size', 'acf' ),
- 'instructions' => '',
- 'type' => 'number',
- 'name' => 'step',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Prepend', 'acf' ),
- 'instructions' => __( 'Appears before the input', 'acf' ),
- 'type' => 'text',
- 'name' => 'prepend',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Append', 'acf' ),
- 'instructions' => __( 'Appears after the input', 'acf' ),
- 'type' => 'text',
- 'name' => 'append',
- )
- );
- }
- /*
- * validate_value
- *
- * description
- *
- * @type function
- * @date 11/02/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function validate_value( $valid, $value, $field, $input ) {
- // remove ','
- if ( acf_str_exists( ',', $value ) ) {
- $value = str_replace( ',', '', $value );
- }
- // if value is not numeric...
- if ( ! is_numeric( $value ) ) {
- // allow blank to be saved
- if ( ! empty( $value ) ) {
- $valid = __( 'Value must be a number', 'acf' );
- }
- // return early
- return $valid;
- }
- // convert
- $value = floatval( $value );
- // min
- if ( is_numeric( $field['min'] ) && $value < floatval( $field['min'] ) ) {
- $valid = sprintf( __( 'Value must be equal to or higher than %d', 'acf' ), $field['min'] );
- }
- // max
- if ( is_numeric( $field['max'] ) && $value > floatval( $field['max'] ) ) {
- $valid = sprintf( __( 'Value must be equal to or lower than %d', 'acf' ), $field['max'] );
- }
- // return
- return $valid;
- }
- /*
- * update_value()
- *
- * This filter is appied to the $value before it is updated in the db
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value - the value which will be saved in the database
- * @param $field - the field array holding all the field options
- * @param $post_id - the $post_id of which the value will be saved
- *
- * @return $value - the modified value
- */
- function update_value( $value, $post_id, $field ) {
- // no formatting needed for empty value
- if ( empty( $value ) ) {
- return $value;
- }
- // remove ','
- if ( acf_str_exists( ',', $value ) ) {
- $value = str_replace( ',', '', $value );
- }
- // return
- return $value;
- }
- /**
- * Return the schema array for the REST API.
- *
- * @param array $field
- * @return array
- */
- public function get_rest_schema( array $field ) {
- $schema = array(
- 'type' => array( 'number', 'null' ),
- 'required' => ! empty( $field['required'] ),
- );
- if ( ! empty( $field['min'] ) ) {
- $schema['minimum'] = (float) $field['min'];
- }
- if ( ! empty( $field['max'] ) ) {
- $schema['maximum'] = (float) $field['max'];
- }
- if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
- $schema['default'] = (float) $field['default_value'];
- }
- return $schema;
- }
- /**
- * Apply basic formatting to prepare the value for default REST output.
- *
- * @param mixed $value
- * @param string|int $post_id
- * @param array $field
- * @return mixed
- */
- public function format_value_for_rest( $value, $post_id, array $field ) {
- return acf_format_numerics( $value );
- }
- }
- // initialize
- acf_register_field_type( 'acf_field_number' );
- endif; // class_exists check
|