dimensions.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: dimensions
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_dimensions' ) ) {
  11. class CSF_Field_dimensions 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. 'width_icon' => '<i class="fas fa-arrows-alt-h"></i>',
  18. 'height_icon' => '<i class="fas fa-arrows-alt-v"></i>',
  19. 'width_placeholder' => esc_html__( 'width', 'sakurairo_csf' ),
  20. 'height_placeholder' => esc_html__( 'height', 'sakurairo_csf' ),
  21. 'width' => true,
  22. 'height' => true,
  23. 'unit' => true,
  24. 'show_units' => true,
  25. 'units' => array( 'px', '%', 'em' )
  26. ) );
  27. $default_values = array(
  28. 'width' => '',
  29. 'height' => '',
  30. 'unit' => 'px',
  31. );
  32. $value = wp_parse_args( $this->value, $default_values );
  33. $unit = ( count( $args['units'] ) === 1 && ! empty( $args['unit'] ) ) ? $args['units'][0] : '';
  34. $is_unit = ( ! empty( $unit ) ) ? ' csf--is-unit' : '';
  35. echo $this->field_before();
  36. echo '<div class="csf--inputs" data-depend-id="'. esc_attr( $this->field['id'] ) .'">';
  37. if ( ! empty( $args['width'] ) ) {
  38. $placeholder = ( ! empty( $args['width_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args['width_placeholder'] ) .'"' : '';
  39. echo '<div class="csf--input">';
  40. echo ( ! empty( $args['width_icon'] ) ) ? '<span class="csf--label csf--icon">'. $args['width_icon'] .'</span>' : '';
  41. echo '<input type="number" name="'. esc_attr( $this->field_name( '[width]' ) ) .'" value="'. esc_attr( $value['width'] ) .'"'. $placeholder .' class="csf-input-number'. esc_attr( $is_unit ) .'" step="any" />';
  42. echo ( ! empty( $unit ) ) ? '<span class="csf--label csf--unit">'. esc_attr( $args['units'][0] ) .'</span>' : '';
  43. echo '</div>';
  44. }
  45. if ( ! empty( $args['height'] ) ) {
  46. $placeholder = ( ! empty( $args['height_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args['height_placeholder'] ) .'"' : '';
  47. echo '<div class="csf--input">';
  48. echo ( ! empty( $args['height_icon'] ) ) ? '<span class="csf--label csf--icon">'. $args['height_icon'] .'</span>' : '';
  49. echo '<input type="number" name="'. esc_attr( $this->field_name( '[height]' ) ) .'" value="'. esc_attr( $value['height'] ) .'"'. $placeholder .' class="csf-input-number'. esc_attr( $is_unit ) .'" step="any" />';
  50. echo ( ! empty( $unit ) ) ? '<span class="csf--label csf--unit">'. esc_attr( $args['units'][0] ) .'</span>' : '';
  51. echo '</div>';
  52. }
  53. if ( ! empty( $args['unit'] ) && ! empty( $args['show_units'] ) && count( $args['units'] ) > 1 ) {
  54. echo '<div class="csf--input">';
  55. echo '<select name="'. esc_attr( $this->field_name( '[unit]' ) ) .'">';
  56. foreach ( $args['units'] as $unit ) {
  57. $selected = ( $value['unit'] === $unit ) ? ' selected' : '';
  58. echo '<option value="'. esc_attr( $unit ) .'"'. esc_attr( $selected ) .'>'. esc_attr( $unit ) .'</option>';
  59. }
  60. echo '</select>';
  61. echo '</div>';
  62. }
  63. echo '</div>';
  64. echo $this->field_after();
  65. }
  66. public function output() {
  67. $output = '';
  68. $element = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output'];
  69. $prefix = ( ! empty( $this->field['output_prefix'] ) ) ? $this->field['output_prefix'] .'-' : '';
  70. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  71. $unit = ( ! empty( $this->value['unit'] ) ) ? $this->value['unit'] : 'px';
  72. $width = ( isset( $this->value['width'] ) && $this->value['width'] !== '' ) ? $prefix .'width:'. $this->value['width'] . $unit . $important .';' : '';
  73. $height = ( isset( $this->value['height'] ) && $this->value['height'] !== '' ) ? $prefix .'height:'. $this->value['height'] . $unit . $important .';' : '';
  74. if ( $width !== '' || $height !== '' ) {
  75. $output = $element .'{'. $width . $height .'}';
  76. }
  77. $this->parent->output_css .= $output;
  78. return $output;
  79. }
  80. }
  81. }