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

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