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

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