| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.10 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2022 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\FB; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FireBox\Core\Helpers\BoxHelper; |
| 20 |
use FPFramework\Libs\Registry; |
| 21 |
use FPFramework\Helpers\Fields\DimensionsHelper; |
| 22 |
|
| 23 |
class Box |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Send useful JS snippet once in first box |
| 27 |
* |
| 28 |
* @var boolean |
| 29 |
*/ |
| 30 |
static $loadedLocalizedScript = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* The box settings |
| 34 |
*/ |
| 35 |
private $boxSettings; |
| 36 |
|
| 37 |
/** |
| 38 |
* The factory |
| 39 |
* |
| 40 |
* @var Factory |
| 41 |
*/ |
| 42 |
protected $factory; |
| 43 |
|
| 44 |
public function __construct($boxSettings = null, $factory = null) |
| 45 |
{ |
| 46 |
$this->init($boxSettings, $factory); |
| 47 |
} |
| 48 |
|
| 49 |
public function init($boxSettings = null, $factory = null) |
| 50 |
{ |
| 51 |
if (!$factory) |
| 52 |
{ |
| 53 |
$factory = new \FPFramework\Base\Factory(); |
| 54 |
} |
| 55 |
|
| 56 |
$this->boxSettings = $boxSettings; |
| 57 |
$this->factory = $factory; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Gets published box. |
| 62 |
* |
| 63 |
* @param integer $id |
| 64 |
* @param string $status |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function get($id, $status = null) |
| 69 |
{ |
| 70 |
$payload = [ |
| 71 |
'where' => [ |
| 72 |
'ID' => ' = ' . esc_sql($id), |
| 73 |
'post_type' => " = 'firebox'" |
| 74 |
] |
| 75 |
]; |
| 76 |
|
| 77 |
// apply status if given |
| 78 |
if ($status) |
| 79 |
{ |
| 80 |
$payload['where']['post_status'] = ' = \'' . esc_sql($status) . '\''; |
| 81 |
} |
| 82 |
|
| 83 |
if (!$box = firebox()->tables->box->getResults($payload)) |
| 84 |
{ |
| 85 |
return []; |
| 86 |
} |
| 87 |
|
| 88 |
if (!isset($box[0])) |
| 89 |
{ |
| 90 |
return []; |
| 91 |
} |
| 92 |
|
| 93 |
$box = $box[0]; |
| 94 |
|
| 95 |
// get meta options for box |
| 96 |
$meta = self::getMeta($id); |
| 97 |
$box->params = new Registry($meta); |
| 98 |
|
| 99 |
return $box; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get box meta |
| 104 |
* |
| 105 |
* @param int $id |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public static function getMeta($id) |
| 110 |
{ |
| 111 |
return get_post_meta($id, 'fpframework_meta_settings', true); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render box |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
public function render($box = null) |
| 120 |
{ |
| 121 |
// we do not have a passed box or box settings from constructor, abort |
| 122 |
// as we cannot render a box without any box parameters |
| 123 |
if (!$box && !$this->boxSettings) |
| 124 |
{ |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// if a box was given to the function, use these settings instead |
| 129 |
if ($box) |
| 130 |
{ |
| 131 |
$this->boxSettings = $box; |
| 132 |
} |
| 133 |
|
| 134 |
// if we have box settings, then set up our box with these settings |
| 135 |
if ($this->boxSettings) |
| 136 |
{ |
| 137 |
$box = $this->boxSettings; |
| 138 |
} |
| 139 |
|
| 140 |
// Check Publishing Assignments |
| 141 |
if (!$this->pass($box)) |
| 142 |
{ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
// get settings params |
| 147 |
$params = BoxHelper::getParams(); |
| 148 |
|
| 149 |
if (!did_action('wp_enqueue_scripts')) |
| 150 |
{ |
| 151 |
/** |
| 152 |
* Loads all media files. |
| 153 |
*/ |
| 154 |
add_action('wp_enqueue_scripts', function() use ($box, $params) { |
| 155 |
// load box media |
| 156 |
$this->loadBoxMedia($box, $params); |
| 157 |
}); |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
// load box media |
| 162 |
$this->loadBoxMedia($box, $params); |
| 163 |
} |
| 164 |
|
| 165 |
self::prepare($box); |
| 166 |
|
| 167 |
if (!did_action('wp_head')) |
| 168 |
{ |
| 169 |
/** |
| 170 |
* Custom CSS |
| 171 |
*/ |
| 172 |
add_action('wp_head', function() use ($box) { |
| 173 |
if ($css = $this->getCustomCSS($box)) |
| 174 |
{ |
| 175 |
echo '<style type="text/css" class="fb-' . esc_attr($box->ID) . '-style fb-responsive-style">' . $css . '</style>'; |
| 176 |
} |
| 177 |
}); |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
if ($css = $this->getCustomCSS($box)) |
| 182 |
{ |
| 183 |
echo '<style type="text/css" class="fb-' . esc_attr($box->ID) . '-style fb-responsive-style">' . $css . '</style>'; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Runs before rendering the box. |
| 189 |
*/ |
| 190 |
do_action('firebox/box/before_render', $box); |
| 191 |
|
| 192 |
// after we prepare it, update box settings |
| 193 |
$this->boxSettings = $box; |
| 194 |
|
| 195 |
// payload |
| 196 |
$payload = [ |
| 197 |
'box' => $box, |
| 198 |
'params' => $params, |
| 199 |
]; |
| 200 |
|
| 201 |
// return box template |
| 202 |
add_action('wp_footer', function() use ($box, $payload) { |
| 203 |
echo firebox()->renderer->public->render('box', $payload, true); |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Gets the Custom CSS of the popup. |
| 209 |
* |
| 210 |
* @param object $box |
| 211 |
* |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
private function getCustomCSS($box) |
| 215 |
{ |
| 216 |
$css = is_string($box->params->get('customcss')) ? $box->params->get('customcss') : ''; |
| 217 |
|
| 218 |
// add responsive CSS |
| 219 |
if ($responsive_css = (array) $box->params->get('responsive_css', [])) |
| 220 |
{ |
| 221 |
if (isset($responsive_css['tablet']) && !empty($responsive_css['tablet'])) |
| 222 |
{ |
| 223 |
$css .= ' |
| 224 |
@media only screen and (max-width: 768px) { |
| 225 |
.fb-' . esc_attr($box->ID) . ' .fb-dialog { ' . esc_html($responsive_css['tablet']) . ' } |
| 226 |
}'; |
| 227 |
} |
| 228 |
if (isset($responsive_css['mobile']) && !empty($responsive_css['mobile'])) |
| 229 |
{ |
| 230 |
$css .= ' |
| 231 |
@media only screen and (max-width: 468px) { |
| 232 |
.fb-' . esc_attr($box->ID) . ' .fb-dialog { ' . esc_html($responsive_css['mobile']) . ' } |
| 233 |
}'; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return $css; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Send a helpful object to JavaScript files |
| 242 |
* |
| 243 |
* @return void |
| 244 |
*/ |
| 245 |
public static function setJSObject() |
| 246 |
{ |
| 247 |
if (self::$loadedLocalizedScript) |
| 248 |
{ |
| 249 |
return; |
| 250 |
} |
| 251 |
self::$loadedLocalizedScript = true; |
| 252 |
|
| 253 |
$data = array( |
| 254 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 255 |
'nonce' => wp_create_nonce('fbox_js_nonce'), |
| 256 |
'site_url' => site_url('/'), |
| 257 |
'referrer' => wp_get_referer() |
| 258 |
); |
| 259 |
|
| 260 |
wp_localize_script('firebox', 'fbox_js_object', $data); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Load Box Media |
| 265 |
* |
| 266 |
* @param array $box |
| 267 |
* @param array $params |
| 268 |
* |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public function loadBoxMedia($box, $params) |
| 272 |
{ |
| 273 |
$box = new Registry($box); |
| 274 |
$params = new Registry($params); |
| 275 |
|
| 276 |
/** |
| 277 |
* We require a CSS handle to register inline styles. |
| 278 |
* |
| 279 |
* For this case, we use this dummy style to add inline CSS for our popups |
| 280 |
* even though the FireBox CSS file may not appear in the page. |
| 281 |
*/ |
| 282 |
wp_register_style('firebox-custom-styles', false); |
| 283 |
wp_enqueue_style('firebox-custom-styles'); |
| 284 |
|
| 285 |
// Add polyfills for Internet Explorer |
| 286 |
$browser = $this->factory->getBrowser(); |
| 287 |
if ($browser && is_array($browser) && array_key_exists('name', $browser) && $browser['name'] == 'ie') |
| 288 |
{ |
| 289 |
wp_enqueue_script( |
| 290 |
'firebox-ie11-polyfill', |
| 291 |
'https://polyfill.io/v3/polyfill.min.js?features=NodeList.prototype.forEach%2CElement.prototype.closest%2CArray.prototype.forEach%2CArray.prototype.find%2CIntersectionObserver%2CIntersectionObserverEntry', |
| 292 |
[], |
| 293 |
FBOX_VERSION, |
| 294 |
false |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Velocity |
| 300 |
*/ |
| 301 |
if ($params->get('loadVelocity', true)) |
| 302 |
{ |
| 303 |
wp_enqueue_script( |
| 304 |
'firebox-velocity', |
| 305 |
FBOX_MEDIA_PUBLIC_URL . 'js/vendor/velocity.js', |
| 306 |
[], |
| 307 |
FBOX_VERSION, |
| 308 |
false |
| 309 |
); |
| 310 |
wp_enqueue_script( |
| 311 |
'firebox-velocity-ui', |
| 312 |
FBOX_MEDIA_PUBLIC_URL . 'js/vendor/velocity.ui.js', |
| 313 |
[], |
| 314 |
FBOX_VERSION, |
| 315 |
false |
| 316 |
); |
| 317 |
|
| 318 |
/** |
| 319 |
* Animations |
| 320 |
*/ |
| 321 |
if (strpos($box->get('params.data.animationin'), 'firebox') !== false || strpos($box->get('params.data.animationout'), 'firebox') !== false) |
| 322 |
{ |
| 323 |
wp_enqueue_script( |
| 324 |
'firebox-animations', |
| 325 |
FBOX_MEDIA_PUBLIC_URL . 'js/animations.js', |
| 326 |
[], |
| 327 |
FBOX_VERSION, |
| 328 |
false |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* FireBox JS |
| 335 |
*/ |
| 336 |
wp_enqueue_script( |
| 337 |
'firebox', |
| 338 |
FBOX_MEDIA_PUBLIC_URL . 'js/firebox.js', |
| 339 |
[], |
| 340 |
FBOX_VERSION, |
| 341 |
false |
| 342 |
); |
| 343 |
|
| 344 |
// run above the main JS script to run only once |
| 345 |
self::setJSObject(); |
| 346 |
|
| 347 |
/** |
| 348 |
* FireBox CSS |
| 349 |
*/ |
| 350 |
if ($params->get('loadCSS', true)) |
| 351 |
{ |
| 352 |
wp_enqueue_style( |
| 353 |
'firebox', |
| 354 |
FBOX_MEDIA_PUBLIC_URL . 'css/firebox.css', |
| 355 |
[], |
| 356 |
FBOX_VERSION, |
| 357 |
false |
| 358 |
); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Page Slide mode JS |
| 363 |
*/ |
| 364 |
if ($box->get('params.data.mode') == 'pageslide') |
| 365 |
{ |
| 366 |
wp_enqueue_script( |
| 367 |
'firebox-pageslide-mode', |
| 368 |
FBOX_MEDIA_PUBLIC_URL . 'js/pageslide_mode.js', |
| 369 |
[], |
| 370 |
FBOX_VERSION, |
| 371 |
false |
| 372 |
); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
$this->loadThemeCSSOverrides(); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Some themes require overrides to preserve as much as we can the styling of the popups. |
| 382 |
* |
| 383 |
* @return void |
| 384 |
*/ |
| 385 |
private function loadThemeCSSOverrides() |
| 386 |
{ |
| 387 |
$active_theme = wp_get_theme(); |
| 388 |
$theme = $active_theme->template; |
| 389 |
|
| 390 |
/** |
| 391 |
* This is the listed of the themes that we have created overrides |
| 392 |
*/ |
| 393 |
$themes = [ |
| 394 |
'twentytwentyone' |
| 395 |
]; |
| 396 |
|
| 397 |
if (!in_array($theme, $themes)) |
| 398 |
{ |
| 399 |
return; |
| 400 |
} |
| 401 |
|
| 402 |
wp_enqueue_style( |
| 403 |
'firebox-theme-' . $theme . '-override', |
| 404 |
FBOX_MEDIA_PUBLIC_URL . 'css/themes/' . $theme . '.css', |
| 405 |
[], |
| 406 |
FBOX_VERSION, |
| 407 |
false |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Prepares the box before rendering |
| 413 |
* |
| 414 |
* @param array $box |
| 415 |
* |
| 416 |
* @return void |
| 417 |
*/ |
| 418 |
public static function prepare(&$box) |
| 419 |
{ |
| 420 |
$cParam = BoxHelper::getParams(); |
| 421 |
$cParam = new Registry($cParam); |
| 422 |
|
| 423 |
$box->post_content = apply_filters('the_content', $box->post_content); |
| 424 |
|
| 425 |
$css_class_prefix = 'fb-'; |
| 426 |
|
| 427 |
$position = $box->params->get('position', ''); |
| 428 |
$position = !is_string($position) ? '' : $position; |
| 429 |
|
| 430 |
/* Classes */ |
| 431 |
$css_class = [ |
| 432 |
$box->ID, |
| 433 |
$position |
| 434 |
]; |
| 435 |
|
| 436 |
$rtl = $box->params->get('rtl', '0'); |
| 437 |
if ($rtl == '1') |
| 438 |
{ |
| 439 |
$css_class[] = 'rtl'; |
| 440 |
} |
| 441 |
|
| 442 |
self::prefixCSSClasses($css_class); |
| 443 |
|
| 444 |
// class suffix |
| 445 |
$classSuffix = $box->params->get('classsuffix', ''); |
| 446 |
$classSuffix = is_string($classSuffix) ? $classSuffix : ''; |
| 447 |
|
| 448 |
$css_class[] = $classSuffix; |
| 449 |
|
| 450 |
$box->classes = $css_class; |
| 451 |
|
| 452 |
// box shadow |
| 453 |
$boxshadow = (is_string($box->params->get('boxshadow', '1')) || is_int($box->params->get('boxshadow', '1'))) ? $box->params->get('boxshadow', '1') : '0'; |
| 454 |
|
| 455 |
$dialog_css_classes = [ |
| 456 |
$boxshadow != '0' ? 'shd' . $boxshadow : null |
| 457 |
]; |
| 458 |
|
| 459 |
// Align Content |
| 460 |
$aligncontent = is_string($box->params->get('aligncontent')) ? explode(' ', $box->params->get('aligncontent')) : []; |
| 461 |
$dialog_css_classes = array_merge($dialog_css_classes, $aligncontent); |
| 462 |
|
| 463 |
self::prefixCSSClasses($dialog_css_classes); |
| 464 |
$box->dialog_classes = $dialog_css_classes; |
| 465 |
|
| 466 |
/* CSS */ |
| 467 |
// border |
| 468 |
$border = is_string($box->params->get('bordertype', 'none')) ? $box->params->get('bordertype', 'none') : 'none'; |
| 469 |
$border_width = $box->params->get('borderwidth.value', 0) ? $box->params->get('borderwidth.value', 0) : 0; |
| 470 |
$border_unit = is_string($box->params->get('borderwidth.unit', 'px')) ? $box->params->get('borderwidth.unit', 'px') : 0; |
| 471 |
$border_color = is_string($box->params->get('bordercolor')) ? $box->params->get('bordercolor') : ''; |
| 472 |
|
| 473 |
$style = [ |
| 474 |
'background' => $box->params->get('backgroundcolor'), |
| 475 |
'color' => $box->params->get('textcolor'), |
| 476 |
'border' => $border == 'none' ? null : $border . ' ' . $border_width . $border_unit . ' ' . $border_color, |
| 477 |
]; |
| 478 |
|
| 479 |
// add shared properties |
| 480 |
$shared_properties = [ |
| 481 |
'width' => [ |
| 482 |
'prefix_only' => true |
| 483 |
], |
| 484 |
'height' => [ |
| 485 |
'prefix_only' => true |
| 486 |
], |
| 487 |
'padding' => [], |
| 488 |
'margin' => [] |
| 489 |
]; |
| 490 |
foreach ($shared_properties as $prop => $prop_data) |
| 491 |
{ |
| 492 |
// add dekstop property |
| 493 |
$value = (array) $box->params->get($prop . '_control.' . $prop, []); |
| 494 |
if (isset($value['desktop'])) |
| 495 |
{ |
| 496 |
$prefix_only = isset($prop_data['prefix_only']) ? (bool) $prop_data['prefix_only'] : false; |
| 497 |
|
| 498 |
$desktop_value = DimensionsHelper::parseDimensionsData($value['desktop'], $prop, false, $prefix_only); |
| 499 |
$style = array_merge($style, $desktop_value); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
// add dekstop border_radius |
| 504 |
$border_radius = (array) $box->params->get('borderradius_control.borderradius', []); |
| 505 |
if (isset($border_radius['desktop'])) |
| 506 |
{ |
| 507 |
$desktop_border_radius = DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['desktop']); |
| 508 |
$style = array_merge($style, $desktop_border_radius); |
| 509 |
} |
| 510 |
|
| 511 |
// Background Image |
| 512 |
if ($box->params->get('bgimage', false)) |
| 513 |
{ |
| 514 |
$bgrepeat = is_string($box->params->get('bgrepeat')) ? $box->params->get('bgrepeat') : ''; |
| 515 |
$bgsize = is_string($box->params->get('bgsize')) ? $box->params->get('bgsize') : ''; |
| 516 |
$bgposition = is_string($box->params->get('bgposition')) ? $box->params->get('bgposition') : ''; |
| 517 |
|
| 518 |
$style['background-image'] = 'url(\'' . esc_url($box->params->get('bgimagefile')) . '\')'; |
| 519 |
$style['background-repeat'] = strtolower($bgrepeat); |
| 520 |
$style['background-size'] = strtolower($bgsize); |
| 521 |
$style['background-position'] = strtolower($bgposition); |
| 522 |
} |
| 523 |
|
| 524 |
$box->style = BoxHelper::arrayToCSSS($style); |
| 525 |
|
| 526 |
$trigger_point_methods = [ |
| 527 |
'pageheight' => 'onScrollDepth', |
| 528 |
'element' => 'onElementVisibility', |
| 529 |
'pageready' => 'onPageReady', |
| 530 |
'pageload' => 'onPageLoad', |
| 531 |
'userleave' => 'onExit', |
| 532 |
'onclick' => 'onClick', |
| 533 |
'elementHover' => 'onHover', |
| 534 |
'ondemand' => 'onDemand' |
| 535 |
]; |
| 536 |
|
| 537 |
/* Other Settings */ |
| 538 |
$scroll_depth = $box->params->get('scroll_depth', 'percentage'); |
| 539 |
$scroll_depth = is_string($scroll_depth) ? $scroll_depth : ''; |
| 540 |
|
| 541 |
$animation_duration = $box->params->get('duration') ? (float) $box->params->get('duration') : 0; |
| 542 |
|
| 543 |
// Use Namespaced classes for each trigger point and let them manipulate the settings dynamicaly. |
| 544 |
$box->settings = [ |
| 545 |
'trigger' => (is_string($box->params->get('triggermethod'))) && array_key_exists($box->params->get('triggermethod'), $trigger_point_methods) ? $trigger_point_methods[$box->params->get('triggermethod')] : $box->params->get('triggermethod'), |
| 546 |
'trigger_selector' => $box->params->get('triggerelement'), |
| 547 |
'delay' => (int) $box->params->get('triggerdelay') * 1000, |
| 548 |
'scroll_depth' => $scroll_depth, |
| 549 |
'scroll_depth_value' => $scroll_depth == 'percentage' ? (int) $box->params->get('triggerpercentage') : (int) $box->params->get('scroll_pixel'), |
| 550 |
'firing_frequency' => (int) $box->params->get('firing_frequency', 1), |
| 551 |
'reverse_scroll_close' => (bool) $box->params->get('autohide'), |
| 552 |
'threshold' => (float) $box->params->get('threshold', 0) / 100, |
| 553 |
'close_out_viewport' => (bool) $box->params->get('close_out_viewport', false), |
| 554 |
'exit_timer' => (int) $box->params->get('exittimer') * 1000, |
| 555 |
'idle_time' => (int) $box->params->get('idle_time') * 1000, |
| 556 |
'animation_open' => $box->params->get('animationin'), |
| 557 |
'animation_close' => $box->params->get('animationout'), |
| 558 |
'animation_duration' => (float) $animation_duration * 1000, |
| 559 |
'prevent_default' => (bool) $box->params->get('preventdefault', true), |
| 560 |
'backdrop' => (bool) $box->params->get('overlay'), |
| 561 |
'backdrop_color' => $box->params->get('overlay_color'), |
| 562 |
'backdrop_click' => (bool) $box->params->get('overlayclick'), |
| 563 |
'disable_page_scroll' => (bool) $box->params->get('preventpagescroll'), |
| 564 |
'test_mode' => (bool) $box->params->get('testmode'), |
| 565 |
'debug' => (bool) $cParam->get('debug', false), |
| 566 |
'ga_tracking' => (bool) $cParam->get('gaTrack', 0), |
| 567 |
'ga_tracking_id' => $cParam->get('gaID', 0), |
| 568 |
'ga_tracking_label' => $cParam->get('gaCategory'), |
| 569 |
'auto_focus' => (bool) $box->params->get('autofocus', false) |
| 570 |
]; |
| 571 |
|
| 572 |
$box->styles_container = BoxHelper::arrayToCSSS([ |
| 573 |
'z-index' => $box->params->get('zindex', 99999) != 99999 ? $box->params->get('zindex') : null |
| 574 |
]); |
| 575 |
|
| 576 |
// Let's start using CSS vars for each box settings. |
| 577 |
$cssVars = [ |
| 578 |
'animation_duration' => (float) $animation_duration . 's' |
| 579 |
]; |
| 580 |
|
| 581 |
// Enqueue firebox custom inline CSS |
| 582 |
$cssVarsForm = self::cssVarsToString($cssVars, '.fb-' . $box->ID); |
| 583 |
if (!did_action('wp_enqueue_scripts')) |
| 584 |
{ |
| 585 |
add_action('wp_enqueue_scripts', function() use ($cssVarsForm) { |
| 586 |
wp_add_inline_style('firebox-custom-styles', $cssVarsForm); |
| 587 |
}); |
| 588 |
} |
| 589 |
else |
| 590 |
{ |
| 591 |
wp_add_inline_style('firebox-custom-styles', $cssVarsForm); |
| 592 |
} |
| 593 |
|
| 594 |
// set padding and margin for other devices as well |
| 595 |
self::setResponsiveCSS($box); |
| 596 |
|
| 597 |
self::replaceBoxSmartTags($box); |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Converts an array of CSS variables to its corresponding strings. |
| 602 |
* |
| 603 |
* @param array $cssVars |
| 604 |
* @param string $namespace |
| 605 |
* |
| 606 |
* @return string |
| 607 |
*/ |
| 608 |
private static function cssVarsToString($cssVars, $namespace) |
| 609 |
{ |
| 610 |
$output = ''; |
| 611 |
|
| 612 |
foreach ($cssVars as $key => $value) |
| 613 |
{ |
| 614 |
$output .= '--' . $key . ': ' . $value . ';' . "\n"; |
| 615 |
} |
| 616 |
|
| 617 |
return $namespace . ' { |
| 618 |
' . $output . ' |
| 619 |
} |
| 620 |
'; |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Sets all responsive CSS to use in box view |
| 625 |
* |
| 626 |
* @param object $box |
| 627 |
* |
| 628 |
* @return void |
| 629 |
*/ |
| 630 |
private static function setResponsiveCSS(&$box) |
| 631 |
{ |
| 632 |
$tablet = ''; |
| 633 |
$mobile = ''; |
| 634 |
|
| 635 |
/** |
| 636 |
* Handle width, height, padding, margin the same way. |
| 637 |
*/ |
| 638 |
$shared_properties = [ |
| 639 |
'width' => [ |
| 640 |
'prefix_only' => true |
| 641 |
], |
| 642 |
'height' => [ |
| 643 |
'prefix_only' => true |
| 644 |
], |
| 645 |
'padding' => [], |
| 646 |
'margin' => [] |
| 647 |
]; |
| 648 |
|
| 649 |
foreach ($shared_properties as $prop => $prop_settings) |
| 650 |
{ |
| 651 |
$prop_data = (array) $box->params->get($prop . '_control.' . $prop, []); |
| 652 |
|
| 653 |
$prefix_only = isset($prop_settings['prefix_only']) ? $prop_settings['prefix_only'] : false; |
| 654 |
|
| 655 |
// assign each device CSS |
| 656 |
if (isset($prop_data['tablet'])) |
| 657 |
{ |
| 658 |
$tablet .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsData($prop_data['tablet'], $prop, true, $prefix_only)); |
| 659 |
} |
| 660 |
if (isset($prop_data['mobile'])) |
| 661 |
{ |
| 662 |
$mobile .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsData($prop_data['mobile'], $prop, true, $prefix_only)); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
// border radius requires a special method |
| 667 |
$border_radius = (array) $box->params->get('borderradius_control.borderradius', []); |
| 668 |
if (isset($border_radius['tablet'])) |
| 669 |
{ |
| 670 |
$tablet .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['tablet'], true)); |
| 671 |
} |
| 672 |
if (isset($border_radius['mobile'])) |
| 673 |
{ |
| 674 |
$mobile .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['mobile'], true)); |
| 675 |
} |
| 676 |
|
| 677 |
$responsive_css = [ |
| 678 |
'tablet' => $tablet, |
| 679 |
'mobile' => $mobile |
| 680 |
]; |
| 681 |
$box->params->set('responsive_css', $responsive_css); |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* Replaces all box smart tags |
| 686 |
* |
| 687 |
* @param object $box |
| 688 |
* |
| 689 |
* @return object |
| 690 |
*/ |
| 691 |
public static function replaceBoxSmartTags(&$box) |
| 692 |
{ |
| 693 |
$tags = new \FPFramework\Base\SmartTags\SmartTags(); |
| 694 |
|
| 695 |
// register FB Smart Tags |
| 696 |
$tags->register('\FireBox\Core\SmartTags', FBOX_BASE_FOLDER . '/Inc/Core/SmartTags', $box); |
| 697 |
|
| 698 |
$box = $tags->replace($box); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Checks if a box passes assignments |
| 703 |
* |
| 704 |
* @param object $box |
| 705 |
* |
| 706 |
* @return boolean |
| 707 |
*/ |
| 708 |
public function pass($box) |
| 709 |
{ |
| 710 |
if (!$box || !is_object($box)) |
| 711 |
{ |
| 712 |
return false; |
| 713 |
} |
| 714 |
|
| 715 |
// set box settings if empty |
| 716 |
if (empty($this->boxSettings)) |
| 717 |
{ |
| 718 |
$this->boxSettings = $box; |
| 719 |
} |
| 720 |
|
| 721 |
// Prepare boxes that mirror other boxes assignments |
| 722 |
if ($box->params->get('mirror', false) && $mirror_box_id = $box->params->get('mirror_box')) |
| 723 |
{ |
| 724 |
$box->params->merge(self::getAssignmentsForMirroring($mirror_box_id)); |
| 725 |
} |
| 726 |
|
| 727 |
// Check first local assignments |
| 728 |
if (!$this->passLocalAssignments($box)) |
| 729 |
{ |
| 730 |
return false; |
| 731 |
} |
| 732 |
|
| 733 |
// If testmode is enabled disable the User Groups assignment |
| 734 |
if ($box->params->get('testmode')) |
| 735 |
{ |
| 736 |
$box->params->set('assignments.assign_grouplevel.selection', '0'); |
| 737 |
} |
| 738 |
|
| 739 |
$globalAssignments = $this->passGlobalAssignments($box); |
| 740 |
|
| 741 |
// Check framework based assignments |
| 742 |
return $globalAssignments; |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Passes framework based global assignments |
| 747 |
* |
| 748 |
* @param object $box |
| 749 |
* |
| 750 |
* @return boolean |
| 751 |
*/ |
| 752 |
private function passGlobalAssignments($box) |
| 753 |
{ |
| 754 |
$assignments = new \FPFramework\Base\Assignments($this->factory); |
| 755 |
$pass = $assignments->passAll($box, $box->params->get('assignmentMatchingMethod', 'and')); |
| 756 |
return $pass; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* Check if a box passes local assignments |
| 761 |
* |
| 762 |
* @param object $box |
| 763 |
* |
| 764 |
* @return boolean |
| 765 |
*/ |
| 766 |
private function passLocalAssignments($box) |
| 767 |
{ |
| 768 |
$localAssignments = new \FireBox\Core\FB\Assignments($this, $this->factory); |
| 769 |
return $localAssignments->passAll(); |
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* Gets assignments of mirrored box |
| 774 |
* |
| 775 |
* @param int $box_id |
| 776 |
* |
| 777 |
* @return object |
| 778 |
*/ |
| 779 |
private static function getAssignmentsForMirroring($box_id) |
| 780 |
{ |
| 781 |
$payload = [ |
| 782 |
'where' => [ |
| 783 |
'ID' => ' = ' . intval($box_id), |
| 784 |
'post_status' => " = 'publish'", |
| 785 |
'post_type' => " = 'firebox'" |
| 786 |
] |
| 787 |
]; |
| 788 |
|
| 789 |
// Load box |
| 790 |
if (!$box = firebox()->tables->box->getResults($payload)) |
| 791 |
{ |
| 792 |
return; |
| 793 |
} |
| 794 |
|
| 795 |
$box = $box[0]; |
| 796 |
|
| 797 |
// get meta options for box |
| 798 |
$meta = get_post_meta($box_id, 'fpframework_meta_settings', true); |
| 799 |
$box->params = $meta; |
| 800 |
|
| 801 |
// To prevent user frustration, we ignore the following assignments because they are not displayed in the Publishing Assignments. |
| 802 |
$params_to_ignore = [ |
| 803 |
'assign_impressions' |
| 804 |
]; |
| 805 |
|
| 806 |
$assignments = []; |
| 807 |
|
| 808 |
// Gather params to merge |
| 809 |
foreach ($box->params as $param_key => $param_value) |
| 810 |
{ |
| 811 |
if (strpos($param_key, 'assign') === false || in_array($param_key, $params_to_ignore)) |
| 812 |
{ |
| 813 |
continue; |
| 814 |
} |
| 815 |
|
| 816 |
$assignments[$param_key] = $param_value; |
| 817 |
} |
| 818 |
|
| 819 |
return new Registry($assignments); |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
* Prefixes the CSS classes |
| 824 |
* |
| 825 |
* @param array $classes |
| 826 |
* @param string $prefix |
| 827 |
* |
| 828 |
* @return void |
| 829 |
*/ |
| 830 |
private static function prefixCSSClasses(&$classes, $prefix = 'fb-') |
| 831 |
{ |
| 832 |
$classes = array_filter($classes); |
| 833 |
|
| 834 |
if (empty($classes)) |
| 835 |
{ |
| 836 |
return; |
| 837 |
} |
| 838 |
|
| 839 |
foreach ($classes as &$class) |
| 840 |
{ |
| 841 |
$class = $prefix . $class; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
/** |
| 846 |
* Track box open |
| 847 |
* |
| 848 |
* @param integer $box_id |
| 849 |
* @param string $page |
| 850 |
* @param string $referrer |
| 851 |
* |
| 852 |
* @return void |
| 853 |
*/ |
| 854 |
public function logOpenEvent($box_id, $page = null, $referrer = null) |
| 855 |
{ |
| 856 |
$box = $this->get($box_id); |
| 857 |
|
| 858 |
// Do not track if statistics option is disabled |
| 859 |
$track_open_event = (bool) (is_null($box->params->get('stats', null)) ? BoxHelper::getParams()->get('stats', 1) : $box->params->get('stats')); |
| 860 |
if (!$track_open_event) |
| 861 |
{ |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
return firebox()->log->track($box_id, 1, null, $page, $referrer); |
| 866 |
} |
| 867 |
|
| 868 |
/** |
| 869 |
* Track box close |
| 870 |
* |
| 871 |
* @param integer $box_id |
| 872 |
* @param integer $box_log_id |
| 873 |
* |
| 874 |
* @return void |
| 875 |
*/ |
| 876 |
public function logCloseEvent($box_id, $box_log_id) |
| 877 |
{ |
| 878 |
$box = $this->get($box_id); |
| 879 |
|
| 880 |
// Do not track if statistics option is disabled |
| 881 |
$track_open_event = (bool) (is_null($box->params->get('stats', null)) ? BoxHelper::getParams()->get('stats', 1) : $box->params->get('stats')); |
| 882 |
if (!$track_open_event) |
| 883 |
{ |
| 884 |
return null; |
| 885 |
} |
| 886 |
|
| 887 |
firebox()->log->track($box_id, 2, $box_log_id); |
| 888 |
} |
| 889 |
|
| 890 |
/** |
| 891 |
* Get box impressions |
| 892 |
* |
| 893 |
* @param array $payload |
| 894 |
* |
| 895 |
* @return array |
| 896 |
*/ |
| 897 |
public function getImpressions($payload) |
| 898 |
{ |
| 899 |
return firebox()->tables->boxlog->getResults($payload); |
| 900 |
} |
| 901 |
|
| 902 |
/** |
| 903 |
* Get total box impressions |
| 904 |
* |
| 905 |
* @param array $payload |
| 906 |
* |
| 907 |
* @return array |
| 908 |
*/ |
| 909 |
public function getTotalImpressions($payload) |
| 910 |
{ |
| 911 |
return count($this->getImpressions($payload)); |
| 912 |
} |
| 913 |
|
| 914 |
/** |
| 915 |
* Return the box settings |
| 916 |
* |
| 917 |
* @return object |
| 918 |
*/ |
| 919 |
public function getBoxSettings() |
| 920 |
{ |
| 921 |
return $this->boxSettings; |
| 922 |
} |
| 923 |
|
| 924 |
/** |
| 925 |
* Sets the box settings |
| 926 |
* |
| 927 |
* @param object $settings |
| 928 |
* |
| 929 |
* @return object |
| 930 |
*/ |
| 931 |
public function setBoxSettings($settings) |
| 932 |
{ |
| 933 |
$this->boxSettings = $settings; |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* Returns the box cookie |
| 938 |
* |
| 939 |
* @param string $cookie |
| 940 |
* |
| 941 |
* @return mixed |
| 942 |
*/ |
| 943 |
public function getCookie($cookie = null) |
| 944 |
{ |
| 945 |
$cookie = $cookie ? $cookie : (isset($this->boxSettings->ID) ? $this->boxSettings->ID : null); |
| 946 |
if (!isset($cookie)) |
| 947 |
{ |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
return new Cookie($cookie); |
| 952 |
} |
| 953 |
} |