PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.8
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.8
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Helpers / BoxHelper.php

BoxHelper.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.8, at Inc/Core/Helpers/BoxHelper.php

453 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 3.1.8 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\Helpers;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Libs\Registry;
20
21 class BoxHelper
22 {
23 /**
24 * Get box meta
25 *
26 * @param int $id
27 *
28 * @return array
29 */
30 public static function getMeta($id)
31 {
32 $meta = get_post_meta($id, 'firebox_meta', false);
33 $meta = isset($meta[0]) && is_array($meta[0]) ? $meta[0] : [];
34
35 return $meta;
36 }
37
38 /**
39 * Gets all Boxes.
40 *
41 * @param array $status
42 * @param int $limit
43 *
44 * @return array
45 */
46 public static function getAllBoxes($status = ['publish'], $limit = -1)
47 {
48 // cache key
49 $hash = md5('firebox_getAllBoxes_' . implode(',', $status) . '_' . (int) $limit);
50
51 // check cache
52 if ($data = wp_cache_get($hash))
53 {
54 return $data;
55 }
56
57 $args = [
58 'post_status' => $status,
59 'post_type' => 'firebox',
60 'posts_per_page' => $limit,
61 'no_found_rows' => true,
62 'update_post_term_cache' => false,
63 'cache_results' => true
64 ];
65
66 // Get the query.
67 $query = new \WP_Query($args);
68
69 wp_reset_postdata();
70
71 // set cache
72 wp_cache_set($hash, $query);
73
74 return $query;
75 }
76
77 /**
78 * Retrieves all boxes in a key => value array of ID => title
79 *
80 * @return array
81 */
82 public static function getAllBoxesParsedByKeyValue()
83 {
84 if (!$boxes = self::getAllBoxes())
85 {
86 return [];
87 }
88
89 return self::produceKeyValueBoxes($boxes->posts);
90 }
91
92 /**
93 * Produce a key,value pair of boxes containg their ID,title
94 *
95 * @return array
96 */
97 public static function produceKeyValueBoxes($boxes)
98 {
99 if (!$boxes)
100 {
101 return [];
102 }
103
104 $data = [];
105
106 foreach ($boxes as $key => $box)
107 {
108 $data[$box->ID] = $box->post_title;
109 }
110
111 return $data;
112 }
113
114 /**
115 * Gets all published Boxes except the given id.
116 * The array structure is [ID, title] to properly appear in a Dropdown field.
117 *
118 * @param integer $id
119 *
120 * @return array
121 */
122 public static function getAllMirrorBoxesExceptID($id)
123 {
124 if (!$id)
125 {
126 return [];
127 }
128
129 $boxes = firebox()->tables->box->getResults([
130 'where' => [
131 'ID' => ' NOT IN (' . absint($id) . ')',
132 'post_status' => " = 'publish'",
133 'post_type' => " = 'firebox'"
134 ]
135 ]);
136
137 $boxes_parsed = [];
138
139 foreach ($boxes as $key => $p)
140 {
141 $boxes_parsed[$p->ID] = $p->post_title . ' (' . $p->ID . ')';
142 }
143
144 return $boxes_parsed;
145 }
146
147 /**
148 * Get box data
149 *
150 * @param int $box
151 *
152 * @return array
153 */
154 public static function getBoxData($box)
155 {
156 if (!$box)
157 {
158 return false;
159 }
160
161 $box = (int) $box;
162
163 $box = firebox()->tables->box->getResults([
164 'where' => [
165 'ID' => " = '" . absint($box) . "'"
166 ]
167 ]);
168
169 return isset($box[0]) ? $box[0] : [];
170 }
171
172 /**
173 * Checks whether the box exist
174 *
175 * @param int $box
176 *
177 * @return boolean
178 */
179 public static function boxExist($box)
180 {
181 if (!$box)
182 {
183 return false;
184 }
185
186 $box = (int) $box;
187
188 $box = firebox()->tables->box->getResults([
189 'where' => [
190 'ID' => " = '" . absint($box) . "'"
191 ]
192 ]);
193
194 if (!$box)
195 {
196 return false;
197 }
198
199 return true;
200 }
201
202 /**
203 * Gets boxes in a [id, title] pair from a list of Box IDs
204 *
205 * @param array $items
206 *
207 * @return array
208 */
209 public static function getSelectedSearchItems($items)
210 {
211 $boxes = firebox()->tables->box->getResults([
212 'where' => [
213 'ID' => ' IN(' . implode(',', array_map('intval', $items)) . ')',
214 'post_status' => " = 'publish'",
215 'post_type' => " = 'firebox'"
216 ]
217 ]);
218
219 $boxes_parsed = [];
220
221 foreach ($boxes as $key => $p)
222 {
223 $boxes_parsed[] = [
224 'id' => $p->ID,
225 'title' => $p->post_title
226 ];
227 }
228
229 return $boxes_parsed;
230 }
231
232 /**
233 * Gets Settings Data
234 *
235 * @return array
236 */
237 public static function getParams()
238 {
239 // cache key
240 $cache_key = md5('fboxSettings');
241
242 // check cache
243 if ($params = wp_cache_get($cache_key))
244 {
245 return $params;
246 }
247
248 // get params
249 $params = get_option('firebox_settings');
250
251 // set cache
252 wp_cache_set($cache_key, $params);
253
254 return $params;
255 }
256
257 /**
258 * Duplicates a box
259 *
260 * @param integer $box_id
261 *
262 * @return bool
263 */
264 public static function duplicateBox($box_id)
265 {
266 // get box
267 $box = firebox()->tables->box->getResults([
268 'where' => [
269 'ID' => " = '" . absint($box_id) . "'",
270 'post_status' => " = '" . sanitize_key(get_post_status($box_id)) . "'",
271 'post_type' => " = 'firebox'"
272 ],
273 'limit' => 1
274 ]);
275
276 if (empty($box))
277 {
278 return false;
279 }
280
281 // reset box ID and make it a draft
282 $box = $box[0];
283 $box->ID = '';
284 $box->post_title = 'Copy of ' . $box->post_title;
285 $box->post_status = 'draft';
286
287 $factory = new \FPFramework\Base\Factory();
288
289 $tz = wp_timezone();
290 $date_without_tz = $factory->getDate();
291 $date_with_tz = $factory->getDate()->setTimezone($tz);
292
293 $box->post_date = $date_with_tz->format('Y-m-d H:i:s');
294 $box->post_date_gmt = $date_without_tz->format('Y-m-d H:i:s');
295
296 Form\Form::ensureUniqueFormIDs($box->post_content);
297
298 // get meta options
299 $meta = self::getMeta($box_id);
300
301 // insert new box
302 $new_box_id = firebox()->tables->box->insert($box);
303
304 // add meta options for new box
305 // TODO: In the future, use "firebox_meta". This is a temporary fix for backwards compatibility.
306 $checkMeta = (array) $meta;
307 $meta_key = isset($checkMeta['width']) ? 'firebox_meta' : 'fpframework_meta_settings';
308 update_post_meta($new_box_id, $meta_key, wp_slash($meta));
309
310 return true;
311 }
312
313 /**
314 * Reset Box Stats
315 *
316 * @param array $box_ids
317 *
318 * @return void
319 */
320 public static function resetBoxStats($box_ids)
321 {
322 $logs_table = firebox()->tables->boxlog->getFullTableName();
323 $logs_details_table = firebox()->tables->boxlogdetails->getFullTableName();
324
325 // delete box logs details
326 firebox()->tables->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box IN (" . implode(",", $box_ids) . "))");
327
328 // delete box logs
329 firebox()->tables->boxlog->deleteRaw('WHERE box IN (' . implode(',', $box_ids) . ')');
330 }
331
332 /**
333 * Exports boxes
334 *
335 * @param array $box_ids
336 *
337 * @return string
338 */
339 public static function exportBoxes($box_ids)
340 {
341 // get boxes
342 $boxes = firebox()->tables->box->getResults([
343 'where' => [
344 'ID' => ' IN (' . implode(',', array_map('intval', $box_ids)) . ')',
345 'post_type' => " = 'firebox'"
346 ]
347 ]);
348
349 $boxes = (array) $boxes;
350
351 if (!count($boxes))
352 {
353 return;
354 }
355
356 $exported = [];
357
358 $filename = firebox()->_('FB_PLUGIN_NAME') . ' Items';
359
360 // name for 1 box
361 if (count($boxes) == 1)
362 {
363 $name = mb_strtolower(html_entity_decode($boxes['0']->post_title));
364 $name = preg_replace('#[^a-z0-9_-]#', '_', $name);
365 $name = trim(preg_replace('#__+#', '_', $name), '_-');
366
367 $filename = firebox()->_('FB_PLUGIN_NAME') . ' Item (' . $name . ')';
368 }
369
370 foreach ($boxes as $box)
371 {
372 $meta = self::getMeta($box->ID);
373
374 $exported[] = [
375 'box' => $box,
376 'meta' => $meta
377 ];
378 }
379
380 // SET DOCUMENT HEADER
381 $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '';
382 if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', $userAgent))
383 {
384 $UserBrowser = "Opera";
385 }
386 elseif (preg_match('#MSIE ([0-9].[0-9]{1,2})#', $userAgent))
387 {
388 $UserBrowser = "IE";
389 }
390 else
391 {
392 $UserBrowser = '';
393 }
394 $mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
395 @ob_end_clean();
396 ob_start();
397
398 header('Content-Type: ' . $mime_type);
399 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
400
401 if ($UserBrowser == 'IE')
402 {
403 header('Content-Disposition: inline; filename="' . $filename . '.fbox"');
404 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
405 header('Pragma: public');
406 }
407 else
408 {
409 header('Content-Disposition: attachment; filename="' . $filename . '.fbox"');
410 header('Pragma: no-cache');
411 }
412
413 // PRINT STRING
414 echo wp_json_encode($exported);
415
416 if (ob_get_level())
417 {
418 @ob_end_flush();
419 }
420 die();
421 }
422
423 /**
424 * Returns the last date viewed of a campaign.
425 *
426 * @param int $id
427 *
428 * @return string
429 */
430 public static function getCampaignLastDateViewed($id = null)
431 {
432 if (!$id)
433 {
434 return;
435 }
436
437 $last_date_viewed = firebox()->tables->boxlog->getResults([
438 'select' => [
439 'date as last_date_viewed'
440 ],
441 'where' => [
442 'box' => ' = ' . absint($id),
443 ],
444 'orderby' => ' date desc',
445 'limit' => 1
446 ]);
447
448 return isset($last_date_viewed[0]->last_date_viewed) ? get_date_from_gmt($last_date_viewed[0]->last_date_viewed) : null;
449 }
450
451
452 }
453