| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.5 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Previewer |
| 20 |
{ |
| 21 |
/** |
| 22 |
* FireBox data. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
public $box_data; |
| 27 |
|
| 28 |
protected $embed_html = ''; |
| 29 |
|
| 30 |
/** |
| 31 |
* Inits Previewer |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function init() |
| 36 |
{ |
| 37 |
if (!$this->is_preview_page()) |
| 38 |
{ |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
$this->hooks(); |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if current page request meets requirements for firebox preview page. |
| 48 |
* |
| 49 |
* @return boolean |
| 50 |
*/ |
| 51 |
public function is_preview_page() |
| 52 |
{ |
| 53 |
global $post; |
| 54 |
$post_type = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 55 |
$post_id = isset($_GET['p']) ? \absint($_GET['p']) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 56 |
|
| 57 |
if (!$post_type && !$post_id && $post) |
| 58 |
{ |
| 59 |
$post_type = $post->post_type; |
| 60 |
$post_id = $post->ID; |
| 61 |
} |
| 62 |
|
| 63 |
// Only proceed if we have a item to preview |
| 64 |
if (empty($post_type)) |
| 65 |
{ |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if ($post_type != 'firebox') |
| 70 |
{ |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
if (empty($post_id)) |
| 75 |
{ |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
if (empty($_GET['preview'])) //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 80 |
{ |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
if (!sanitize_key(wp_unslash($_GET['preview']))) //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 85 |
{ |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
// Check for logged in user. |
| 90 |
if (!\is_user_logged_in()) |
| 91 |
{ |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
if (!\current_user_can('read_firebox')) |
| 96 |
{ |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if (!$this->box_data = firebox()->box->get($post_id)) |
| 101 |
{ |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Hooks |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function hooks() |
| 114 |
{ |
| 115 |
if ($this->box_data->params->get('mode') !== 'embed') |
| 116 |
{ |
| 117 |
// Set to trigger on Page Load |
| 118 |
$this->box_data->params->set('triggermethod', 'pageload'); |
| 119 |
} |
| 120 |
|
| 121 |
// Remove Impressions |
| 122 |
$this->box_data->params->set('assign_impressions_param_type', 'always'); |
| 123 |
// Remove Assignments |
| 124 |
$this->box_data->params->remove('assignments'); |
| 125 |
// Remove Rules |
| 126 |
$this->box_data->params->remove('rules'); |
| 127 |
// Enable Test Mode to prevent cookies |
| 128 |
$this->box_data->params->set('testmode', true); |
| 129 |
// Disable Statistics to prevent impressions from being tracked into the database |
| 130 |
$this->box_data->params->set('stats', 0); |
| 131 |
|
| 132 |
\FireBox\Core\Helpers\Actions::run(); |
| 133 |
|
| 134 |
if ($this->box_data->params->get('mode') !== 'embed') |
| 135 |
{ |
| 136 |
firebox()->box->setBox($this->box_data)->render(); |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
global $post; |
| 141 |
$this->embed_html = \FireBox\Core\Helpers\Embed::renderCampaign($post->ID, ['draft']); |
| 142 |
} |
| 143 |
|
| 144 |
\add_filter('the_title', [$this, 'the_title'], 100, 1); |
| 145 |
|
| 146 |
\add_filter('the_content', [$this, 'the_content'], 999); |
| 147 |
|
| 148 |
\add_filter('get_the_excerpt', [$this, 'the_content'], 999); |
| 149 |
} |
| 150 |
|
| 151 |
public function the_content() |
| 152 |
{ |
| 153 |
if (!isset($this->box_data->ID)) |
| 154 |
{ |
| 155 |
return ''; |
| 156 |
} |
| 157 |
|
| 158 |
if (!current_user_can('read_firebox')) |
| 159 |
{ |
| 160 |
return ''; |
| 161 |
} |
| 162 |
|
| 163 |
$links = []; |
| 164 |
|
| 165 |
$links[] = [ |
| 166 |
'url' => esc_url( |
| 167 |
add_query_arg( |
| 168 |
[ |
| 169 |
'post' => absint($this->box_data->ID), |
| 170 |
'action' => 'edit', |
| 171 |
], |
| 172 |
admin_url('post.php') |
| 173 |
) |
| 174 |
), |
| 175 |
'text' => esc_html(firebox()->_('FB_EDIT_CAMPAIGN')), |
| 176 |
]; |
| 177 |
|
| 178 |
$links[] = [ |
| 179 |
'url' => esc_url( |
| 180 |
add_query_arg( |
| 181 |
[ |
| 182 |
'post_type' => 'firebox', |
| 183 |
], |
| 184 |
admin_url('edit.php') |
| 185 |
) |
| 186 |
), |
| 187 |
'text' => esc_html(firebox()->_('FB_VIEW_CAMPAIGNS')), |
| 188 |
]; |
| 189 |
|
| 190 |
$content = '<div style="padding: 15px; background: #ededed;">'; |
| 191 |
$content .= '<p>'; |
| 192 |
$content .= esc_html(firebox()->_('FB_FIREBOX_PREVIEW_DESC')); |
| 193 |
if (!empty($links)) |
| 194 |
{ |
| 195 |
$content .= '<br>'; |
| 196 |
foreach ($links as $key => $link) |
| 197 |
{ |
| 198 |
$content .= '<a href="' . esc_url($link['url']) . '">' . esc_html($link['text']) . '</a>'; |
| 199 |
$l = array_keys($links); |
| 200 |
if (end($l) !== $key) |
| 201 |
{ |
| 202 |
$content .= ' <span style="display:inline-block;margin:0 6px;opacity: 0.5">|</span> '; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
$content .= '</p>'; |
| 207 |
$content .= '</div>'; |
| 208 |
|
| 209 |
// Add embed campaign to preview page |
| 210 |
if ($this->box_data->params->get('mode') === 'embed') |
| 211 |
{ |
| 212 |
$content .= '<div style="margin: 30px auto; display: flex; justify-content: center;">' . $this->embed_html . '</div>'; |
| 213 |
} |
| 214 |
|
| 215 |
return $content; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Customize firebox popup preview page title. |
| 220 |
* |
| 221 |
* @param string $title |
| 222 |
* |
| 223 |
* @return string |
| 224 |
*/ |
| 225 |
public function the_title($title) |
| 226 |
{ |
| 227 |
if (in_the_loop()) |
| 228 |
{ |
| 229 |
$title = sprintf( |
| 230 |
esc_html('%s'), |
| 231 |
! empty($this->box_data->post_title) ? sanitize_text_field($this->box_data->post_title) . ' ' . firebox()->_('FB_PREVIEW') : esc_html(firebox()->_('FB_FIREBOX_CAMPAIGN_PREVIEW')) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
return $title; |
| 236 |
} |
| 237 |
} |