PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.13
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.13
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 / Admin / Analytics / Helpers / MetricHelper.php

MetricHelper.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 1.0.13, at Inc/Core/Admin/Analytics/Helpers/MetricHelper.php

243 lines 5.6 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.13 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\Admin\Analytics\Helpers;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 class MetricHelper
20 {
21 /**
22 * Adds date data to the dates period at given position(index)
23 *
24 * @param array $dates
25 * @param int $index
26 * @param array $date_data
27 *
28 * @return array
29 */
30 public static function insertDateDataAtPosition($dates, $index, $date_data)
31 {
32 if (!$dates)
33 {
34 $dates = [];
35 }
36
37 if (!is_int($index) || !$date_data)
38 {
39 return [];
40 }
41
42 if (!is_array($dates))
43 {
44 return [];
45 }
46
47 $size = count($dates);
48 if (!is_int($index) || $index < 0 || $index > $size)
49 {
50 return $dates;
51 }
52
53 $temp = array_slice($dates, 0, $index);
54 $temp[] = $date_data;
55 return array_merge($temp, array_slice($dates, $index, $size));
56 }
57
58 /**
59 * Adds missing dates to the data in order to have a consistent chart graph
60 *
61 * @param array $dates
62 * @param array $payload
63 *
64 * @return void
65 */
66 public static function fillMissingData($dates, $payload)
67 {
68 if (!$payload)
69 {
70 return [];
71 }
72
73 if (!is_array($dates))
74 {
75 return [];
76 }
77
78 if (!isset($payload['start_date']) && !isset($payload['total_days']))
79 {
80 return $dates;
81 }
82
83 $start_date = $payload['start_date'];
84 $total_days = $payload['total_days'];
85
86 if ($total_days < 0)
87 {
88 return $dates;
89 }
90
91
92
93 // find the period between start date and total days and fill missing dates
94 $start_expl = explode('/', $start_date);
95
96 $start = new \DateTime();
97 $start->setDate($start_expl[2], $start_expl[1], $start_expl[0]);
98
99 $end = clone $start;
100 $end->add(new \DateInterval('P' . $total_days . 'D'));
101
102 // add 1 extra hour to the custom date range in order to be able to get the last day in the data.
103 if ($payload['options_key'] == 'custom')
104 {
105 $end->add(new \DateInterval('P1D'));
106 }
107
108 $end = $end->format('Y-m-d');
109
110 $period = new \DatePeriod(
111 new \DateTime($start->format('Y-m-d')),
112 new \DateInterval('P1D'),
113 new \DateTime($end)
114 );
115
116 return self::composeFinalDatesData('dates', $period, $dates);
117 }
118
119 /**
120 * Composes the final data array either with all dates in a period
121 * or will all hours in a day.
122 *
123 * @param string $type
124 * @param array $period
125 * @param array $date_data
126 *
127 * @return array
128 */
129 private static function composeFinalDatesData($type, $period, $date_data)
130 {
131 $index = 0;
132 foreach ($period as $key => $value)
133 {
134 // get the current date or hour
135 $date_item = $type == 'dates' ? $value->format('d/m/Y') : $value['label'];
136
137 if (self::dateInPeriodDataExists($date_item, $date_data))
138 {
139 $index++;
140 continue;
141 }
142
143 $data = [
144 'label' => $date_item,
145 'total' => 0
146 ];
147
148 $date_data = self::insertDateDataAtPosition($date_data, $index, $data);
149
150 $index++;
151 }
152
153 return $date_data;
154 }
155
156
157
158 /**
159 * Checks whether the date exists in given period of dates
160 *
161 * @param string $date
162 * @param array $dates
163 *
164 * @return boolean
165 */
166 public static function dateInPeriodDataExists($date, $dates)
167 {
168 if (!$date || !$dates)
169 {
170 return false;
171 }
172
173 if (!is_array($dates))
174 {
175 return false;
176 }
177
178 if (!is_string($date))
179 {
180 return false;
181 }
182
183 foreach ($dates as $key => $value)
184 {
185 if (!isset($value))
186 {
187 continue;
188 }
189
190 $value = (array) $value;
191
192 if (!isset($value['label']))
193 {
194 continue;
195 }
196
197 if ($value['label'] == $date)
198 {
199 return true;
200 }
201 }
202
203 return false;
204 }
205
206 /**
207 * Prepares the hour:minute given in the data by appling the timezone of the site.
208 *
209 * @param string $period
210 * @param array $data
211 *
212 * @return array
213 */
214 public static function prepareDates($period, $data)
215 {
216 if ($period !== 'today')
217 {
218 return $data;
219 }
220
221 $offset = get_option('gmt_offset');
222 $offset = $offset > 0 ? '+' . $offset : $offset;
223 $tz = new \DateTimeZone($offset);
224
225 foreach ($data as $key => &$value)
226 {
227 if (!isset($value->label))
228 {
229 continue;
230 }
231
232 $date = \DateTime::createFromFormat(\DateTime::ISO8601, '2222-01-01T' . $value->label . ':00Z');
233 if (!$date)
234 {
235 continue;
236 }
237
238 $value->label = $date->setTimezone($tz)->format('H:i');
239 }
240
241 return $data;
242 }
243 }