| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.0 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; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
|
| 21 |
class Metrics |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Default active metrics |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
const default_active_metrics = [ |
| 29 |
'impressions' |
| 30 |
]; |
| 31 |
|
| 32 |
/** |
| 33 |
* Toolbar |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
private $toolbar; |
| 38 |
|
| 39 |
/** |
| 40 |
* Analytics Metrics |
| 41 |
* |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
private $metrics; |
| 45 |
|
| 46 |
/** |
| 47 |
* Metrics Data |
| 48 |
* |
| 49 |
* @var array |
| 50 |
*/ |
| 51 |
private $metrics_data; |
| 52 |
|
| 53 |
/** |
| 54 |
* Metrics Constructor |
| 55 |
* |
| 56 |
* @param array $toolbar |
| 57 |
* @param array $metrics |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function __construct($toolbar = null, $metrics = null) |
| 62 |
{ |
| 63 |
if (!$toolbar) |
| 64 |
{ |
| 65 |
$toolbar = new Toolbar(); |
| 66 |
} |
| 67 |
$this->toolbar = $toolbar; |
| 68 |
|
| 69 |
if (!$metrics) |
| 70 |
{ |
| 71 |
// initialize the metrics |
| 72 |
$metrics = $this->initializeMetrics(); |
| 73 |
} |
| 74 |
$this->metrics = $metrics; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Init metrics |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function init() |
| 83 |
{ |
| 84 |
// hook to render metrics |
| 85 |
add_action('firebox/analytics/metrics', [$this, 'renderMetrics']); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Render metrics |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function renderMetrics() |
| 94 |
{ |
| 95 |
firebox()->renderer->admin->render('analytics/metrics/tmpl'); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Gets all metric data with chart-related data |
| 100 |
* |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function getMetricsData() |
| 104 |
{ |
| 105 |
if ($this->metrics_data) |
| 106 |
{ |
| 107 |
return $this->metrics_data; |
| 108 |
} |
| 109 |
|
| 110 |
// set the metrics data |
| 111 |
$this->setMetricsData(); |
| 112 |
|
| 113 |
return $this->metrics_data; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the default active metrics |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function getDefaultActiveMetrics() |
| 122 |
{ |
| 123 |
return self::default_active_metrics; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns all metrics labels |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function getMetricsLabels() |
| 132 |
{ |
| 133 |
$metrics = []; |
| 134 |
|
| 135 |
foreach ($this->metrics as $key => $metric) |
| 136 |
{ |
| 137 |
if (!$metric instanceof Metric) |
| 138 |
{ |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
$metric->setBasicData(); |
| 143 |
|
| 144 |
$metrics[$key] = $metric->getCleanLabel(); |
| 145 |
} |
| 146 |
|
| 147 |
return $metrics; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Creates and sets an array of each metric and their data |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public function setMetricsData() |
| 156 |
{ |
| 157 |
$compareLabels = $this->toolbar->getComparedLabelsFromSelectedToolbarItems(); |
| 158 |
|
| 159 |
// get all db-related metrics |
| 160 |
$extra_data = [ |
| 161 |
'data_source' => [ 'db' ] |
| 162 |
]; |
| 163 |
|
| 164 |
// get the metric data |
| 165 |
$data = Helpers\AnalyticsHelper::getData($this->getMetricsKeys(), $this->toolbar->getSelectedToolbarItems(), $extra_data); |
| 166 |
|
| 167 |
// loop all metrics that depend on other metrics to calculate their data |
| 168 |
foreach ($this->metrics as $key => $metric) |
| 169 |
{ |
| 170 |
// skip non-metrics items |
| 171 |
if ($metric->getDataSource() != 'metrics') |
| 172 |
{ |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
// if the metric does not depend on any other metric, skip |
| 177 |
if (empty($metric->getDataSourceInput())) |
| 178 |
{ |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
// get all dependend metrics data |
| 183 |
$metrics_data = []; |
| 184 |
foreach ($metric->getDataSourceInput() as $_metric) |
| 185 |
{ |
| 186 |
$metrics_data[$_metric] = $data[$_metric]; |
| 187 |
} |
| 188 |
|
| 189 |
// get the data from the metric |
| 190 |
$newdata = $metric->getMetricDataFromOtherMetrics($metrics_data); |
| 191 |
|
| 192 |
// merge them with the current data |
| 193 |
$data[$key] = array_merge_recursive($data[$key], $newdata); |
| 194 |
} |
| 195 |
|
| 196 |
$this->metrics_data = $data; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Initialize all metrics items |
| 201 |
* |
| 202 |
* @return array |
| 203 |
*/ |
| 204 |
private function initializeMetrics() |
| 205 |
{ |
| 206 |
// get all Metric Namespace Files |
| 207 |
if (!$files = \scandir(__DIR__ . '/Metrics')) |
| 208 |
{ |
| 209 |
return []; |
| 210 |
} |
| 211 |
|
| 212 |
$items = []; |
| 213 |
|
| 214 |
foreach ($files as $key => $file) |
| 215 |
{ |
| 216 |
// ignore unimportant files |
| 217 |
if (in_array($file, ['index.php', '.', '..'])) |
| 218 |
{ |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
$file = basename($file, '.php'); |
| 223 |
|
| 224 |
// instantiate class |
| 225 |
$className = '\FireBox\Core\Admin\Analytics\Metrics\\' . $file; |
| 226 |
|
| 227 |
if (!class_exists($className)) |
| 228 |
{ |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
// init metric and fetch its data |
| 233 |
$metric = new $className($this->toolbar); |
| 234 |
|
| 235 |
// set metric data |
| 236 |
$items[strtolower($file)] = $metric; |
| 237 |
} |
| 238 |
|
| 239 |
return $items; |
| 240 |
} |
| 241 |
|
| 242 |
public function getMetricsKeys() |
| 243 |
{ |
| 244 |
$keys = []; |
| 245 |
|
| 246 |
foreach ($this->metrics as $key => $metric) |
| 247 |
{ |
| 248 |
$keys[] = $key; |
| 249 |
} |
| 250 |
|
| 251 |
return $keys; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Return the metrics |
| 256 |
* |
| 257 |
* @return array |
| 258 |
*/ |
| 259 |
public function getMetrics() |
| 260 |
{ |
| 261 |
return $this->metrics; |
| 262 |
} |
| 263 |
} |