1
0

spacing.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: spacing
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_spacing' ) ) {
  11. class CSF_Field_spacing 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. 'top_icon' => '<i class="fas fa-long-arrow-alt-up"></i>',
  18. 'right_icon' => '<i class="fas fa-long-arrow-alt-right"></i>',
  19. 'bottom_icon' => '<i class="fas fa-long-arrow-alt-down"></i>',
  20. 'left_icon' => '<i class="fas fa-long-arrow-alt-left"></i>',
  21. 'all_icon' => '<i class="fas fa-arrows-alt"></i>',
  22. 'top_placeholder' => esc_html__( 'top', 'sakurairo_csf' ),
  23. 'right_placeholder' => esc_html__( 'right', 'sakurairo_csf' ),
  24. 'bottom_placeholder' => esc_html__( 'bottom', 'sakurairo_csf' ),
  25. 'left_placeholder' => esc_html__( 'left', 'sakurairo_csf' ),
  26. 'all_placeholder' => esc_html__( 'all', 'sakurairo_csf' ),
  27. 'top' => true,
  28. 'left' => true,
  29. 'bottom' => true,
  30. 'right' => true,
  31. 'unit' => true,
  32. 'show_units' => true,
  33. 'all' => false,
  34. 'units' => array( 'px', '%', 'em' )
  35. ) );
  36. $default_values = array(
  37. 'top' => '',
  38. 'right' => '',
  39. 'bottom' => '',
  40. 'left' => '',
  41. 'all' => '',
  42. 'unit' => 'px',
  43. );
  44. $value = wp_parse_args( $this->value, $default_values );
  45. $unit = ( count( $args['units'] ) === 1 && ! empty( $args['unit'] ) ) ? $args['units'][0] : '';
  46. $is_unit = ( ! empty( $unit ) ) ? ' csf--is-unit' : '';
  47. echo $this->field_before();
  48. echo '<div class="csf--inputs" data-depend-id="'. esc_attr( $this->field['id'] ) .'">';
  49. if ( ! empty( $args['all'] ) ) {
  50. $placeholder = ( ! empty( $args['all_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args['all_placeholder'] ) .'"' : '';
  51. echo '<div class="csf--input">';
  52. echo ( ! empty( $args['all_icon'] ) ) ? '<span class="csf--label csf--icon">'. $args['all_icon'] .'</span>' : '';
  53. echo '<input type="number" name="'. esc_attr( $this->field_name( '[all]' ) ) .'" value="'. esc_attr( $value['all'] ) .'"'. $placeholder .' class="csf-input-number'. esc_attr( $is_unit ) .'" step="any" />';
  54. echo ( $unit ) ? '<span class="csf--label csf--unit">'. esc_attr( $args['units'][0] ) .'</span>' : '';
  55. echo '</div>';
  56. } else {
  57. $properties = array();
  58. foreach ( array( 'top', 'right', 'bottom', 'left' ) as $prop ) {
  59. if ( ! empty( $args[$prop] ) ) {
  60. $properties[] = $prop;
  61. }
  62. }
  63. $properties = ( $properties === array( 'right', 'left' ) ) ? array_reverse( $properties ) : $properties;
  64. foreach ( $properties as $property ) {
  65. $placeholder = ( ! empty( $args[$property.'_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args[$property.'_placeholder'] ) .'"' : '';
  66. echo '<div class="csf--input">';
  67. echo ( ! empty( $args[$property.'_icon'] ) ) ? '<span class="csf--label csf--icon">'. $args[$property.'_icon'] .'</span>' : '';
  68. echo '<input type="number" name="'. esc_attr( $this->field_name( '['. $property .']' ) ) .'" value="'. esc_attr( $value[$property] ) .'"'. $placeholder .' class="csf-input-number'. esc_attr( $is_unit ) .'" step="any" />';
  69. echo ( $unit ) ? '<span class="csf--label csf--unit">'. esc_attr( $args['units'][0] ) .'</span>' : '';
  70. echo '</div>';
  71. }
  72. }
  73. if ( ! empty( $args['unit'] ) && ! empty( $args['show_units'] ) && count( $args['units'] ) > 1 ) {
  74. echo '<div class="csf--input">';
  75. echo '<select name="'. esc_attr( $this->field_name( '[unit]' ) ) .'">';
  76. foreach ( $args['units'] as $unit ) {
  77. $selected = ( $value['unit'] === $unit ) ? ' selected' : '';
  78. echo '<option value="'. esc_attr( $unit ) .'"'. esc_attr( $selected ) .'>'. esc_attr( $unit ) .'</option>';
  79. }
  80. echo '</select>';
  81. echo '</div>';
  82. }
  83. echo '</div>';
  84. echo $this->field_after();
  85. }
  86. public function output() {
  87. $output = '';
  88. $element = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output'];
  89. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  90. $unit = ( ! empty( $this->value['unit'] ) ) ? $this->value['unit'] : 'px';
  91. $mode = ( ! empty( $this->field['output_mode'] ) ) ? $this->field['output_mode'] : 'padding';
  92. if ( $mode === 'border-radius' || $mode === 'radius' ) {
  93. $top = 'border-top-left-radius';
  94. $right = 'border-top-right-radius';
  95. $bottom = 'border-bottom-right-radius';
  96. $left = 'border-bottom-left-radius';
  97. } else if ( $mode === 'relative' || $mode === 'absolute' || $mode === 'none' ) {
  98. $top = 'top';
  99. $right = 'right';
  100. $bottom = 'bottom';
  101. $left = 'left';
  102. } else {
  103. $top = $mode .'-top';
  104. $right = $mode .'-right';
  105. $bottom = $mode .'-bottom';
  106. $left = $mode .'-left';
  107. }
  108. if ( ! empty( $this->field['all'] ) && isset( $this->value['all'] ) && $this->value['all'] !== '' ) {
  109. $output = $element .'{';
  110. $output .= $top .':'. $this->value['all'] . $unit . $important .';';
  111. $output .= $right .':'. $this->value['all'] . $unit . $important .';';
  112. $output .= $bottom .':'. $this->value['all'] . $unit . $important .';';
  113. $output .= $left .':'. $this->value['all'] . $unit . $important .';';
  114. $output .= '}';
  115. } else {
  116. $top = ( isset( $this->value['top'] ) && $this->value['top'] !== '' ) ? $top .':'. $this->value['top'] . $unit . $important .';' : '';
  117. $right = ( isset( $this->value['right'] ) && $this->value['right'] !== '' ) ? $right .':'. $this->value['right'] . $unit . $important .';' : '';
  118. $bottom = ( isset( $this->value['bottom'] ) && $this->value['bottom'] !== '' ) ? $bottom .':'. $this->value['bottom'] . $unit . $important .';' : '';
  119. $left = ( isset( $this->value['left'] ) && $this->value['left'] !== '' ) ? $left .':'. $this->value['left'] . $unit . $important .';' : '';
  120. if ( $top !== '' || $right !== '' || $bottom !== '' || $left !== '' ) {
  121. $output = $element .'{'. $top . $right . $bottom . $left .'}';
  122. }
  123. }
  124. $this->parent->output_css .= $output;
  125. return $output;
  126. }
  127. }
  128. }