1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
<?php
class Papi_Property_Number extends Papi_Property {
public $convert_type = 'int';
public function format_value( $value, $slug, $post_id ) {
$value = is_string( $value ) && is_numeric( $value ) ? $value + 0 : $value;
if ( is_float( $value ) ) {
return floatval( $value );
} else {
return intval( $value );
}
}
public function get_default_settings() {
return [
'max' => '',
'min' => '',
'step' => '',
'type' => 'number'
];
}
public function get_value() {
return $this->format_value(
parent::get_value(),
$this->get_slug(),
papi_get_post_id()
);
}
public function html() {
$settings = $this->get_settings();
$value = $this->get_value();
if ( $settings->type === 'range' ) {
$settings->max = papi_is_empty( $settings->max )
? 100 : $settings->max;
$settings->min = papi_is_empty( $settings->min )
? 0 : $settings->min;
$settings->step = papi_is_empty( $settings->step )
? 1 : $settings->step;
}
if ( $settings->min !== 0 && $value < $settings->min ) {
$value = $settings->min;
}
papi_render_html_tag( 'input', [
'id' => $this->html_id(),
'max' => $settings->max,
'min' => $settings->min,
'name' => $this->html_name(),
'step' => $settings->step,
'type' => $settings->type,
'value' => $value
] );
}
public function import_value( $value, $slug, $post_id ) {
return $this->format_value( $value, $slug, $post_id );
}
}