* @link https://www.fireplugins.com * @copyright Copyright © 2026 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class Previewer { /** * FireBox data. * * @var array */ public $box_data; protected $embed_html = ''; /** * Inits Previewer * * @return void */ public function init() { if (!$this->is_preview_page()) { return false; } $this->hooks(); return true; } /** * Check if current page request meets requirements for firebox preview page. * * @return boolean */ public function is_preview_page() { global $post; $post_type = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended $post_id = isset($_GET['p']) ? \absint($_GET['p']) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended if (!$post_type && !$post_id && $post) { $post_type = $post->post_type; $post_id = $post->ID; } // Only proceed if we have a item to preview if (empty($post_type)) { return false; } if ($post_type != 'firebox') { return false; } if (empty($post_id)) { return false; } if (empty($_GET['preview'])) //phpcs:ignore WordPress.Security.NonceVerification.Recommended { return false; } if (!sanitize_key(wp_unslash($_GET['preview']))) //phpcs:ignore WordPress.Security.NonceVerification.Recommended { return false; } // Check for logged in user. if (!\is_user_logged_in()) { return false; } if (!\current_user_can('read_firebox', $post_id)) { return false; } if (!$this->box_data = firebox()->box->get($post_id)) { return false; } return true; } /** * Hooks * * @return void */ public function hooks() { if ($this->box_data->params->get('mode') !== 'embed') { // Set to trigger on Page Load $this->box_data->params->set('triggermethod', 'pageload'); } // Remove Impressions $this->box_data->params->set('assign_impressions_param_type', 'always'); // Remove Assignments $this->box_data->params->remove('assignments'); // Remove Rules $this->box_data->params->remove('rules'); // Enable Test Mode to prevent cookies $this->box_data->params->set('testmode', true); // Disable Statistics to prevent impressions from being tracked into the database $this->box_data->params->set('stats', 0); \FireBox\Core\Helpers\Actions::run(); if ($this->box_data->params->get('mode') !== 'embed') { firebox()->box->setBox($this->box_data)->render(); } else { global $post; $this->embed_html = \FireBox\Core\Helpers\Embed::renderCampaign($post->ID, ['draft']); } \add_filter('the_title', [$this, 'the_title'], 100, 1); \add_filter('the_content', [$this, 'the_content'], 999); \add_filter('get_the_excerpt', [$this, 'the_content'], 999); } public function the_content() { if (!isset($this->box_data->ID)) { return ''; } if (!current_user_can('read_firebox', $this->box_data->ID)) { return ''; } $links = []; $links[] = [ 'url' => esc_url( add_query_arg( [ 'post' => absint($this->box_data->ID), 'action' => 'edit', ], admin_url('post.php') ) ), 'text' => esc_html(firebox()->_('FB_EDIT_CAMPAIGN')), ]; $links[] = [ 'url' => esc_url( add_query_arg( [ 'post_type' => 'firebox', ], admin_url('edit.php') ) ), 'text' => esc_html(firebox()->_('FB_VIEW_CAMPAIGNS')), ]; $content = '
'; $content .= '

'; $content .= esc_html(firebox()->_('FB_FIREBOX_PREVIEW_DESC')); $content .= '
'; foreach ($links as $key => $link) { $content .= '' . esc_html($link['text']) . ''; $l = array_keys($links); if (end($l) !== $key) { $content .= ' | '; } } $content .= '

'; $content .= '
'; // Add embed campaign to preview page if ($this->box_data->params->get('mode') === 'embed') { $content .= '
' . $this->embed_html . '
'; } return $content; } /** * Customize firebox popup preview page title. * * @param string $title * * @return string */ public function the_title($title) { if (in_the_loop()) { $title = sprintf( esc_html('%s'), ! empty($this->box_data->post_title) ? sanitize_text_field($this->box_data->post_title) . ' ' . firebox()->_('FB_PREVIEW') : esc_html(firebox()->_('FB_FIREBOX_CAMPAIGN_PREVIEW')) ); } return $title; } }