* @link https://www.fireplugins.com * @copyright Copyright © 2021 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } use FPFramework\Libs\Registry; class Frontend { public function __construct() { // ensure we run only on front-end if (is_admin()) { return; } $this->init_dependencies(); $this->render(); } /** * Initializes the dependencies * * @return void */ private function init_dependencies() { /** * Event that runs before any rendering of popups. */ do_action('firebox/boxes/before_render'); $actions = []; // Initialize Sounds $actions[] = new \FireBox\Core\FB\Actions\Sounds(); // Make actions filterable $actions = apply_filters('firebox/actions/box/edit', $actions); // Run actions new \FireBox\Core\FB\Actions\ActionsBase($actions); // Prepare Gutenberg Blocks that contain attributes by FireBox new \FireBox\Core\FB\BoxBlocksParser(); } /** * Renders all boxes on front-end * * @return void */ private function render() { if ($this->checkForBoxPreview()) { return; } if ($this->checkForBoxTemplatePreview()) { return; } // increment session counter \FPFramework\Libs\Functions::incrementSession(); /** * Adds all boxes at the end of page. */ add_action('wp', function() { firebox()->boxes->render(); }); /** * Event that runs after popups have been rendered. */ do_action('firebox/boxes/after_render'); } /** * Check if we are previewing a box and do not show other boxes. * * @return boolean */ private function checkForBoxPreview() { $previewer = new Previewer(); return $previewer->init(); } /** * Check if we are previewing a box template and do not show other boxes. * * @return boolean */ private function checkForBoxTemplatePreview() { // Preview a box template if (!isset($_GET['firebox_preview_template'])) { return false; } $preview_box = sanitize_text_field($_GET['firebox_preview_template']); $this->render_template(base64_decode($preview_box)); return true; } /** * Render template content * * @param string $template_alias * * @return void */ private function render_template($template_alias) { $templates = firebox()->library; if (!$box = $templates->find($template_alias)) { return; } $box->params = new Registry($box->params); // Remove Publishing Assignments foreach ($box->params as $key => $value) { if (strpos($key, 'assign') !== false) { $box->params->remove($key); } } $box->params->remove('assignments'); // Set id to 999999 for making front-end script happy $box->ID = 999999; // Enable Test Mode to prevent cookies $box->params->set('testmode', true); // Disable Statistics to prevent impressions from being tracked into the database $box->stats = 0; return firebox()->box->render($box); } }