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

222 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 1.0.2 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\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 $country_value = '';
71
72
73
74 // Everything seems OK. Let's save data to db.
75 $data = (object) [
76 'sessionid' => $this->factory->getSession()->getSessionID(),
77 'visitorid' => $visitorID,
78 'user' => $this->factory->getUser()->ID,
79 'box' => $boxid,
80 'page' => $page,
81 'country' => $country_value,
82 'device' => $this->factory->getDevice(),
83 'referrer' => $referrer,
84 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
85 ];
86
87 return $this->handleOpen($data);
88 }
89
90 // other events
91 return $this->handleOtherEvents($box_log_id, $event_id);
92 }
93
94 /**
95 * Handle the logging when opening a box
96 *
97 * @param array $passed_data
98 *
99 * @return mixed
100 */
101 private function handleOpen($passed_data)
102 {
103 // Insert the object into the firebox logs table
104 try
105 {
106 $log_id = firebox()->models->boxlog->insert($passed_data);
107
108 // clean up
109 $this->clean();
110 return $log_id;
111 }
112 catch (Exception $e)
113 {
114 }
115 }
116
117 /**
118 * Handle the logging of other events
119 *
120 * @param int $box_log_id
121 * @param int $event_id
122 *
123 * @return mixed
124 */
125 private function handleOtherEvents($box_log_id, $event_id)
126 {
127 if (!$box_log_id)
128 {
129 return;
130 }
131
132 // Insert the object into the firebox logs details table
133 try
134 {
135 $data = [
136 'log_id' => $box_log_id,
137 'event' => $this->parseEvent($event_id),
138 'params' => '',
139 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
140 ];
141
142 firebox()->models->boxlogdetails->insert($data);
143
144 // clean up
145 $this->clean();
146 return;
147 }
148 catch (Exception $e)
149 {
150 }
151 }
152
153 /**
154 * Translates the event ID to name
155 *
156 * @param int $id
157 *
158 * @return string
159 */
160 private function parseEvent($id)
161 {
162 $name = '';
163
164 switch ($id)
165 {
166 case 1:
167 $name = 'open';
168 break;
169 case 2:
170 $name = 'close';
171 break;
172 }
173
174 return $name;
175 }
176
177 /**
178 * Removes old rows from the logs table
179 * Runs every 12 hours with a self-check
180 *
181 * @return void
182 */
183 private function clean()
184 {
185 // cache key
186 $hash = md5('fireboxclean');
187
188 // check cache
189 if ($params = wp_cache_get($hash))
190 {
191 return;
192 }
193
194 // Removes rows older than x days
195 $stats = \FireBox\Core\Helpers\BoxHelper::getParams();
196 $stats = new Registry($stats);
197
198 $days = $stats->get('statsdays', 180);
199
200 if ($days == 'custom')
201 {
202 $days = $stats->get('statsdays_custom', 1);
203 $days = $days < 0 ? 1 : $days;
204 }
205
206 global $wpdb;
207
208 $sql = 'DELETE bl, bld
209 FROM `' . firebox()->models->boxlog->getFullTableName() . '` as bl
210 LEFT JOIN `' . firebox()->models->boxlogdetails->getFullTableName() . '` as bld
211 ON bld.log_id = bl.id
212 WHERE
213 bl.date < DATE_SUB(NOW(), INTERVAL ' . $wpdb->prepare('%d', $days) . ' DAY)';
214
215 firebox()->models->boxlog->executeRaw($sql);
216
217 // set cache
218 wp_cache_set($hash, $params, $hash, 720);
219
220 return true;
221 }
222 }