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 / FB / Log.php

Log.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 2.1.0, at Inc/Core/FB/Log.php

223 lines 5.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\FB;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Libs\Registry;
20 use FireBox\Core\Helpers\BoxHelper;
21
22 class Log
23 {
24 /**
25 * Factory
26 *
27 * @var Factory
28 */
29 private $factory;
30
31 public function __construct($factory = null)
32 {
33 if (!$factory)
34 {
35 $factory = new \FPFramework\Base\Factory();
36 }
37
38 $this->factory = $factory;
39 }
40
41 /**
42 * Logs box events to the database
43 *
44 * @param integer $boxid The box id
45 * @param string $page The page the box appeared
46 * @param string $referrer The referrer
47 * @param integer $event_id Event id: 1=Open, 2=Close
48 * @param integer $box_log_id The box log ID of the open event that we are closing
49 *
50 * @return bool Returns a boolean indicating if the event logged successfully
51 */
52 public function track($boxid, $event_id = 1, $box_log_id = null, $page = '', $referrer = '')
53 {
54 // Making sure we have a valid Boxid
55 if (!$boxid)
56 {
57 return;
58 }
59
60 // open event
61 if ($event_id == 1)
62 {
63 // Get visitor's token id
64 if (!$visitorID = $this->factory->getVisitorID())
65 {
66 return;
67 }
68
69
70 $country_value = '';
71
72
73
74
75 // Everything seems OK. Let's save data to db.
76 $data = (object) [
77 'sessionid' => $this->factory->getSession()->getSessionID(),
78 'visitorid' => $visitorID,
79 'user' => $this->factory->getUser()->ID,
80 'box' => $boxid,
81 'page' => $page,
82 'country' => $country_value,
83 'device' => $this->factory->getDevice(),
84 'referrer' => $referrer,
85 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
86 ];
87
88 return $this->handleOpen($data);
89 }
90
91 // other events
92 return $this->handleOtherEvents($box_log_id, $event_id);
93 }
94
95 /**
96 * Handle the logging when opening a box
97 *
98 * @param array $passed_data
99 *
100 * @return mixed
101 */
102 private function handleOpen($passed_data)
103 {
104 // Insert the object into the firebox logs table
105 try
106 {
107 $log_id = firebox()->tables->boxlog->insert($passed_data);
108
109 // clean up
110 $this->clean();
111 return $log_id;
112 }
113 catch (Exception $e)
114 {
115 }
116 }
117
118 /**
119 * Handle the logging of other events
120 *
121 * @param int $box_log_id
122 * @param int $event_id
123 *
124 * @return mixed
125 */
126 private function handleOtherEvents($box_log_id, $event_id)
127 {
128 if (!$box_log_id)
129 {
130 return;
131 }
132
133 // Insert the object into the firebox logs details table
134 try
135 {
136 $data = [
137 'log_id' => $box_log_id,
138 'event' => $this->parseEvent($event_id),
139 'params' => '',
140 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
141 ];
142
143 firebox()->tables->boxlogdetails->insert($data);
144
145 // clean up
146 $this->clean();
147 return;
148 }
149 catch (Exception $e)
150 {
151 }
152 }
153
154 /**
155 * Translates the event ID to name
156 *
157 * @param int $id
158 *
159 * @return string
160 */
161 private function parseEvent($id)
162 {
163 $name = '';
164
165 switch ($id)
166 {
167 case 1:
168 $name = 'open';
169 break;
170 case 2:
171 $name = 'close';
172 break;
173 }
174
175 return $name;
176 }
177
178 /**
179 * Removes old rows from the logs table
180 * Runs every 12 hours with a self-check
181 *
182 * @return void
183 */
184 private function clean()
185 {
186 // cache key
187 $hash = md5('fireboxclean');
188
189 // check cache
190 if ($params = wp_cache_get($hash))
191 {
192 return;
193 }
194
195 // Removes rows older than x days
196 $stats = \FireBox\Core\Helpers\BoxHelper::getParams();
197 $stats = new Registry($stats);
198
199 $days = $stats->get('statsdays', 180);
200
201 if ($days == 'custom')
202 {
203 $days = $stats->get('statsdays_custom', 1);
204 $days = $days < 0 ? 1 : $days;
205 }
206
207 global $wpdb;
208
209 $sql = 'DELETE bl, bld
210 FROM `' . firebox()->tables->boxlog->getFullTableName() . '` as bl
211 LEFT JOIN `' . firebox()->tables->boxlogdetails->getFullTableName() . '` as bld
212 ON bld.log_id = bl.id
213 WHERE
214 bl.date < DATE_SUB(NOW(), INTERVAL ' . $wpdb->prepare('%d', $days) . ' DAY)';
215
216 firebox()->tables->boxlog->executeRaw($sql);
217
218 // set cache
219 wp_cache_set($hash, $params, $hash, 720);
220
221 return true;
222 }
223 }