123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- if ( ! class_exists( 'acf_field_number' ) ) :
- class acf_field_number extends acf_field {
-
- function initialize() {
-
- $this->name = 'number';
- $this->label = __( 'Number', 'acf' );
- $this->defaults = array(
- 'default_value' => '',
- 'min' => '',
- 'max' => '',
- 'step' => '',
- 'placeholder' => '',
- 'prepend' => '',
- 'append' => '',
- );
- }
-
- function render_field( $field ) {
-
- $atts = array();
- $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
- $keys2 = array( 'readonly', 'disabled', 'required' );
- $html = '';
-
- if ( ! $field['step'] ) {
- $field['step'] = 'any';
- }
-
- if ( $field['prepend'] !== '' ) {
- $field['class'] .= ' acf-is-prepended';
- $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
- }
-
- if ( $field['append'] !== '' ) {
- $field['class'] .= ' acf-is-appended';
- $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
- }
-
- foreach ( $keys as $k ) {
- if ( isset( $field[ $k ] ) ) {
- $atts[ $k ] = $field[ $k ];
- }
- }
-
- foreach ( $keys2 as $k ) {
- if ( ! empty( $field[ $k ] ) ) {
- $atts[ $k ] = $k;
- }
- }
-
- $atts = acf_clean_atts( $atts );
-
- $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
-
- echo $html;
- }
-
- function render_field_settings( $field ) {
-
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Default Value', 'acf' ),
- 'instructions' => __( 'Appears when creating a new post', 'acf' ),
- 'type' => 'text',
- 'name' => 'default_value',
- )
- );
- }
-
- 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',
- )
- );
- }
-
- 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',
- )
- );
- }
-
- function validate_value( $valid, $value, $field, $input ) {
-
- if ( acf_str_exists( ',', $value ) ) {
- $value = str_replace( ',', '', $value );
- }
-
- if ( ! is_numeric( $value ) ) {
-
- if ( ! empty( $value ) ) {
- $valid = __( 'Value must be a number', 'acf' );
- }
-
- return $valid;
- }
-
- $value = floatval( $value );
-
- if ( is_numeric( $field['min'] ) && $value < floatval( $field['min'] ) ) {
- $valid = sprintf( __( 'Value must be equal to or higher than %d', 'acf' ), $field['min'] );
- }
-
- if ( is_numeric( $field['max'] ) && $value > floatval( $field['max'] ) ) {
- $valid = sprintf( __( 'Value must be equal to or lower than %d', 'acf' ), $field['max'] );
- }
-
- return $valid;
- }
-
- function update_value( $value, $post_id, $field ) {
-
- if ( empty( $value ) ) {
- return $value;
- }
-
- if ( acf_str_exists( ',', $value ) ) {
- $value = str_replace( ',', '', $value );
- }
-
- return $value;
- }
-
- 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;
- }
-
- public function format_value_for_rest( $value, $post_id, array $field ) {
- return acf_format_numerics( $value );
- }
- }
-
- acf_register_field_type( 'acf_field_number' );
- endif;
|