| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\FB\Styling; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
use FPFramework\Helpers\Fields\DimensionsHelper; |
| 21 |
use FPFramework\Helpers\CSS as CSSHelper; |
| 22 |
|
| 23 |
class CSS |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Breakpoints. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $breakpoints = [ |
| 31 |
'desktop', |
| 32 |
'tablet', |
| 33 |
'mobile' |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* CSS per breakpoint. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private $responsive_css = [ |
| 42 |
'desktop' => [], |
| 43 |
'tablet' => [], |
| 44 |
'mobile' => [] |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* The box we are generating CSS for. |
| 49 |
* |
| 50 |
* @var Object |
| 51 |
*/ |
| 52 |
private $box; |
| 53 |
|
| 54 |
public function __construct($box) |
| 55 |
{ |
| 56 |
$this->box = $box; |
| 57 |
|
| 58 |
$this->setInstanceCSS(); |
| 59 |
$this->setDialogCSS(); |
| 60 |
$this->setCloseButtonCSS(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns all popup CSS. |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public function getCSS() |
| 69 |
{ |
| 70 |
$css = ''; |
| 71 |
|
| 72 |
foreach ($this->breakpoints as $breakpoint) |
| 73 |
{ |
| 74 |
$finalcss = $this->getBreakpointCSS($breakpoint); |
| 75 |
|
| 76 |
// Update $responsive_css with final CSS |
| 77 |
$this->responsive_css[$breakpoint] = $finalcss; |
| 78 |
|
| 79 |
$css .= $finalcss; |
| 80 |
} |
| 81 |
|
| 82 |
return $css; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Sets the instance CSS. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
private function setInstanceCSS() |
| 91 |
{ |
| 92 |
$animation_duration = $this->box->params->get('duration') ? (float) $this->box->params->get('duration') : 0; |
| 93 |
|
| 94 |
$style = [ |
| 95 |
'animation-duration' => (float) $animation_duration . 's' |
| 96 |
]; |
| 97 |
|
| 98 |
// Z-index |
| 99 |
$zindex = $this->box->params->get('zindex', 99999) != 99999 ? $this->box->params->get('zindex') : null; |
| 100 |
if (intval($zindex)) |
| 101 |
{ |
| 102 |
$style['z-index'] = $zindex; |
| 103 |
} |
| 104 |
|
| 105 |
// Overlay blur |
| 106 |
$overlay_enabled = (bool) $this->box->params->get('overlay'); |
| 107 |
if ($overlay_enabled && $radius = $this->box->params->get('overlayblurradius', 0)) |
| 108 |
{ |
| 109 |
$radius = intval($radius) * 0.25; |
| 110 |
// Limit blur |
| 111 |
if ($radius > 60) |
| 112 |
{ |
| 113 |
$radius = 60; |
| 114 |
} |
| 115 |
|
| 116 |
$style['backdrop-filter'] = 'blur(' . $radius . 'px)'; |
| 117 |
} |
| 118 |
|
| 119 |
// Add CSS (non-responsive) to desktop breakpoint |
| 120 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', '.fb-inst'); |
| 121 |
|
| 122 |
$css_properties = [ |
| 123 |
'margin' => [ |
| 124 |
'prefix' => 'padding' |
| 125 |
], |
| 126 |
]; |
| 127 |
$this->parseCSSProperties($css_properties, '.fb-inst'); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Sets the dialog CSS. |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
private function setDialogCSS() |
| 136 |
{ |
| 137 |
// Dialog CSS |
| 138 |
$style = [ |
| 139 |
'color' => $this->box->params->get('textcolor'), |
| 140 |
]; |
| 141 |
|
| 142 |
// Border |
| 143 |
$border = is_string($this->box->params->get('bordertype', 'none')) ? trim($this->box->params->get('bordertype', 'none')) : 'none'; |
| 144 |
$border_width = $this->box->params->get('borderwidth.value', 0) ? trim($this->box->params->get('borderwidth.value', 0)) : 0; |
| 145 |
$border_unit = is_string($this->box->params->get('borderwidth.unit', 'px')) ? trim($this->box->params->get('borderwidth.unit', 'px')) : ''; |
| 146 |
$border_color = is_string($this->box->params->get('bordercolor')) ? trim($this->box->params->get('bordercolor')) : ''; |
| 147 |
|
| 148 |
if (!($border === 'none' || empty($border_width) || empty($border_unit) || empty($border_color))) |
| 149 |
{ |
| 150 |
$style['border'] = $border . ' ' . $border_width . $border_unit . ' ' . $border_color; |
| 151 |
} |
| 152 |
|
| 153 |
// Background |
| 154 |
$background = $this->box->params->get('backgroundcolor'); |
| 155 |
if (!empty($background)) |
| 156 |
{ |
| 157 |
$style['background'] = $background; |
| 158 |
} |
| 159 |
|
| 160 |
// Background Image |
| 161 |
if ($this->box->params->get('bgimage', false)) |
| 162 |
{ |
| 163 |
$bgrepeat = is_string($this->box->params->get('bgrepeat')) ? $this->box->params->get('bgrepeat') : ''; |
| 164 |
$bgsize = is_string($this->box->params->get('bgsize')) ? $this->box->params->get('bgsize') : ''; |
| 165 |
$bgposition = is_string($this->box->params->get('bgposition')) ? $this->box->params->get('bgposition') : ''; |
| 166 |
|
| 167 |
$style['background-image'] = 'url(\'' . esc_url($this->box->params->get('bgimagefile', '')) . '\')'; |
| 168 |
$style['background-repeat'] = strtolower($bgrepeat); |
| 169 |
$style['background-size'] = strtolower($bgsize); |
| 170 |
$style['background-position'] = strtolower($bgposition); |
| 171 |
} |
| 172 |
|
| 173 |
// Add CSS (non-responsive) to desktop breakpoint |
| 174 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', ' .fb-dialog'); |
| 175 |
|
| 176 |
/** |
| 177 |
* Calculate Responsive CSS |
| 178 |
* and add them to each breakpoint. |
| 179 |
*/ |
| 180 |
$css_properties = [ |
| 181 |
'fontsize' => [ |
| 182 |
'prefix' => 'font-size' |
| 183 |
], |
| 184 |
'width' => [], |
| 185 |
'height' => [], |
| 186 |
'padding' => [], |
| 187 |
'borderradius' => [ |
| 188 |
'prefix' => 'border-radius', |
| 189 |
'parser' => 'parseDimensionsBorderRadiusData' |
| 190 |
] |
| 191 |
]; |
| 192 |
$this->parseCSSProperties($css_properties, ' .fb-dialog'); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Set close button CSS. |
| 197 |
* |
| 198 |
* @return void |
| 199 |
*/ |
| 200 |
private function setCloseButtonCSS() |
| 201 |
{ |
| 202 |
// Delay the display of the close button using CSS animation |
| 203 |
$delay = (float) $this->box->params->get('closebutton.delay', 0); |
| 204 |
if ($delay > 0) |
| 205 |
{ |
| 206 |
$style = [ |
| 207 |
'visibility' => 'hidden' |
| 208 |
]; |
| 209 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', ' .fb-close'); |
| 210 |
|
| 211 |
$style = [ |
| 212 |
'animation' => $delay . 's ebFadeIn', |
| 213 |
'animation-fill-mode' => 'forwards' |
| 214 |
]; |
| 215 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', '.fb-visible .fb-close'); |
| 216 |
} |
| 217 |
|
| 218 |
// Close Button Initial color |
| 219 |
$btnSource = is_string($this->box->params->get('closebutton.source', 'icon')) ? $this->box->params->get('closebutton.source', 'icon') : 'icon'; |
| 220 |
$color = is_string($this->box->params->get('closebutton.color', null)) ? $this->box->params->get('closebutton.color', null) : null; |
| 221 |
if ($btnSource == 'icon' && !empty($color)) |
| 222 |
{ |
| 223 |
$style = [ |
| 224 |
'color' => $color |
| 225 |
]; |
| 226 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', ' .fb-close'); |
| 227 |
} |
| 228 |
|
| 229 |
// Add the hover color |
| 230 |
if ($hoverColor = $this->box->params->get('closebutton.hover', null)) |
| 231 |
{ |
| 232 |
$style = [ |
| 233 |
'color' => esc_attr($hoverColor) . ' !important' |
| 234 |
]; |
| 235 |
$this->addCSS(CSSHelper::arrayToCSS($style), 'desktop', ' .fb-close:hover'); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Parses the given CSS properties and adds the CSS to the list. |
| 241 |
* |
| 242 |
* @param array $props |
| 243 |
* @param string $selector |
| 244 |
* |
| 245 |
* @return void |
| 246 |
*/ |
| 247 |
private function parseCSSProperties($props = [], $selector = '') |
| 248 |
{ |
| 249 |
foreach ($props as $prop => $prop_data) |
| 250 |
{ |
| 251 |
$value = (array) $this->box->params->get($prop . '_control.' . $prop, []); |
| 252 |
|
| 253 |
foreach ($this->breakpoints as $breakpoint) |
| 254 |
{ |
| 255 |
if (!isset($value[$breakpoint])) |
| 256 |
{ |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
$prefix = isset($prop_data['prefix']) ? $prop_data['prefix'] : $prop; |
| 261 |
|
| 262 |
$parserMethod = isset($prop_data['parser']) ? $prop_data['parser'] : 'parseDimensionsData'; |
| 263 |
|
| 264 |
$_value = DimensionsHelper::$parserMethod($value[$breakpoint], $prefix); |
| 265 |
|
| 266 |
if (empty($_value)) |
| 267 |
{ |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
$breakpoint_value = CSSHelper::arrayToCSS($_value); |
| 272 |
|
| 273 |
$this->addCSS($breakpoint_value, $breakpoint, $selector); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Add CSS to given breakpoint and selector. |
| 280 |
* |
| 281 |
* @param string $css |
| 282 |
* @param string $breakpoint |
| 283 |
* @param string $selector |
| 284 |
* |
| 285 |
* @return void |
| 286 |
*/ |
| 287 |
public function addCSS($css = '', $breakpoint = 'desktop', $selector = '') |
| 288 |
{ |
| 289 |
if (empty($css)) |
| 290 |
{ |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
// No selector, append to breakpoint as new item |
| 295 |
if (empty($selector)) |
| 296 |
{ |
| 297 |
$this->responsive_css[$breakpoint][] = $css; |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
// With selector, if first breakpoint item, set it |
| 302 |
if (empty($this->responsive_css[$breakpoint][$selector])) |
| 303 |
{ |
| 304 |
$this->responsive_css[$breakpoint][$selector] = $css; |
| 305 |
} |
| 306 |
// Otherwise, append to existing CSS |
| 307 |
else |
| 308 |
{ |
| 309 |
$this->responsive_css[$breakpoint][$selector] .= ' ' . $css; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Returns the CSS for a given breakpoint. |
| 315 |
* |
| 316 |
* @param string $breakpoint |
| 317 |
* |
| 318 |
* @return string |
| 319 |
*/ |
| 320 |
private function getBreakpointCSS($breakpoint = 'desktop') |
| 321 |
{ |
| 322 |
if (!isset($this->responsive_css[$breakpoint])) |
| 323 |
{ |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
$skeletonFunctionName = 'get' . ucfirst($breakpoint) . 'Skeleton'; |
| 328 |
if (!method_exists($this, $skeletonFunctionName)) |
| 329 |
{ |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
$skeleton = $this->$skeletonFunctionName(); |
| 334 |
|
| 335 |
/** |
| 336 |
* This array holds all CSS grouped by selector. |
| 337 |
* |
| 338 |
* The empty key,value pair indicates that the CSS does not have a custom selector |
| 339 |
*/ |
| 340 |
$data = [ |
| 341 |
// Global CSS |
| 342 |
'' => '' |
| 343 |
]; |
| 344 |
|
| 345 |
// Group CSS by selector |
| 346 |
foreach ($this->responsive_css[$breakpoint] as $selector => $value) |
| 347 |
{ |
| 348 |
$selector = is_string($selector) && $selector !== '' ? $selector : ''; |
| 349 |
|
| 350 |
if (empty($selector)) |
| 351 |
{ |
| 352 |
$data[''] .= $value; |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
$data[$selector] = $value; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Get final CSS for the breakpoint |
| 361 |
$cssvalue = ''; |
| 362 |
foreach ($data as $selector => $value) |
| 363 |
{ |
| 364 |
if (empty($value)) |
| 365 |
{ |
| 366 |
continue; |
| 367 |
} |
| 368 |
|
| 369 |
$selectorSkeleton = $this->getSelector($selector); |
| 370 |
|
| 371 |
$cssvalue .= sprintf($selectorSkeleton, $value); |
| 372 |
} |
| 373 |
|
| 374 |
// Return breakpoint with final CSS |
| 375 |
return sprintf($skeleton, $cssvalue); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Returns the CSS Selector. |
| 380 |
* |
| 381 |
* @return string |
| 382 |
*/ |
| 383 |
private function getSelector($selector = ' .fb-dialog') |
| 384 |
{ |
| 385 |
return '.fb-' . esc_attr($this->box->ID . $selector) . ' { %s }'; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Desktop CSS Skeleton. |
| 390 |
* |
| 391 |
* @return string |
| 392 |
*/ |
| 393 |
private function getDesktopSkeleton() |
| 394 |
{ |
| 395 |
return '%s'; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Tablet CSS Skeleton. |
| 400 |
* |
| 401 |
* @return string |
| 402 |
*/ |
| 403 |
private function getTabletSkeleton() |
| 404 |
{ |
| 405 |
return '@media only screen and (max-width: 991px) { %s }'; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Mobile CSS Skeleton. |
| 410 |
* |
| 411 |
* @return string |
| 412 |
*/ |
| 413 |
private function getMobileSkeleton() |
| 414 |
{ |
| 415 |
return '@media only screen and (max-width: 575px) { %s }'; |
| 416 |
} |
| 417 |
|
| 418 |
public function getResponsiveCSS() |
| 419 |
{ |
| 420 |
return $this->responsive_css; |
| 421 |
} |
| 422 |
} |