| @@ -1,0 +1,341 @@ | ||
| 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::responsive_defaults() ); | |
| 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. Read every device-suffixed member through device_member(): | |
| 196 | + // a custom breakpoint (or the generator's value-less probe suffix) has no | |
| 197 | + // key of its own in the control's defaults, and indexing it directly is | |
| 198 | + // what produced "Undefined array key …" notices on every page. | |
| 199 | + if ( self::device_member( $value, 'isLinkedWidth', $device ) ) { | |
| 200 | + $commonWidth = self::device_member( $value, 'commonWidth', $device ); | |
| 201 | + if ( '' !== $commonWidth ) { | |
| 202 | + $css['border-width'] = $commonWidth . $widthUnit; | |
| 203 | + } | |
| 204 | + } else { | |
| 205 | + $topWidth = self::device_member( $value, 'topWidth', $device ); | |
| 206 | + $rightWidth = self::device_member( $value, 'rightWidth', $device ); | |
| 207 | + $bottomWidth = self::device_member( $value, 'bottomWidth', $device ); | |
| 208 | + $leftWidth = self::device_member( $value, 'leftWidth', $device ); | |
| 209 | + | |
| 210 | + // Only emit border-width when at least one side was actually set — | |
| 211 | + // otherwise every unlinked border produced "border-width:0px 0px 0px 0px". | |
| 212 | + $has_width = '' !== $topWidth || '' !== $rightWidth || '' !== $bottomWidth || '' !== $leftWidth; | |
| 213 | + if ( $has_width ) { | |
| 214 | + $topWidth = ! empty( $topWidth ) ? $topWidth : 0; | |
| 215 | + $rightWidth = ! empty( $rightWidth ) ? $rightWidth : 0; | |
| 216 | + $bottomWidth = ! empty( $bottomWidth ) ? $bottomWidth : 0; | |
| 217 | + $leftWidth = ! empty( $leftWidth ) ? $leftWidth : 0; | |
| 218 | + | |
| 219 | + $borderWidth = $topWidth . $widthUnit . ' ' . $rightWidth . $widthUnit . ' ' . $bottomWidth . $widthUnit . ' ' . $leftWidth . $widthUnit; | |
| 220 | + | |
| 221 | + $css['border-width'] = $borderWidth; | |
| 222 | + } | |
| 223 | + }//end if | |
| 224 | + | |
| 225 | + // Handle border style and color | |
| 226 | + if ( '' !== $value['borderStyle'] && 'default' !== $value['borderStyle'] ) { | |
| 227 | + $css['border-style'] = $value['borderStyle']; | |
| 228 | + } | |
| 229 | + if ( '' !== $value['borderColor'] ) { | |
| 230 | + $css['border-color'] = Color::get_css( $value['borderColor'] ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + // Separate handling of radius units | |
| 234 | + if ( $device ) { | |
| 235 | + $radiusUnit = self::get_unit([ | |
| 236 | + 'unit' => $value['unitRadius'], | |
| 237 | + 'unitTablet' => $value['unitRadiusTablet'], | |
| 238 | + 'unitMobile' => $value['unitRadiusMobile'], | |
| 239 | + ], $device); | |
| 240 | + } else { | |
| 241 | + $radiusUnit = $value['unitRadius']; | |
| 242 | + } | |
| 243 | + | |
| 244 | + // Handle radius | |
| 245 | + if ( self::device_member( $value, 'isLinkedRadius', $device ) ) { | |
| 246 | + $commonRadius = self::device_member( $value, 'commonRadius', $device ); | |
| 247 | + if ( '' !== $commonRadius ) { | |
| 248 | + $css['border-radius'] = $commonRadius . $radiusUnit; | |
| 249 | + } | |
| 250 | + } else { | |
| 251 | + $corners = [ | |
| 252 | + 'topRadius' => 'border-top-left-radius', | |
| 253 | + 'rightRadius' => 'border-top-right-radius', | |
| 254 | + 'bottomRadius' => 'border-bottom-right-radius', | |
| 255 | + 'leftRadius' => 'border-bottom-left-radius', | |
| 256 | + ]; | |
| 257 | + foreach ( $corners as $member => $property_name ) { | |
| 258 | + $corner = self::device_member( $value, $member, $device ); | |
| 259 | + if ( '' !== $corner ) { | |
| 260 | + $css[ $property_name ] = $corner . $radiusUnit; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + } | |
| 264 | + | |
| 265 | + // Handle transition duration | |
| 266 | + if ( self::has_value( $value['transitionDuration'] ) ) { | |
| 267 | + $css['transition'] = "border {$value['transitionDuration']}s, border-radius {$value['transitionDuration']}s"; | |
| 268 | + } | |
| 269 | + | |
| 270 | + return $css; | |
| 271 | + } | |
| 272 | + | |
| 273 | + public static function get_hover_css( $attribute_value, $property = '', $device = '' ) { | |
| 274 | + $value = wp_parse_args( $attribute_value, self::get_attribute_default_value( true ) ); // avoid Undefined error | |
| 275 | + $css = []; | |
| 276 | + | |
| 277 | + // Handle width units based on device | |
| 278 | + $widthUnit = self::get_unit( [ | |
| 279 | + 'unit' => $value['unitWidthH'], | |
| 280 | + 'unitTablet' => $value['unitWidthHTablet'], | |
| 281 | + 'unitMobile' => $value['unitWidthHMobile'], | |
| 282 | + ], $device ); | |
| 283 | + | |
| 284 | + if ( ! empty( $value['borderStyleH'] ) && 'default' !== $value['borderStyleH'] ) { | |
| 285 | + // Handle hover width | |
| 286 | + if ( ! empty( $value[ 'isLinkedWidthH' . $device ] ) ) { | |
| 287 | + $commonWidthH = self::device_member( $value, 'commonWidthH', $device ); | |
| 288 | + if ( '' !== $commonWidthH ) { | |
| 289 | + $css['border-width'] = $commonWidthH . $widthUnit; | |
| 290 | + } | |
| 291 | + } else { | |
| 292 | + $topWidth = ! empty( $value[ 'topWidthH' . $device ] ) ? $value[ 'topWidthH' . $device ] : 0; | |
| 293 | + $rightWidth = ! empty( $value[ 'rightWidthH' . $device ] ) ? $value[ 'rightWidthH' . $device ] : 0; | |
| 294 | + $bottomWidth = ! empty( $value[ 'bottomWidthH' . $device ] ) ? $value[ 'bottomWidthH' . $device ] : 0; | |
| 295 | + $leftWidth = ! empty( $value[ 'leftWidthH' . $device ] ) ? $value[ 'leftWidthH' . $device ] : 0; | |
| 296 | + | |
| 297 | + $css['border-width'] = $topWidth . $widthUnit . ' ' . $rightWidth . $widthUnit . ' ' . $bottomWidth . $widthUnit . ' ' . $leftWidth . $widthUnit; | |
| 298 | + } | |
| 299 | + | |
| 300 | + // Handle hover border color | |
| 301 | + if ( ! empty( $value['borderColorH'] ) ) { | |
| 302 | + $css['border-color'] = Color::get_css( $value['borderColorH'] ); | |
| 303 | + } | |
| 304 | + }//end if | |
| 305 | + | |
| 306 | + // Handle hover border style | |
| 307 | + if ( ! empty( $value['borderStyleH'] ) && 'default' !== $value['borderStyleH'] ) { | |
| 308 | + $css['border-style'] = $value['borderStyleH']; | |
| 309 | + } | |
| 310 | + | |
| 311 | + // Handle radius units based on device | |
| 312 | + $radiusUnit = self::get_unit( [ | |
| 313 | + 'unit' => $value['unitRadiusH'], | |
| 314 | + 'unitTablet' => $value['unitRadiusHTablet'], | |
| 315 | + 'unitMobile' => $value['unitRadiusHMobile'], | |
| 316 | + ], $device ); | |
| 317 | + | |
| 318 | + // Handle hover border radius | |
| 319 | + if ( ! empty( $value[ 'isLinkedRadiusH' . $device ] ) ) { | |
| 320 | + if ( '' !== ( $value[ 'commonRadiusH' . $device ] ?? '' ) ) { | |
| 321 | + $css['border-radius'] = $value[ 'commonRadiusH' . $device ] . $radiusUnit; | |
| 322 | + } | |
| 323 | + } else { | |
| 324 | + if ( isset( $value[ 'topRadiusH' . $device ] ) && '' !== $value[ 'topRadiusH' . $device ] ) { | |
| 325 | + $css['border-top-left-radius'] = $value[ 'topRadiusH' . $device ] . $radiusUnit; | |
| 326 | + } | |
| 327 | + if ( isset( $value[ 'rightRadiusH' . $device ] ) && '' !== $value[ 'rightRadiusH' . $device ] ) { | |
| 328 | + $css['border-top-right-radius'] = $value[ 'rightRadiusH' . $device ] . $radiusUnit; | |
| 329 | + } | |
| 330 | + if ( isset( $value[ 'bottomRadiusH' . $device ] ) && '' !== $value[ 'bottomRadiusH' . $device ] ) { | |
| 331 | + $css['border-bottom-right-radius'] = $value[ 'bottomRadiusH' . $device ] . $radiusUnit; | |
| 332 | + } | |
| 333 | + if ( isset( $value[ 'leftRadiusH' . $device ] ) && '' !== $value[ 'leftRadiusH' . $device ] ) { | |
| 334 | + $css['border-bottom-left-radius'] = $value[ 'leftRadiusH' . $device ] . $radiusUnit; | |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + return $css; | |
| 339 | + | |
| 340 | + } | |
| 341 | +} | |