| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.6 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\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 |
$date = \DateTime::createFromFormat(\DateTime::ISO8601, '2222-01-01T' . $value->label . ':00Z'); |
| 228 |
if (!$date) |
| 229 |
{ |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
$value->label = $date->setTimezone($tz)->format('H:i'); |
| 234 |
} |
| 235 |
|
| 236 |
return $data; |
| 237 |
} |
| 238 |
} |