| 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 Background extends ControlBaseAbstract { |
| 12 |
public static function get_attribute_default_value( $is_responsive = false ) { |
| 13 |
if ( $is_responsive ) { |
| 14 |
return [ |
| 15 |
'backgroundType' => 'none', |
| 16 |
'backgroundTypeH' => 'none', |
| 17 |
'backgroundColor' => '', |
| 18 |
'backgroundColorH' => '', |
| 19 |
'imgId' => '', |
| 20 |
'imgIdTablet' => '', |
| 21 |
'imgIdMobile' => '', |
| 22 |
'imgUrl' => '', |
| 23 |
'imgUrlTablet' => '', |
| 24 |
'imgUrlMobile' => '', |
| 25 |
'imgSize' => '', |
| 26 |
'imgSizeTablet' => '', |
| 27 |
'imgSizeMobile' => '', |
| 28 |
'imgPosition' => '', |
| 29 |
'imgPositionTablet' => '', |
| 30 |
'imgPositionMobile' => '', |
| 31 |
'imgXPositionUnit' => 'px', |
| 32 |
'imgXPositionUnitTablet' => '', |
| 33 |
'imgXPositionUnitMobile' => '', |
| 34 |
'imgXPosition' => '', |
| 35 |
'imgXPositionTablet' => '', |
| 36 |
'imgXPositionMobile' => '', |
| 37 |
'imgYPositionUnit' => 'px', |
| 38 |
'imgYPositionUnitTablet' => '', |
| 39 |
'imgYPositionUnitMobile' => '', |
| 40 |
'imgYPosition' => '', |
| 41 |
'imgYPositionTablet' => '', |
| 42 |
'imgYPositionMobile' => '', |
| 43 |
'imgAttachment' => '', |
| 44 |
'imgRepeat' => '', |
| 45 |
'imgRepeatTablet' => '', |
| 46 |
'imgRepeatMobile' => '', |
| 47 |
'imgDisplaySize' => '', |
| 48 |
'imgDisplaySizeTablet' => '', |
| 49 |
'imgDisplaySizeMobile' => '', |
| 50 |
'imgDisplaySizeWidth' => '', |
| 51 |
'imgDisplaySizeWidthTablet' => '', |
| 52 |
'imgDisplaySizeWidthMobile' => '', |
| 53 |
'imgDisplaySizeWidthUnit' => 'px', |
| 54 |
'imgDisplaySizeWidthUnitTablet' => '', |
| 55 |
'imgDisplaySizeWidthUnitMobile' => '', |
| 56 |
// Background image - Hover |
| 57 |
'imgIdH' => '', |
| 58 |
'imgIdTabletH' => '', |
| 59 |
'imgIdMobileH' => '', |
| 60 |
'imgUrlH' => '', |
| 61 |
'imgUrlHTablet' => '', |
| 62 |
'imgUrlHMobile' => '', |
| 63 |
'imgSizeH' => '', |
| 64 |
'imgSizeHTablet' => '', |
| 65 |
'imgSizeHMobile' => '', |
| 66 |
'imgPositionH' => '', |
| 67 |
'imgPositionHTablet' => '', |
| 68 |
'imgPositionHMobile' => '', |
| 69 |
'imgXPositionUnitH' => 'px', |
| 70 |
'imgXPositionUnitHTablet' => '', |
| 71 |
'imgXPositionUnitHMobile' => '', |
| 72 |
'imgXPositionH' => '', |
| 73 |
'imgXPositionHTablet' => '', |
| 74 |
'imgXPositionHMobile' => '', |
| 75 |
'imgYPositionUnitH' => 'px', |
| 76 |
'imgYPositionUnitHTablet' => '', |
| 77 |
'imgYPositionUnitHMobile' => '', |
| 78 |
'imgYPositionH' => '', |
| 79 |
'imgYPositionHTablet' => '', |
| 80 |
'imgYPositionHMobile' => '', |
| 81 |
'imgAttachmentH' => '', |
| 82 |
'imgRepeatH' => '', |
| 83 |
'imgRepeatHTablet' => '', |
| 84 |
'imgRepeatHMobile' => '', |
| 85 |
'imgDisplaySizeH' => '', |
| 86 |
'imgDisplaySizeHTablet' => '', |
| 87 |
'imgDisplaySizeHMobile' => '', |
| 88 |
'imgDisplaySizeWidthH' => '', |
| 89 |
'imgDisplaySizeWidthHTablet' => '', |
| 90 |
'imgDisplaySizeWidthHMobile' => '', |
| 91 |
'imgDisplaySizeWidthHUnit' => 'px', |
| 92 |
'imgDisplaySizeWidthHUnitTablet' => '', |
| 93 |
'imgDisplaySizeWidthHUnitMobile' => '', |
| 94 |
// Background image transition |
| 95 |
'transitionDuration' => '', |
| 96 |
// Background video |
| 97 |
'videoUrl' => '', |
| 98 |
'youtubeURL' => '', |
| 99 |
'vimeoURL' => '', |
| 100 |
'selfHostedURL' => '', |
| 101 |
'externalLink' => false, |
| 102 |
'videoSource' => 'none', |
| 103 |
'videoPlayOnce' => '', |
| 104 |
'videoStartTime' => '', |
| 105 |
'videoEndTime' => '', |
| 106 |
'playOnMobile' => '', |
| 107 |
'videoPrivacyMode' => '', |
| 108 |
'fallbackImageUrl' => '', |
| 109 |
]; |
| 110 |
}//end if |
| 111 |
return [ |
| 112 |
'backgroundType' => 'none', |
| 113 |
'backgroundTypeH' => 'none', |
| 114 |
'backgroundColor' => '', |
| 115 |
'backgroundColorH' => '', |
| 116 |
// Background image - Normal |
| 117 |
'imgId' => '', |
| 118 |
'imgUrl' => '', |
| 119 |
'imgSize' => '', |
| 120 |
'imgPosition' => '', |
| 121 |
'imgAttachment' => '', |
| 122 |
'imgRepeat' => '', |
| 123 |
'imgDisplaySize' => '', |
| 124 |
'imgdisplaySizeWidth' => '', |
| 125 |
'imgDisplaySizeWidthUnit' => 'px', |
| 126 |
'imgXPositionUnit' => 'px', |
| 127 |
'imgXPosition' => '', |
| 128 |
'imgYPositionUnit' => 'px', |
| 129 |
'imgYPosition' => '', |
| 130 |
// Background image - Hover |
| 131 |
'imgIdH' => '', |
| 132 |
'imgUrlH' => '', |
| 133 |
'imgSizeH' => '', |
| 134 |
'imgPositionH' => '', |
| 135 |
'imgXPositionUnitH' => 'px', |
| 136 |
'imgXPositionH' => '', |
| 137 |
'imgYPositionUnitH' => 'px', |
| 138 |
'imgYPositionH' => '', |
| 139 |
'imgAttachmentH' => '', |
| 140 |
'imgRepeatH' => '', |
| 141 |
'imgDisplaySizeH' => '', |
| 142 |
'imgDisplaySizeWidthH' => '', |
| 143 |
'imgDisplaySizeWidthHUnit' => 'px', |
| 144 |
// Background image transition |
| 145 |
'transitionDuration' => '', |
| 146 |
// Background video |
| 147 |
'videoUrl' => '', |
| 148 |
'youtubeURL' => '', |
| 149 |
'vimeoURL' => '', |
| 150 |
'selfHostedURL' => '', |
| 151 |
'externalLink' => false, |
| 152 |
'videoSource' => 'none', |
| 153 |
'videoPlayOnce' => '', |
| 154 |
'videoStartTime' => '', |
| 155 |
'videoEndTime' => '', |
| 156 |
'playOnMobile' => '', |
| 157 |
'videoPrivacyMode' => '', |
| 158 |
'fallbackImageUrl' => '', |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
public static function get_attribute( $attribute_name, $is_responsive = false ) { |
| 163 |
return [ |
| 164 |
$attribute_name => [ |
| 165 |
'type' => 'object', |
| 166 |
'default' => self::get_attribute_default_value( $is_responsive ), |
| 167 |
] |
| 168 |
]; |
| 169 |
} |
| 170 |
|
| 171 |
public static function get_css( $attribute_value, $property = '', $device = '' ) { |
| 172 |
$value = wp_parse_args( $attribute_value, self::get_attribute_default_value( (bool) $device ) ); |
| 173 |
$css = []; |
| 174 |
|
| 175 |
if ( $device ) { |
| 176 |
$unitXPositionUnit = self::get_unit( |
| 177 |
[ |
| 178 |
'unit' => isset( $value['imgXPositionUnit'] ) ? $value['imgXPositionUnit'] : '', |
| 179 |
'unitTablet' => isset( $value['imgXPositionUnitTablet'] ) ? $value['imgXPositionUnitTablet'] : '', |
| 180 |
'unitMobile' => isset( $value['imgXPositionUnitMobile'] ) ? $value['imgXPositionUnitMobile'] : '', |
| 181 |
], |
| 182 |
$device |
| 183 |
); |
| 184 |
$unitYPositionUnit = self::get_unit( |
| 185 |
[ |
| 186 |
'unit' => isset( $value['imgYPositionUnit'] ) ? $value['imgYPositionUnit'] : '', |
| 187 |
'unitTablet' => isset( $value['imgYPositionUnitTablet'] ) ? $value['imgYPositionUnitTablet'] : '', |
| 188 |
'unitMobile' => isset( $value['imgYPositionUnitMobile'] ) ? $value['imgYPositionUnitMobile'] : '', |
| 189 |
], |
| 190 |
$device |
| 191 |
); |
| 192 |
$imgDisplaySizeWidthUnit = self::get_unit( |
| 193 |
[ |
| 194 |
'unit' => isset( $value['imgDisplaySizeWidthUnit'] ) ? $value['imgDisplaySizeWidthUnit'] : '', |
| 195 |
'unitTablet' => isset( $value['imgDisplaySizeWidthUnitTablet'] ) ? $value['imgDisplaySizeWidthUnitTablet'] : '', |
| 196 |
'unitMobile' => isset( $value['imgDisplaySizeWidthUnitMobile'] ) ? $value['imgDisplaySizeWidthUnitMobile'] : '', |
| 197 |
], |
| 198 |
$device |
| 199 |
); |
| 200 |
} else { |
| 201 |
$unitXPositionUnit = isset( $value['imgXPositionUnit'] ) ? $value['imgXPositionUnit'] : ''; |
| 202 |
$unitYPositionUnit = isset( $value['imgYPositionUnit'] ) ? $value['imgYPositionUnit'] : ''; |
| 203 |
$imgDisplaySizeWidthUnit = isset( $value['imgDisplaySizeWidthUnit'] ) ? $value['imgDisplaySizeWidthUnit'] : ''; |
| 204 |
}//end if |
| 205 |
|
| 206 |
if ( 'image' === $value['backgroundType'] && '' !== $value[ 'imgUrl' . $device ] ) { |
| 207 |
// background image |
| 208 |
$css[ $property . '-image' ] = 'url("' . $value[ 'imgUrl' . $device ] . '")'; |
| 209 |
// background-position css |
| 210 |
if ( '' !== $value[ 'imgPosition' . $device ] ) { |
| 211 |
if ( 'custom' === $value[ 'imgPosition' . $device ] ) { |
| 212 |
$imgXPosition = isset( $value[ 'imgXPosition' . $device ] ) ? $value[ 'imgXPosition' . $device ] : '0'; |
| 213 |
$imgYPosition = isset( $value[ 'imgYPosition' . $device ] ) ? $value[ 'imgYPosition' . $device ] : '0'; |
| 214 |
$css[ $property . '-position' ] = $imgXPosition . $unitXPositionUnit . $imgYPosition . $unitYPositionUnit; |
| 215 |
} else { |
| 216 |
$css[ $property . '-position' ] = $value[ 'imgPosition' . $device ]; |
| 217 |
} |
| 218 |
} |
| 219 |
// background-attachments css |
| 220 |
if ( isset( $value[ 'imgAttachment' . $device ] ) && '' !== $value[ 'imgAttachment' . $device ] ) { |
| 221 |
$css[ $property . '-attachment' ] = $value[ 'imgAttachment' . $device ]; |
| 222 |
} |
| 223 |
// background-repeat css |
| 224 |
if ( '' !== $value[ 'imgRepeat' . $device ] ) { |
| 225 |
$css[ $property . '-repeat' ] = $value[ 'imgRepeat' . $device ]; |
| 226 |
} |
| 227 |
// background-size css |
| 228 |
if ( '' !== $value[ 'imgDisplaySize' . $device ] ) { |
| 229 |
if ( 'custom' === $value[ 'imgDisplaySize' . $device ] ) { |
| 230 |
$css[ $property . '-size' ] = isset( $value[ 'imgDisplaySizeWidth' . $device ] ) ? $value[ 'imgDisplaySizeWidth' . $device ] . $imgDisplaySizeWidthUnit . ' auto' : ''; |
| 231 |
} else { |
| 232 |
$css[ $property . '-size' ] = $value[ 'imgDisplaySize' . $device ]; |
| 233 |
} |
| 234 |
} |
| 235 |
} elseif ( 'video' === $value['backgroundType'] && '' !== $value['fallbackImageUrl'] && ( $value['videoSource'] == 'none' || ( $value['videoSource'] == 'selfHosted' && empty( $value['videoUrl'] ) ) || ( $value['videoSource'] == 'youtube' && empty( $value['youtubeURL'] ) ) || ( $value['videoSource'] == 'vimeo' && empty( $value['vimeoURL'] ) ) ) ) { |
| 236 |
$css[ $property . '-image' ] = 'url("' . $value['fallbackImageUrl'] . '")'; |
| 237 |
$css[ $property . '-size' ] = 'cover'; |
| 238 |
} elseif ( 'color' === $value['backgroundType'] && $value['backgroundColor'] ) { |
| 239 |
$css[ $property ] = Color::get_css( $value['backgroundColor'] ); |
| 240 |
}//end if |
| 241 |
|
| 242 |
if ( '' !== $value['transitionDuration'] ) { |
| 243 |
$css['transition'] = $property . ' ' . $value['transitionDuration'] . 's ease'; |
| 244 |
} |
| 245 |
|
| 246 |
return $css; |
| 247 |
} |
| 248 |
|
| 249 |
public static function get_hover_css( $attribute_value, $property = '', $device = '' ) { |
| 250 |
$value = wp_parse_args( $attribute_value, self::get_attribute_default_value( (bool) $device ) ); |
| 251 |
$css = []; |
| 252 |
|
| 253 |
if ( $device ) { |
| 254 |
$unitXPositionUnitH = self::get_unit( |
| 255 |
[ |
| 256 |
'unit' => isset( $value['imgXPositionUnitH'] ) ? $value['imgXPositionUnitH'] : '', |
| 257 |
'unitTablet' => isset( $value['imgXPositionUnitHTablet'] ) ? $value['imgXPositionUnitHTablet'] : '', |
| 258 |
'unitMobile' => isset( $value['imgXPositionUnitHMobile'] ) ? $value['imgXPositionUnitHMobile'] : '', |
| 259 |
], |
| 260 |
$device |
| 261 |
); |
| 262 |
$unitYPositionUnitH = self::get_unit( |
| 263 |
[ |
| 264 |
'unit' => isset( $value['imgYPositionUnitH'] ) ? $value['imgYPositionUnitH'] : '', |
| 265 |
'unitTablet' => isset( $value['imgYPositionUnitHTablet'] ) ? $value['imgYPositionUnitHTablet'] : '', |
| 266 |
'unitMobile' => isset( $value['imgYPositionUnitHMobile'] ) ? $value['imgYPositionUnitHMobile'] : '', |
| 267 |
], |
| 268 |
$device |
| 269 |
); |
| 270 |
$imgDisplaySizeWidthUnitH = self::get_unit( |
| 271 |
[ |
| 272 |
'unit' => isset( $value['imgDisplaySizeWidthHUnit'] ) ? $value['imgDisplaySizeWidthHUnit'] : '', |
| 273 |
'unitTablet' => isset( $value['imgDisplaySizeWidthHUnitTablet'] ) ? $value['imgDisplaySizeWidthHUnitTablet'] : '', |
| 274 |
'unitMobile' => isset( $value['imgDisplaySizeWidthHUnitMobile'] ) ? $value['imgDisplaySizeWidthHUnitMobile'] : '', |
| 275 |
], |
| 276 |
$device |
| 277 |
); |
| 278 |
} else { |
| 279 |
$unitXPositionUnitH = isset( $value['imgXPositionUnitH'] ) ? $value['imgXPositionUnitH'] : ''; |
| 280 |
$unitYPositionUnitH = isset( $value['imgYPositionUnitH'] ) ? $value['imgYPositionUnitH'] : ''; |
| 281 |
$imgDisplaySizeWidthUnitH = isset( $value['imgDisplaySizeWidthHUnit'] ) ? $value['imgDisplaySizeWidthHUnit'] : ''; |
| 282 |
}//end if |
| 283 |
|
| 284 |
if ( 'image' === $value['backgroundTypeH'] && '' !== $value[ 'imgUrlH' . $device ] ) { |
| 285 |
// background image |
| 286 |
$css[ $property . '-image' ] = 'url("' . $value[ 'imgUrlH' . $device ] . '")'; |
| 287 |
// background-position css |
| 288 |
if ( '' !== $value[ 'imgPositionH' . $device ] ) { |
| 289 |
if ( 'custom' === $value[ 'imgPositionH' . $device ] ) { |
| 290 |
$imgXPosition = isset( $value[ 'imgXPositionH' . $device ] ) ? $value[ 'imgXPositionH' . $device ] : '0'; |
| 291 |
$imgYPosition = isset( $value[ 'imgYPositionH' . $device ] ) ? $value[ 'imgYPositionH' . $device ] : '0'; |
| 292 |
$css[ $property . '-position' ] = $imgXPosition . $unitXPositionUnitH . $imgYPosition . $unitYPositionUnitH; |
| 293 |
} else { |
| 294 |
$css[ $property . '-position' ] = $value[ 'imgPositionH' . $device ]; |
| 295 |
} |
| 296 |
} |
| 297 |
// background-attachments css |
| 298 |
if ( '' !== isset( $value[ 'imgAttachmentH' . $device ] ) ) { |
| 299 |
$css[ $property . '-attachment' ] = $value[ 'imgAttachmentH' . $device ]; |
| 300 |
} |
| 301 |
// background-repeat css |
| 302 |
if ( '' !== $value[ 'imgRepeatH' . $device ] ) { |
| 303 |
$css[ $property . '-repeat' ] = $value[ 'imgRepeatH' . $device ]; |
| 304 |
} |
| 305 |
// background-size css |
| 306 |
if ( '' !== $value[ 'imgDisplaySizeH' . $device ] ) { |
| 307 |
if ( 'custom' === $value[ 'imgDisplaySizeH' . $device ] ) { |
| 308 |
$css[ $property . '-size' ] = isset( $value[ 'imgDisplaySizeWidthH' . $device ] ) ? $value[ 'imgDisplaySizeWidthH' . $device ] . $imgDisplaySizeWidthUnitH . ' auto' : ''; |
| 309 |
} else { |
| 310 |
$css[ $property . '-size' ] = $value[ 'imgDisplaySizeH' . $device ]; |
| 311 |
} |
| 312 |
} |
| 313 |
} elseif ( 'video' === $value['backgroundTypeH'] ) { |
| 314 |
|
| 315 |
} elseif ( 'color' === $value['backgroundTypeH'] && $value['backgroundColorH'] ) { |
| 316 |
$css[ $property ] = Color::get_css( $value['backgroundColorH'] ); |
| 317 |
}//end if |
| 318 |
|
| 319 |
return $css; |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|