| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.12 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 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 |
use FPFramework\Libs\Registry; |
| 20 |
use Firebox\Core\Admin\Analytics\Toolbar; |
| 21 |
|
| 22 |
class AnalyticsHelper |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Returns the data based on metrics, toolbar items and other data |
| 26 |
* |
| 27 |
* @param array $metrics |
| 28 |
* @param array $toolbar_data |
| 29 |
* @param array $other_data |
| 30 |
* |
| 31 |
* @return array |
| 32 |
*/ |
| 33 |
public static function getData($metrics = [], $toolbar_data = [], $other_data = []) |
| 34 |
{ |
| 35 |
if (!$metrics || !is_array($metrics)) |
| 36 |
{ |
| 37 |
return []; |
| 38 |
} |
| 39 |
|
| 40 |
$other_data = new Registry($other_data); |
| 41 |
|
| 42 |
$data = []; |
| 43 |
|
| 44 |
// create a Toolbar and set the selected toolbar items |
| 45 |
$toolbar = new Toolbar(null, $toolbar_data); |
| 46 |
|
| 47 |
$metricsObjects = new \FireBox\Core\Admin\Analytics\Metrics($toolbar); |
| 48 |
$metricsObjects = $metricsObjects->getMetricsData(); |
| 49 |
|
| 50 |
foreach ($metrics as $metric) |
| 51 |
{ |
| 52 |
$metricInstance = $metricsObjects[$metric]['metric']; |
| 53 |
|
| 54 |
// Metrics that require other metrics to generate data |
| 55 |
if ($metricInstance->getDataSource() === 'metrics') |
| 56 |
{ |
| 57 |
$found_metrics = array_intersect_key($metricsObjects, array_flip($metricInstance->getDataSourceInput())); |
| 58 |
|
| 59 |
if ($found_metrics) |
| 60 |
{ |
| 61 |
foreach ($found_metrics as &$_metric) |
| 62 |
{ |
| 63 |
$_metric = $_metric['metric']; |
| 64 |
} |
| 65 |
|
| 66 |
// Skip fill dates |
| 67 |
$metric_opts = $metricInstance->getOptionsRaw(); |
| 68 |
$metric_opts['skip_filling_dates'] = true; |
| 69 |
$metricInstance->setOptions($metric_opts); |
| 70 |
|
| 71 |
// Set custom metrics |
| 72 |
$metricInstance->setCustomMetrics($found_metrics); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Check if we were given extra query data that modify the base query and apply them. |
| 77 |
$metric_modify_query_data = $other_data->get('modify_query_data', []); |
| 78 |
if ($metric_modify_query_data) |
| 79 |
{ |
| 80 |
$metricInstance->getMetricData()->setModifyQueryData($metric_modify_query_data); |
| 81 |
} |
| 82 |
|
| 83 |
// Check if we have extra options to set for the Metric |
| 84 |
$metric_options = $other_data->get('options', []); |
| 85 |
if ($metric_options) |
| 86 |
{ |
| 87 |
$metricInstance->setOptions($metric_options); |
| 88 |
} |
| 89 |
|
| 90 |
$metric_data = $metricInstance->update()->getData(); |
| 91 |
|
| 92 |
// store them |
| 93 |
if (count($metrics) == 1) |
| 94 |
{ |
| 95 |
$data = $metric_data; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
$data[$metricInstance->getType()] = $metric_data; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $data; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Whether we can run an analytics page or not |
| 108 |
* |
| 109 |
* @param string $page |
| 110 |
* |
| 111 |
* @return boolean |
| 112 |
*/ |
| 113 |
public static function canRun($page = '') |
| 114 |
{ |
| 115 |
if (!$page) |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
// we must be on admin panel |
| 121 |
if (!is_admin()) |
| 122 |
{ |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
// ensure we have correct priviledges |
| 127 |
if (!is_user_logged_in() || !current_user_can('manage_options')) |
| 128 |
{ |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
// we must be on admin.php page |
| 133 |
global $pagenow; |
| 134 |
if ($pagenow !== 'admin.php') |
| 135 |
{ |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
// ensure we are on firebox dashboard |
| 140 |
$page = fpframework()->getPluginPage(); |
| 141 |
if (!$page || !in_array($page, [$page])) |
| 142 |
{ |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
} |