PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.0.1
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.0.1
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.0.1, at Inc/Core/Helpers/BoxHelper.php

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