| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.13 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Data |
| 20 |
{ |
| 21 |
private $start_date = null; |
| 22 |
private $end_date = null; |
| 23 |
private $metrics = []; |
| 24 |
private $filters = []; |
| 25 |
private $offset = null; |
| 26 |
private $limit = null; |
| 27 |
protected $options = []; |
| 28 |
|
| 29 |
public function __construct($start_date = '', $end_date = '', $options = []) |
| 30 |
{ |
| 31 |
Helpers\Date::transformStartEndDateToUTC($start_date, $end_date); |
| 32 |
|
| 33 |
$this->start_date = $start_date; |
| 34 |
$this->end_date = $end_date; |
| 35 |
$this->options = $options; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Fluent interface for building analytics queries |
| 40 |
* |
| 41 |
* @param array $metrics Array of metric slugs |
| 42 |
* @return self |
| 43 |
*/ |
| 44 |
public function metrics(array $metrics) |
| 45 |
{ |
| 46 |
$this->metrics = $metrics; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Fluent interface for adding filters |
| 52 |
* |
| 53 |
* @param array $filters |
| 54 |
* @return self |
| 55 |
*/ |
| 56 |
public function filters(array $filters) |
| 57 |
{ |
| 58 |
$this->filters = $filters; |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Fluent interface for pagination |
| 64 |
* |
| 65 |
* @param int $limit |
| 66 |
* @param int $offset |
| 67 |
* @return self |
| 68 |
*/ |
| 69 |
public function paginate($limit, $offset = 0) |
| 70 |
{ |
| 71 |
$this->limit = (int) $limit; |
| 72 |
$this->offset = (int) $offset; |
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Fluent interface for setting limit |
| 78 |
* |
| 79 |
* @param int|null $limit |
| 80 |
* @return self |
| 81 |
*/ |
| 82 |
public function limit($limit) |
| 83 |
{ |
| 84 |
$this->limit = $limit !== null ? (int) $limit : null; |
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Fluent interface for setting offset |
| 90 |
* |
| 91 |
* @param int $offset |
| 92 |
* @return self |
| 93 |
*/ |
| 94 |
public function offset($offset) |
| 95 |
{ |
| 96 |
$this->offset = (int) $offset; |
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get analytics data with improved error handling and performance |
| 102 |
* |
| 103 |
* @param string $type |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public function getData($type = 'list') |
| 107 |
{ |
| 108 |
if (empty($this->metrics)) { |
| 109 |
return []; |
| 110 |
} |
| 111 |
|
| 112 |
$data = []; |
| 113 |
$metric_configs = $this->buildMetricConfigs($type); |
| 114 |
|
| 115 |
foreach ($metric_configs as $metric_slug => $config) { |
| 116 |
try { |
| 117 |
$data[$metric_slug] = $this->getMetricData($config); |
| 118 |
} catch (\Exception $e) { |
| 119 |
$data[$metric_slug] = $this->getEmptyMetricData($type); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $data; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Build metric configurations for batch processing |
| 128 |
* |
| 129 |
* @param string $type |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
private function buildMetricConfigs($type) |
| 133 |
{ |
| 134 |
$configs = []; |
| 135 |
|
| 136 |
foreach ($this->metrics as $metric_slug) { |
| 137 |
$class_name = \FireBox\Core\Analytics\Helpers\Metrics::getClassFromSlug($metric_slug); |
| 138 |
|
| 139 |
if (!$class_name) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
$class_path = '\FireBox\Core\Analytics\Metrics\\' . $class_name; |
| 144 |
|
| 145 |
if (!class_exists($class_path)) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$configs[$metric_slug] = [ |
| 150 |
'class' => $class_path, |
| 151 |
'type' => $type, |
| 152 |
'start_date' => $this->start_date, |
| 153 |
'end_date' => $this->end_date, |
| 154 |
'options' => $this->options, |
| 155 |
'filters' => $this->filters, |
| 156 |
'limit' => $this->limit, |
| 157 |
'offset' => $this->offset |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
return $configs; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get data for a single metric using dependency injection |
| 166 |
* |
| 167 |
* @param array $config |
| 168 |
* @return mixed |
| 169 |
*/ |
| 170 |
private function getMetricData(array $config) |
| 171 |
{ |
| 172 |
$metric = $this->createMetric($config); |
| 173 |
$data = $metric->getData(); |
| 174 |
|
| 175 |
// Apply transformations through the registry system |
| 176 |
if (!empty($data)) { |
| 177 |
\FireBox\Core\Analytics\Transformers\TransformerRegistry::transformData( |
| 178 |
$data, |
| 179 |
$config['type'], |
| 180 |
array_merge($config['options'], [ |
| 181 |
'is_single_day' => \FireBox\Core\Analytics\Helpers\Date::isSingleDay( |
| 182 |
$config['start_date'], |
| 183 |
$config['end_date'] |
| 184 |
), |
| 185 |
'has_timezone_sql_conversion' => method_exists($metric, 'hasTimezoneSQLConversion') ? $metric->hasTimezoneSQLConversion() : false |
| 186 |
]) |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
return $data; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Create metric instance with all dependencies injected |
| 195 |
* |
| 196 |
* @param array $config |
| 197 |
* @return object |
| 198 |
*/ |
| 199 |
private function createMetric(array $config) |
| 200 |
{ |
| 201 |
$metric = new $config['class']( |
| 202 |
$config['start_date'], |
| 203 |
$config['end_date'], |
| 204 |
$config['type'], |
| 205 |
$config['options'] |
| 206 |
); |
| 207 |
|
| 208 |
// Inject dependencies in one go |
| 209 |
if (!empty($config['filters'])) { |
| 210 |
$metric->filters($config['filters']); |
| 211 |
} |
| 212 |
|
| 213 |
if (!empty($config['limit'])) { |
| 214 |
$metric->limit($config['limit']); |
| 215 |
} |
| 216 |
|
| 217 |
if (!empty($config['offset'])) { |
| 218 |
$metric->offset($config['offset']); |
| 219 |
} |
| 220 |
|
| 221 |
return $metric; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Get empty data structure for failed metrics |
| 226 |
* |
| 227 |
* @param string $type |
| 228 |
* @return mixed |
| 229 |
*/ |
| 230 |
private function getEmptyMetricData($type) |
| 231 |
{ |
| 232 |
return $type === 'count' ? 0 : []; |
| 233 |
} |
| 234 |
} |