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

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