| 1 |
<?php |
| 2 |
namespace BTECH; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 5 |
class GetCSS{ |
| 6 |
static function isValidCSS($property, $value) { |
| 7 |
if ( empty( $value ) && $value !== '0' && $value !== 0 ) { |
| 8 |
return ''; |
| 9 |
} |
| 10 |
return "$property: $value;"; |
| 11 |
} |
| 12 |
|
| 13 |
static function getBackgroundCSS( $bg, $isSolid = true, $isGradient = true, $isImage = true ) { |
| 14 |
extract( $bg ); |
| 15 |
$type = $type ?? 'solid'; |
| 16 |
$color = $color ?? ''; |
| 17 |
$gradient = $gradient ?? 'linear-gradient(135deg, #0040E3, #18D4FD)'; |
| 18 |
$image = $image ?? []; |
| 19 |
$position = $position ?? 'center center'; |
| 20 |
$attachment = $attachment ?? ''; |
| 21 |
$repeat = $repeat ?? ''; |
| 22 |
$size = $size ?? ''; |
| 23 |
$overlayColor = $overlayColor ?? ''; |
| 24 |
|
| 25 |
if ( 'gradient' === $type && $isGradient ) { |
| 26 |
$styles = self::isValidCSS('background', $gradient); |
| 27 |
} elseif ( 'image' === $type && $isImage ) { |
| 28 |
$imgUrl = $image['url'] ?? ''; |
| 29 |
$styles = "background: url($imgUrl);" |
| 30 |
. self::isValidCSS('background-color', $overlayColor) |
| 31 |
. self::isValidCSS('background-position', $position) |
| 32 |
. self::isValidCSS('background-size', $size) |
| 33 |
. self::isValidCSS('background-repeat', $repeat) |
| 34 |
. self::isValidCSS('background-attachment', $attachment) |
| 35 |
. self::isValidCSS('background-repeat', $repeat) |
| 36 |
. "background-blend-mode: overlay;"; |
| 37 |
} else { |
| 38 |
$styles = $isSolid ? self::isValidCSS('background', $color) : ''; |
| 39 |
} |
| 40 |
|
| 41 |
return $styles; |
| 42 |
} |
| 43 |
|
| 44 |
static function getBorderBoxCSS( $border ) { |
| 45 |
if ( empty( $border ) ) return ''; |
| 46 |
|
| 47 |
// Helper to generate border CSS string |
| 48 |
$generateBorderCSS = function( $borderObj ) { |
| 49 |
$color = $borderObj['color'] ?? '#000000'; |
| 50 |
$style = $borderObj['style'] ?? 'solid'; |
| 51 |
$width = $borderObj['width'] ?? '0px'; |
| 52 |
return "$width $style $color"; |
| 53 |
}; |
| 54 |
|
| 55 |
// `$border !== array_values( $border )` is the version-agnostic equivalent |
| 56 |
// of `! array_is_list( $border )` (associative array) — array_is_list() |
| 57 |
// needs PHP 8.1 / the WP 6.5 polyfill, but this plugin supports WP 6.2. |
| 58 |
if ( is_array( $border ) && $border !== array_values( $border ) ) { |
| 59 |
// Check for individual sides |
| 60 |
if ( |
| 61 |
array_key_exists( 'top', $border ) || |
| 62 |
array_key_exists( 'right', $border ) || |
| 63 |
array_key_exists( 'bottom', $border ) || |
| 64 |
array_key_exists( 'left', $border ) |
| 65 |
) { |
| 66 |
$sides = [ 'top', 'right', 'bottom', 'left' ]; |
| 67 |
$css = ''; |
| 68 |
foreach ( $sides as $side ) { |
| 69 |
if ( !empty( $border[$side] ) ) { |
| 70 |
$css .= "border-$side: " . $generateBorderCSS( $border[$side] ) . "; "; |
| 71 |
} |
| 72 |
} |
| 73 |
return trim( $css ); |
| 74 |
} else { |
| 75 |
// Single border object |
| 76 |
return self::isValidCSS( 'border', $generateBorderCSS( $border ) ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return ''; |
| 81 |
} |
| 82 |
|
| 83 |
static function getBorderCSS( $border ) { |
| 84 |
extract( $border ); |
| 85 |
$width = $width ?? '0px'; |
| 86 |
$style = $style ?? 'solid'; |
| 87 |
$color = $color ?? '#0000'; |
| 88 |
$side = $side ?? 'all'; |
| 89 |
$radius = $radius ?? '0px'; |
| 90 |
|
| 91 |
$borderSideCheck = function( $s ) use ( $side ) { |
| 92 |
$bSide = strtolower( $side ); |
| 93 |
return false !== strpos( $bSide, 'all' ) || false !== strpos( $bSide, $s ); |
| 94 |
}; |
| 95 |
|
| 96 |
$noWidth = $width === '0px' || !$width; |
| 97 |
$borderCSS = "$width $style $color"; |
| 98 |
|
| 99 |
$styles = ''; |
| 100 |
foreach ( ['top', 'right', 'bottom', 'left'] as $s ) { |
| 101 |
if ( !$noWidth && $borderSideCheck( $s ) ) { $styles .= "border-$s: $borderCSS;"; } |
| 102 |
} |
| 103 |
if ( $radius ) { $styles .= "border-radius: $radius;"; } |
| 104 |
|
| 105 |
return $styles; |
| 106 |
} |
| 107 |
|
| 108 |
static function getColorsCSS( $colors ) { |
| 109 |
extract( $colors ); |
| 110 |
$color = $color ?? ''; |
| 111 |
$bgType = $bgType ?? 'solid'; |
| 112 |
$bg = $bg ?? ''; |
| 113 |
$gradient = $gradient ?? 'linear-gradient(135deg, #0040E3, #18D4FD)'; |
| 114 |
|
| 115 |
$background = ( $bgType === 'gradient' ) ? $gradient : $bg; |
| 116 |
|
| 117 |
$styles = self::isValidCSS('color', $color); |
| 118 |
$styles .= ($gradient || $bg) ? self::isValidCSS('background', $background) : ''; |
| 119 |
|
| 120 |
return $styles; |
| 121 |
} |
| 122 |
|
| 123 |
static function getIconCSS( $icon, $isSize = true, $isColor = true ) { |
| 124 |
extract( $icon ); |
| 125 |
$fontSize = $fontSize ?? 16; |
| 126 |
$colorType = $colorType ?? 'solid'; |
| 127 |
$color = $color ?? 'inherit'; |
| 128 |
$gradient = $gradient ?? 'linear-gradient(135deg, #0040E3, #18D4FD)'; |
| 129 |
|
| 130 |
$colorCSS = 'gradient' === $colorType ? |
| 131 |
"color: transparent; background-image: $gradient; -webkit-background-clip: text; background-clip: text;" : |
| 132 |
"color: $color;"; |
| 133 |
|
| 134 |
$styles = (!$fontSize || !$isSize ? '' : 'font-size: '. $fontSize .'px;') . ($isColor ? $colorCSS : ''); |
| 135 |
|
| 136 |
return $styles; |
| 137 |
} |
| 138 |
|
| 139 |
static function getSeparatorCSS( $separator ) { |
| 140 |
extract( $separator ); |
| 141 |
$width = $width ?? '50%'; |
| 142 |
$height = $height ?? '2px'; |
| 143 |
$style = $style ?? 'solid'; |
| 144 |
$color = $color ?? '#85b4fa'; |
| 145 |
|
| 146 |
$styles = self::isValidCSS( 'width', $width ); |
| 147 |
if ( preg_replace('/[^\d]/', '', $height) !== '0' ) { |
| 148 |
$styles .= "border-top: $height $style $color;"; |
| 149 |
} |
| 150 |
|
| 151 |
return $styles; |
| 152 |
} |
| 153 |
|
| 154 |
static function getSpaceCSS( $space ) { |
| 155 |
extract( $space ); |
| 156 |
$side = $side ?? 2; |
| 157 |
$vertical = $vertical ?? '0px'; |
| 158 |
$horizontal = $horizontal ?? '0px'; |
| 159 |
$top = $top ?? '0px'; |
| 160 |
$right = $right ?? '0px'; |
| 161 |
$bottom = $bottom ?? '0px'; |
| 162 |
$left = $left ?? '0px'; |
| 163 |
|
| 164 |
$styles = ( 2 === $side ) ? "$vertical $horizontal" : "$top $right $bottom $left"; |
| 165 |
|
| 166 |
return $styles; |
| 167 |
} |
| 168 |
|
| 169 |
static function getShadowCSS( $shadow, $type = 'box' ) { |
| 170 |
extract( $shadow ); |
| 171 |
$hOffset = $hOffset ?? '0px'; |
| 172 |
$vOffset = $vOffset ?? '0px'; |
| 173 |
$blur = $blur ?? '0px'; |
| 174 |
$spreed = $spreed ?? '0px'; |
| 175 |
$color = $color ?? '#7090b0'; |
| 176 |
$isInset = $isInset ?? false; |
| 177 |
|
| 178 |
$inset = $isInset ? 'inset' : ''; |
| 179 |
$offsetBlur = "$hOffset $vOffset $blur"; |
| 180 |
|
| 181 |
$styles = 'text' === $type ? "$offsetBlur $color" : "$offsetBlur $spreed $color $inset"; |
| 182 |
|
| 183 |
return $styles ?: 'none'; |
| 184 |
} |
| 185 |
|
| 186 |
static function checkUnit( $size ) { |
| 187 |
$value = (string)$size; |
| 188 |
$units = ['px', 'em', 'rem', '%', 'vh', 'vw']; |
| 189 |
|
| 190 |
foreach ( $units as $unit ) { |
| 191 |
if ( substr( $value, -strlen( $unit ) ) === $unit ) { |
| 192 |
return $value; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if ( is_numeric( $size ) ) { |
| 197 |
return $value . 'px'; |
| 198 |
} |
| 199 |
|
| 200 |
return ''; |
| 201 |
} |
| 202 |
|
| 203 |
static function getTypoCSS( $selector, $typo, $isFamily = true ) { |
| 204 |
extract( $typo ); |
| 205 |
$fontFamily = $fontFamily ?? 'Default'; |
| 206 |
$fontCategory = $fontCategory ?? 'sans-serif'; |
| 207 |
$fontVariant = $fontVariant ?? 400; |
| 208 |
$fontWeight = $fontWeight ?? ''; |
| 209 |
$isUploadFont = $isUploadFont ?? true; |
| 210 |
$fontSize = $fontSize ?? [ 'desktop' => null, 'tablet' => null, 'mobile' => null ]; |
| 211 |
$fontStyle = $fontStyle ?? ''; |
| 212 |
$textTransform = $textTransform ?? ''; |
| 213 |
$textDecoration = $textDecoration ?? ''; |
| 214 |
$lineHeight = $lineHeight ?? ''; |
| 215 |
$letterSpace = $letterSpace ?? ''; |
| 216 |
|
| 217 |
$isEmptyFamily = !$isFamily || !$fontFamily || 'Default' === $fontFamily; |
| 218 |
$desktopFontSize = $fontSize['desktop'] ?? $fontSize; |
| 219 |
$tabletFontSize = $fontSize['tablet'] ?? $desktopFontSize; |
| 220 |
$mobileFontSize = $fontSize['mobile'] ?? $tabletFontSize; |
| 221 |
|
| 222 |
$tabBreakpoint = '@media only screen and (max-width: 1024px)'; |
| 223 |
$mobileBreakpoint = '@media only screen and (max-width: 640px)'; |
| 224 |
|
| 225 |
$styles = |
| 226 |
($isEmptyFamily ? '' : "font-family: '$fontFamily', $fontCategory;") . |
| 227 |
self::isValidCSS('font-weight', $fontWeight) . |
| 228 |
self::isValidCSS('font-size', self::checkUnit($desktopFontSize)) . |
| 229 |
self::isValidCSS('font-style', $fontStyle) . |
| 230 |
self::isValidCSS('text-transform', $textTransform) . |
| 231 |
self::isValidCSS('text-decoration', $textDecoration) . |
| 232 |
self::isValidCSS('line-height', $lineHeight) . |
| 233 |
self::isValidCSS('letter-spacing', $letterSpace); |
| 234 |
|
| 235 |
// Google font link |
| 236 |
if (!$fontVariant || $fontVariant === 400) { |
| 237 |
$linkQuery = ''; |
| 238 |
} elseif ($fontVariant === '400i') { |
| 239 |
$linkQuery = ':ital@1'; |
| 240 |
} elseif (strpos($fontVariant, '00i') !== false) { |
| 241 |
$linkQuery = ':ital,wght@1,' . str_replace('00i', '00', $fontVariant); |
| 242 |
} else { |
| 243 |
$linkQuery = ":wght@$fontVariant"; |
| 244 |
} |
| 245 |
|
| 246 |
$link = $isEmptyFamily ? '' : 'https://fonts.googleapis.com/css2?family=' . str_replace(' ', '+', $fontFamily) . $linkQuery . '&display=swap'; |
| 247 |
|
| 248 |
return [ |
| 249 |
'googleFontLink' => (!$isUploadFont || $isEmptyFamily) ? '' : "@import url($link);", |
| 250 |
'styles' => preg_replace('/\s+/', ' ', trim(" |
| 251 |
$selector { $styles } |
| 252 |
$tabBreakpoint { |
| 253 |
$selector { " . self::isValidCSS('font-size', self::checkUnit($tabletFontSize)) . " } |
| 254 |
} |
| 255 |
$mobileBreakpoint { |
| 256 |
$selector { " . self::isValidCSS('font-size', self::checkUnit($mobileFontSize)) . " } |
| 257 |
} |
| 258 |
")) |
| 259 |
]; |
| 260 |
} |
| 261 |
} |