spinner.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: spinner
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_spinner' ) ) {
  11. class CSF_Field_spinner 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. 'max' => 100,
  18. 'min' => 0,
  19. 'step' => 1,
  20. 'unit' => '',
  21. ) );
  22. echo $this->field_before();
  23. echo '<div class="csf--spin"><input type="number" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'"'. $this->field_attributes( array( 'class' => 'csf-input-number' ) ) .' data-min="'. esc_attr( $args['min'] ) .'" data-max="'. esc_attr( $args['max'] ) .'" data-step="'. esc_attr( $args['step'] ) .'" data-unit="'. esc_attr( $args['unit'] ) .'" step="any" /></div>';
  24. echo $this->field_after();
  25. }
  26. public function enqueue() {
  27. if ( ! wp_script_is( 'jquery-ui-spinner' ) ) {
  28. wp_enqueue_script( 'jquery-ui-spinner' );
  29. }
  30. }
  31. public function output() {
  32. $output = '';
  33. $elements = ( is_array( $this->field['output'] ) ) ? $this->field['output'] : array_filter( (array) $this->field['output'] );
  34. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  35. $mode = ( ! empty( $this->field['output_mode'] ) ) ? $this->field['output_mode'] : 'width';
  36. $unit = ( ! empty( $this->field['unit'] ) ) ? $this->field['unit'] : 'px';
  37. if ( ! empty( $elements ) && isset( $this->value ) && $this->value !== '' ) {
  38. foreach ( $elements as $key_property => $element ) {
  39. if ( is_numeric( $key_property ) ) {
  40. if ( $mode ) {
  41. $output = implode( ',', $elements ) .'{'. $mode .':'. $this->value . $unit . $important .';}';
  42. }
  43. break;
  44. } else {
  45. $output .= $element .'{'. $key_property .':'. $this->value . $unit . $important .'}';
  46. }
  47. }
  48. }
  49. $this->parent->output_css .= $output;
  50. return $output;
  51. }
  52. }
  53. }