| 1 |
<?php |
| 2 |
namespace ABlocks\Controls; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\ControlBaseAbstract; |
| 9 |
use ABlocks\Controls\Color; |
| 10 |
|
| 11 |
class Border extends ControlBaseAbstract { |
| 12 |
public static function get_attribute_default_value( $is_responsive = false ) { |
| 13 |
|
| 14 |
if ( $is_responsive ) { |
| 15 |
return array( |
| 16 |
'borderStyle' => 'default', |
| 17 |
'borderStyleH' => 'default', |
| 18 |
// border color |
| 19 |
'borderColor' => '', |
| 20 |
// border color - Hover |
| 21 |
'borderColorH' => '', |
| 22 |
// width |
| 23 |
'isLinkedWidth' => true, |
| 24 |
'isLinkedWidthTablet' => true, |
| 25 |
'isLinkedWidthMobile' => true, |
| 26 |
'commonWidth' => '', |
| 27 |
'commonWidthTablet' => '', |
| 28 |
'commonWidthMobile' => '', |
| 29 |
'topWidth' => '', |
| 30 |
'rightWidth' => '', |
| 31 |
'bottomWidth' => '', |
| 32 |
'leftWidth' => '', |
| 33 |
'topWidthTablet' => '', |
| 34 |
'rightWidthTablet' => '', |
| 35 |
'bottomWidthTablet' => '', |
| 36 |
'leftWidthTablet' => '', |
| 37 |
'topWidthMobile' => '', |
| 38 |
'rightWidthMobile' => '', |
| 39 |
'bottomWidthMobile' => '', |
| 40 |
'leftWidthMobile' => '', |
| 41 |
'unitWidth' => 'px', |
| 42 |
'unitWidthTablet' => '', |
| 43 |
'unitWidthMobile' => '', |
| 44 |
'isLinkedWidthH' => true, |
| 45 |
'isLinkedWidthHTablet' => true, |
| 46 |
'isLinkedWidthHMobile' => true, |
| 47 |
// Width Hover |
| 48 |
'commonWidthH' => '', |
| 49 |
'commonWidthHTablet' => '', |
| 50 |
'commonWidthHMobile' => '', |
| 51 |
'topWidthH' => '', |
| 52 |
'rightWidthH' => '', |
| 53 |
'bottomWidthH' => '', |
| 54 |
'leftWidthH' => '', |
| 55 |
'topWidthHTablet' => '', |
| 56 |
'rightWidthHTablet' => '', |
| 57 |
'bottomWidthHTablet' => '', |
| 58 |
'leftWidthHTablet' => '', |
| 59 |
'topWidthHMobile' => '', |
| 60 |
'rightWidthHMobile' => '', |
| 61 |
'bottomWidthHMobile' => '', |
| 62 |
'leftWidthHMobile' => '', |
| 63 |
'unitWidthH' => 'px', |
| 64 |
'unitWidthHTablet' => '', |
| 65 |
'unitWidthHMobile' => '', |
| 66 |
// Radius |
| 67 |
'isLinkedRadius' => true, |
| 68 |
'isLinkedRadiusTablet' => true, |
| 69 |
'isLinkedRadiusMobile' => true, |
| 70 |
'commonRadius' => '', |
| 71 |
'commonRadiusTablet' => '', |
| 72 |
'commonRadiusMobile' => '', |
| 73 |
'topRadius' => '', |
| 74 |
'rightRadius' => '', |
| 75 |
'bottomRadius' => '', |
| 76 |
'leftRadius' => '', |
| 77 |
'topRadiusTablet' => '', |
| 78 |
'rightRadiusTablet' => '', |
| 79 |
'bottomRadiusTablet' => '', |
| 80 |
'leftRadiusTablet' => '', |
| 81 |
'topRadiusMobile' => '', |
| 82 |
'rightRadiusMobile' => '', |
| 83 |
'bottomRadiusMobile' => '', |
| 84 |
'leftRadiusMobile' => '', |
| 85 |
'unitRadius' => 'px', |
| 86 |
'unitRadiusTablet' => '', |
| 87 |
'unitRadiusMobile' => '', |
| 88 |
// Radius Hover |
| 89 |
'isLinkedRadiusH' => true, |
| 90 |
'isLinkedRadiusHTablet' => true, |
| 91 |
'isLinkedRadiusHMobile' => true, |
| 92 |
'commonRadiusH' => '', |
| 93 |
'commonRadiusHTablet' => '', |
| 94 |
'commonRadiusHMobile' => '', |
| 95 |
'topRadiusH' => '', |
| 96 |
'rightRadiusH' => '', |
| 97 |
'bottomRadiusH' => '', |
| 98 |
'leftRadiusH' => '', |
| 99 |
'topRadiusHTablet' => '', |
| 100 |
'rightRadiusHTablet' => '', |
| 101 |
'bottomRadiusHTablet' => '', |
| 102 |
'leftRadiusHTablet' => '', |
| 103 |
'topRadiusHMobile' => '', |
| 104 |
'rightRadiusHMobile' => '', |
| 105 |
'bottomRadiusHMobile' => '', |
| 106 |
'leftRadiusHMobile' => '', |
| 107 |
'unitRadiusH' => 'px', |
| 108 |
'unitRadiusHTablet' => '', |
| 109 |
'unitRadiusHMobile' => '', |
| 110 |
'transitionDuration' => '' |
| 111 |
); |
| 112 |
}//end if |
| 113 |
return [ |
| 114 |
'borderStyle' => 'default', |
| 115 |
'borderStyleH' => 'default', |
| 116 |
// border color |
| 117 |
'borderColor' => '', |
| 118 |
// border color - Hover |
| 119 |
'borderColorH' => '', |
| 120 |
// border width - Normal |
| 121 |
'isLinkedWidth' => true, |
| 122 |
'isLinkedWidthTablet' => true, |
| 123 |
'isLinkedWidthMobile' => true, |
| 124 |
'commonWidth' => '', |
| 125 |
'topWidth' => '', |
| 126 |
'rightWidth' => '', |
| 127 |
'bottomWidth' => '', |
| 128 |
'leftWidth' => '', |
| 129 |
'unitWidth' => 'px', |
| 130 |
|
| 131 |
// border width - Hover |
| 132 |
'isLinkedWidthH' => true, |
| 133 |
'isLinkedWidthHTablet' => true, |
| 134 |
'isLinkedWidthHMobile' => true, |
| 135 |
'commonWidthH' => '', |
| 136 |
'topWidthH' => '', |
| 137 |
'rightWidthH' => '', |
| 138 |
'bottomWidthH' => '', |
| 139 |
'leftWidthH' => '', |
| 140 |
'unitWidthH' => 'px', |
| 141 |
|
| 142 |
// border Radius |
| 143 |
'isLinkedRadius' => true, |
| 144 |
'commonRadius' => '', |
| 145 |
'topRadius' => '', |
| 146 |
'rightRadius' => '', |
| 147 |
'bottomRadius' => '', |
| 148 |
'leftRadius' => '', |
| 149 |
'unitRadius' => 'px', |
| 150 |
|
| 151 |
// border RadiusH - Hover |
| 152 |
'isLinkedRadiusH' => true, |
| 153 |
'isLinkedRadiusHTablet' => true, |
| 154 |
'isLinkedRadiusHMobile' => true, |
| 155 |
'commonRadiusH' => '', |
| 156 |
'topRadiusH' => '', |
| 157 |
'leftRadiusH' => '', |
| 158 |
'unitRadiusH' => 'px', |
| 159 |
'transitionDuration' => '', |
| 160 |
]; |
| 161 |
} |
| 162 |
|
| 163 |
public static function get_attribute( $attributeName, $isResponsive = false ) { |
| 164 |
$attribute_value = self::get_attribute_default_value( $isResponsive ); |
| 165 |
if ( $isResponsive ) { |
| 166 |
return [ |
| 167 |
$attributeName => [ |
| 168 |
'type' => 'object', |
| 169 |
'default' => $attribute_value |
| 170 |
] |
| 171 |
]; |
| 172 |
} |
| 173 |
return [ |
| 174 |
$attributeName => [ |
| 175 |
'type' => 'object', |
| 176 |
'default' => $attribute_value |
| 177 |
] |
| 178 |
]; |
| 179 |
} |
| 180 |
public static function get_css( $attribute_value, $property = '', $device = '' ) { |
| 181 |
$value = wp_parse_args( $attribute_value, self::get_attribute_default_value( true ) ); |
| 182 |
$css = []; |
| 183 |
|
| 184 |
// Separate handling of width units |
| 185 |
if ( $device ) { |
| 186 |
$widthUnit = self::get_unit([ |
| 187 |
'unit' => $value['unitWidth'], |
| 188 |
'unitTablet' => $value['unitWidthTablet'], |
| 189 |
'unitMobile' => $value['unitWidthMobile'], |
| 190 |
], $device); |
| 191 |
} else { |
| 192 |
$widthUnit = $value['unitWidth']; |
| 193 |
} |
| 194 |
|
| 195 |
// Handle width |
| 196 |
if ( $value[ 'isLinkedWidth' . $device ] ) { |
| 197 |
if ( '' !== $value[ 'commonWidth' . $device ] ) { |
| 198 |
$css['border-width'] = $value[ 'commonWidth' . $device ] . $widthUnit; |
| 199 |
} |
| 200 |
} else { |
| 201 |
$topWidth = ! empty( $value[ 'topWidth' . $device ] ) ? $value[ 'topWidth' . $device ] : 0; |
| 202 |
$rightWidth = ! empty( $value[ 'rightWidth' . $device ] ) ? $value[ 'rightWidth' . $device ] : 0; |
| 203 |
$bottomWidth = ! empty( $value[ 'bottomWidth' . $device ] ) ? $value[ 'bottomWidth' . $device ] : 0; |
| 204 |
$leftWidth = ! empty( $value[ 'leftWidth' . $device ] ) ? $value[ 'leftWidth' . $device ] : 0; |
| 205 |
|
| 206 |
$borderWidth = $topWidth . $widthUnit . ' ' . $rightWidth . $widthUnit . ' ' . $bottomWidth . $widthUnit . ' ' . $leftWidth . $widthUnit; |
| 207 |
|
| 208 |
$css['border-width'] = $borderWidth; |
| 209 |
} |
| 210 |
|
| 211 |
// Handle border style and color |
| 212 |
if ( '' !== $value['borderStyle'] && 'default' !== $value['borderStyle'] ) { |
| 213 |
$css['border-style'] = $value['borderStyle']; |
| 214 |
} |
| 215 |
if ( '' !== $value['borderColor'] ) { |
| 216 |
$css['border-color'] = Color::get_css( $value['borderColor'] ); |
| 217 |
} |
| 218 |
|
| 219 |
// Separate handling of radius units |
| 220 |
if ( $device ) { |
| 221 |
$radiusUnit = self::get_unit([ |
| 222 |
'unit' => $value['unitRadius'], |
| 223 |
'unitTablet' => $value['unitRadiusTablet'], |
| 224 |
'unitMobile' => $value['unitRadiusMobile'], |
| 225 |
], $device); |
| 226 |
} else { |
| 227 |
$radiusUnit = $value['unitRadius']; |
| 228 |
} |
| 229 |
|
| 230 |
// Handle radius |
| 231 |
if ( $value[ 'isLinkedRadius' . $device ] ) { |
| 232 |
if ( '' !== $value[ 'commonRadius' . $device ] ) { |
| 233 |
$css['border-radius'] = $value[ 'commonRadius' . $device ] . $radiusUnit; |
| 234 |
} |
| 235 |
} else { |
| 236 |
if ( '' !== $value[ 'topRadius' . $device ] ) { |
| 237 |
$css['border-top-left-radius'] = $value[ 'topRadius' . $device ] . $radiusUnit; |
| 238 |
} |
| 239 |
if ( '' !== $value[ 'rightRadius' . $device ] ) { |
| 240 |
$css['border-top-right-radius'] = $value[ 'rightRadius' . $device ] . $radiusUnit; |
| 241 |
} |
| 242 |
if ( '' !== $value[ 'bottomRadius' . $device ] ) { |
| 243 |
$css['border-bottom-right-radius'] = $value[ 'bottomRadius' . $device ] . $radiusUnit; |
| 244 |
} |
| 245 |
if ( '' !== $value[ 'leftRadius' . $device ] ) { |
| 246 |
$css['border-bottom-left-radius'] = $value[ 'leftRadius' . $device ] . $radiusUnit; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// Handle transition duration |
| 251 |
if ( self::has_value( $value['transitionDuration'] ) ) { |
| 252 |
$css['transition'] = "border {$value['transitionDuration']}s, border-radius {$value['transitionDuration']}s"; |
| 253 |
} |
| 254 |
|
| 255 |
return $css; |
| 256 |
} |
| 257 |
|
| 258 |
public static function get_hover_css( $attribute_value, $property = '', $device = '' ) { |
| 259 |
$value = wp_parse_args( $attribute_value, self::get_attribute_default_value( true ) ); // avoid Undefined error |
| 260 |
$css = []; |
| 261 |
|
| 262 |
// Handle width units based on device |
| 263 |
$widthUnit = self::get_unit( [ |
| 264 |
'unit' => $value['unitWidthH'], |
| 265 |
'unitTablet' => $value['unitWidthHTablet'], |
| 266 |
'unitMobile' => $value['unitWidthHMobile'], |
| 267 |
], $device ); |
| 268 |
|
| 269 |
if ( ! empty( $value['borderStyleH'] ) && 'default' !== $value['borderStyleH'] ) { |
| 270 |
// Handle hover width |
| 271 |
if ( ! empty( $value[ 'isLinkedWidthH' . $device ] ) ) { |
| 272 |
if ( '' !== $value[ 'commonWidthH' . $device ] ) { |
| 273 |
$css['border-width'] = $value[ 'commonWidthH' . $device ] . $widthUnit; |
| 274 |
} |
| 275 |
} else { |
| 276 |
$topWidth = ! empty( $value[ 'topWidthH' . $device ] ) ? $value[ 'topWidthH' . $device ] : 0; |
| 277 |
$rightWidth = ! empty( $value[ 'rightWidthH' . $device ] ) ? $value[ 'rightWidthH' . $device ] : 0; |
| 278 |
$bottomWidth = ! empty( $value[ 'bottomWidthH' . $device ] ) ? $value[ 'bottomWidthH' . $device ] : 0; |
| 279 |
$leftWidth = ! empty( $value[ 'leftWidthH' . $device ] ) ? $value[ 'leftWidthH' . $device ] : 0; |
| 280 |
|
| 281 |
$css['border-width'] = $topWidth . $widthUnit . ' ' . $rightWidth . $widthUnit . ' ' . $bottomWidth . $widthUnit . ' ' . $leftWidth . $widthUnit; |
| 282 |
} |
| 283 |
|
| 284 |
// Handle hover border color |
| 285 |
if ( ! empty( $value['borderColorH'] ) ) { |
| 286 |
$css['border-color'] = Color::get_css( $value['borderColorH'] ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// Handle hover border style |
| 291 |
if ( ! empty( $value['borderStyleH'] ) && 'default' !== $value['borderStyleH'] ) { |
| 292 |
$css['border-style'] = $value['borderStyleH']; |
| 293 |
} |
| 294 |
|
| 295 |
// Handle radius units based on device |
| 296 |
$radiusUnit = self::get_unit( [ |
| 297 |
'unit' => $value['unitRadiusH'], |
| 298 |
'unitTablet' => $value['unitRadiusHTablet'], |
| 299 |
'unitMobile' => $value['unitRadiusHMobile'], |
| 300 |
], $device ); |
| 301 |
|
| 302 |
// Handle hover border radius |
| 303 |
if ( ! empty( $value[ 'isLinkedRadiusH' . $device ] ) ) { |
| 304 |
if ( '' !== ( $value[ 'commonRadiusH' . $device ] ?? '' ) ) { |
| 305 |
$css['border-radius'] = $value[ 'commonRadiusH' . $device ] . $radiusUnit; |
| 306 |
} |
| 307 |
} else { |
| 308 |
if ( isset( $value[ 'topRadiusH' . $device ] ) && '' !== $value[ 'topRadiusH' . $device ] ) { |
| 309 |
$css['border-top-left-radius'] = $value[ 'topRadiusH' . $device ] . $radiusUnit; |
| 310 |
} |
| 311 |
if ( isset( $value[ 'rightRadiusH' . $device ] ) && '' !== $value[ 'rightRadiusH' . $device ] ) { |
| 312 |
$css['border-top-right-radius'] = $value[ 'rightRadiusH' . $device ] . $radiusUnit; |
| 313 |
} |
| 314 |
if ( isset( $value[ 'bottomRadiusH' . $device ] ) && '' !== $value[ 'bottomRadiusH' . $device ] ) { |
| 315 |
$css['border-bottom-right-radius'] = $value[ 'bottomRadiusH' . $device ] . $radiusUnit; |
| 316 |
} |
| 317 |
if ( isset( $value[ 'leftRadiusH' . $device ] ) && '' !== $value[ 'leftRadiusH' . $device ] ) { |
| 318 |
$css['border-bottom-left-radius'] = $value[ 'leftRadiusH' . $device ] . $radiusUnit; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
return $css; |
| 323 |
|
| 324 |
} |
| 325 |
} |
| 326 |
|