* @link https://www.fireplugins.com * @copyright Copyright © 2021 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core\Admin\Analytics; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } use FPFramework\Libs\Registry; class Metrics { /** * Default active metrics * * @var array */ const default_active_metrics = [ 'impressions' ]; /** * Toolbar * * @var array */ private $toolbar; /** * Analytics Metrics * * @var array */ private $metrics; /** * Metrics Data * * @var array */ private $metrics_data; /** * Metrics Constructor * * @param array $toolbar * @param array $metrics * * @return void */ public function __construct($toolbar = null, $metrics = null) { if (!$toolbar) { $toolbar = new Toolbar(); } $this->toolbar = $toolbar; if (!$metrics) { // initialize the metrics $metrics = $this->initializeMetrics(); } $this->metrics = $metrics; } /** * Init metrics * * @return void */ public function init() { // hook to render metrics add_action('firebox/analytics/metrics', [$this, 'renderMetrics']); } /** * Render metrics * * @return void */ public function renderMetrics() { firebox()->renderer->admin->render('analytics/metrics/tmpl'); } /** * Gets all metric data with chart-related data * * @return array */ public function getMetricsData() { if ($this->metrics_data) { return $this->metrics_data; } // set the metrics data $this->setMetricsData(); return $this->metrics_data; } /** * Returns the default active metrics * * @return array */ public function getDefaultActiveMetrics() { return self::default_active_metrics; } /** * Returns all metrics labels * * @return array */ public function getMetricsLabels() { $metrics = []; foreach ($this->metrics as $key => $metric) { if (!$metric instanceof Metric) { continue; } $metric->setBasicData(); $metrics[$key] = $metric->getCleanLabel(); } return $metrics; } /** * Creates and sets an array of each metric and their data * * @return void */ public function setMetricsData() { $compareLabels = $this->toolbar->getComparedLabelsFromSelectedToolbarItems(); // get all db-related metrics $extra_data = [ 'data_source' => [ 'db' ] ]; // get the metric data $data = Helpers\AnalyticsHelper::getData($this->getMetricsKeys(), $this->toolbar->getSelectedToolbarItems(), $extra_data); // loop all metrics that depend on other metrics to calculate their data foreach ($this->metrics as $key => $metric) { // skip non-metrics items if ($metric->getDataSource() != 'metrics') { continue; } // if the metric does not depend on any other metric, skip if (empty($metric->getDataSourceInput())) { continue; } // get all dependend metrics data $metrics_data = []; foreach ($metric->getDataSourceInput() as $_metric) { $metrics_data[$_metric] = $data[$_metric]; } // get the data from the metric $newdata = $metric->getMetricDataFromOtherMetrics($metrics_data); // merge them with the current data $data[$key] = array_merge_recursive($data[$key], $newdata); } $this->metrics_data = $data; } /** * Initialize all metrics items * * @return array */ private function initializeMetrics() { // get all Metric Namespace Files if (!$files = \scandir(__DIR__ . '/Metrics')) { return []; } $items = []; foreach ($files as $key => $file) { // ignore unimportant files if (in_array($file, ['index.php', '.', '..'])) { continue; } $file = basename($file, '.php'); // instantiate class $className = '\FireBox\Core\Admin\Analytics\Metrics\\' . $file; if (!class_exists($className)) { continue; } // init metric and fetch its data $metric = new $className($this->toolbar); // set metric data $items[strtolower($file)] = $metric; } return $items; } public function getMetricsKeys() { $keys = []; foreach ($this->metrics as $key => $metric) { $keys[] = $key; } return $keys; } /** * Return the metrics * * @return array */ public function getMetrics() { return $this->metrics; } }