* @link https://www.fireplugins.com * @copyright Copyright © 2026 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core\FB; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } use FireBox\Core\Helpers\BoxHelper; use FPFramework\Libs\Registry; use FPFramework\Helpers\Fields\DimensionsHelper; use FPFramework\Helpers\CSS; class Box { /** * Send useful JS snippet once in first box * * @var boolean */ static $loadedLocalizedScript = false; /** * The box. * * @param object */ private $box = null; /** * Factory * * @var Factory */ private $factory = null; /** * FireBox settings. * * @var object */ private $params = null; /** * Popup CSS. * * @var CSS */ public $css = null; /** * Display condition groups that must be resolved in the browser before the * campaign may open (session-dependent rules like Time on Site/Pageviews). * Populated by pass(), shipped to the frontend runtime via prepare(). * * @var array */ private $clientRuleGroups = []; /** * Constructor. * * @param object $box * @param object $factory * * @return void */ public function __construct($box = null, $factory = null) { if ($box) { $this->box = $this->prepareConstructorBox($box); } if (!$factory) { $factory = new \FPFramework\Base\Factory(); } $this->factory = $factory; $this->params = new Registry(BoxHelper::getParams()); } /** * Allow to set either a box ID or box object * and we then set the box object. * * @param mixed $box * * @return object */ private function prepareConstructorBox($box) { if (!is_object($box)) { $box = $this->get($box); } return $box; } /** * Get a box. * * @param int $id * @param string $status * * @return object|null */ public function get($id = null, $status = null) { if (!$id) { return null; } $payload = [ 'where' => [ 'ID' => ' = ' . intval($id), 'post_type' => " = 'firebox'" ] ]; // apply status if given if ($status) { $payload['where']['post_status'] = ' = \'' . sanitize_key($status) . '\''; } if (!$box = firebox()->tables->box->getResults($payload)) { return null; } if (!isset($box[0])) { return null; } $this->box = $box[0]; // get meta options for box $meta = \FireBox\Core\Helpers\BoxHelper::getMeta($id); $this->box->params = new Registry($meta); return $this->box; } /** * Renders the box. * * @return void */ public function render() { // Check Publishing Assignments if (!$this->pass()) { return false; } $fbox = $this->box; /** * Runs before rendering the box. */ $this->box = apply_filters('firebox/box/before_render', $this->box); $this->prepare(); $css = $this->getCustomCSS(); add_action('wp_enqueue_scripts', function() use ($fbox, $css) { // Loads all media files. $this->loadBoxMedia($fbox); // Load CSS if ($css) { wp_add_inline_style('firebox', $css); } }); // Allow to manipulate the box before rendering $this->box = apply_filters('firebox/box/edit', $this->box); // payload $payload = [ 'box' => $this->box, 'params' => $this->params, ]; // print campaign HTML add_action('wp_footer', function() use ($payload) { echo $this->getFinalCampaignHTML($payload); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped }); return true; } public function renderEmbed() { // Check Publishing Assignments if (!$this->pass()) { return false; } /** * Runs before rendering the box. */ $this->box = apply_filters('firebox/box/before_render', $this->box); $this->prepare(); // Loads all media files. $this->loadBoxMedia($this->box); $css = $this->getCustomCSS(); wp_register_style('fireboxStyle', false); wp_enqueue_style('fireboxStyle'); // Load CSS if ($css) { wp_add_inline_style('fireboxStyle', $css); } // Allow to manipulate the box before rendering $this->box = apply_filters('firebox/box/edit', $this->box); // payload $payload = [ 'box' => $this->box, 'params' => $this->params, ]; // return box template return $this->getFinalCampaignHTML($payload); } public function getFinalCampaignHTML($payload) { $html = firebox()->renderer->public->render('box', $payload, true); /** * Runs after rendering the box. */ return apply_filters('firebox/box/after_render', $html, $payload['box']); } /** * Gets the Custom CSS of the popup. * * @return string */ public function getCustomCSS() { if (!is_object($this->box) || !isset($this->box->params)) { return ''; } $css = $this->box->params->get('customcss', ''); if (!is_string($css)) { return ''; } /** * The CSS lands inside an inline " * would end the element and let markup (e.g. a