number.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: number
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_number' ) ) {
  11. class CSF_Field_number extends CSF_Fields {
  12. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'min' => 'any',
  18. 'max' => 'any',
  19. 'step' => 'any',
  20. 'unit' => '',
  21. ) );
  22. echo $this->field_before();
  23. echo '<div class="csf--wrap">';
  24. echo '<input type="number" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'"'. $this->field_attributes() .' min="'. esc_attr( $args['min'] ) .'" max="'. esc_attr( $args['max'] ) .'" step="'. esc_attr( $args['step'] ) .'"/>';
  25. echo ( ! empty( $args['unit'] ) ) ? '<span class="csf--unit">'. esc_attr( $args['unit'] ) .'</span>' : '';
  26. echo '</div>';
  27. echo $this->field_after();
  28. }
  29. public function output() {
  30. $output = '';
  31. $elements = ( is_array( $this->field['output'] ) ) ? $this->field['output'] : array_filter( (array) $this->field['output'] );
  32. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  33. $mode = ( ! empty( $this->field['output_mode'] ) ) ? $this->field['output_mode'] : 'width';
  34. $unit = ( ! empty( $this->field['unit'] ) ) ? $this->field['unit'] : 'px';
  35. if ( ! empty( $elements ) && isset( $this->value ) && $this->value !== '' ) {
  36. foreach ( $elements as $key_property => $element ) {
  37. if ( is_numeric( $key_property ) ) {
  38. if ( $mode ) {
  39. $output = implode( ',', $elements ) .'{'. $mode .':'. $this->value . $unit . $important .';}';
  40. }
  41. break;
  42. } else {
  43. $output .= $element .'{'. $key_property .':'. $this->value . $unit . $important .'}';
  44. }
  45. }
  46. }
  47. $this->parent->output_css .= $output;
  48. return $output;
  49. }
  50. }
  51. }