| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 3.1.13 Free | |
| 4 | + * @version 2.1.39 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2025 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core\Analytics; |
| @@ -18,18 +18,39 @@ | ||
| 18 | 18 | |
| 19 | 19 | class Data |
| 20 | 20 | { |
| 21 | 21 | private $start_date = null; |
| 22 | + | |
| 22 | 23 | private $end_date = null; |
| 24 | + | |
| 23 | 25 | private $metrics = []; |
| 26 | + | |
| 24 | 27 | private $filters = []; |
| 28 | + | |
| 25 | 29 | private $offset = null; |
| 30 | + | |
| 26 | 31 | private $limit = null; |
| 32 | + | |
| 27 | 33 | protected $options = []; |
| 28 | 34 | |
| 29 | 35 | public function __construct($start_date = '', $end_date = '', $options = []) |
| 30 | 36 | { |
| 31 | - Helpers\Date::transformStartEndDateToUTC($start_date, $end_date); | |
| 37 | + $utcTimeZone = new \DateTimeZone('UTC'); | |
| 38 | + $tz = new \DateTimeZone(wp_timezone()->getName()); | |
| 39 | + | |
| 40 | + // Make start in UTC | |
| 41 | + if ($start_date_obj = \DateTime::createFromFormat('Y/m/d H:i:s', $start_date, $tz)) | |
| 42 | + { | |
| 43 | + $start_date_obj->setTimezone($utcTimeZone); | |
| 44 | + $start_date = $start_date_obj->format('Y/m/d H:i:s'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Make end_date in UTC | |
| 48 | + if ($end_date_obj = \DateTime::createFromFormat('Y/m/d H:i:s', $end_date, $tz)) | |
| 49 | + { | |
| 50 | + $end_date_obj->setTimezone($utcTimeZone); | |
| 51 | + $end_date = $end_date_obj->format('Y/m/d H:i:s'); | |
| 52 | + } | |
| 32 | 53 | |
| 33 | 54 | $this->start_date = $start_date; |
| 34 | 55 | $this->end_date = $end_date; |
| 35 | 56 | $this->options = $options; |
| @@ -35,200 +56,71 @@ | ||
| 35 | 56 | $this->options = $options; |
| 36 | 57 | } |
| 37 | 58 | |
| 38 | 59 | /** |
| 39 | - * Fluent interface for building analytics queries | |
| 60 | + * Allowed metrics: | |
| 40 | 61 | * |
| 41 | - * @param array $metrics Array of metric slugs | |
| 42 | - * @return self | |
| 62 | + * [ | |
| 63 | + * 'impressions', | |
| 64 | + * 'submissions', | |
| 65 | + * 'conversionrate' | |
| 66 | + * ] | |
| 43 | 67 | */ |
| 44 | - public function metrics(array $metrics) | |
| 68 | + public function setMetrics($metrics = []) | |
| 45 | 69 | { |
| 46 | 70 | $this->metrics = $metrics; |
| 47 | - return $this; | |
| 48 | 71 | } |
| 49 | 72 | |
| 50 | - /** | |
| 51 | - * Fluent interface for adding filters | |
| 52 | - * | |
| 53 | - * @param array $filters | |
| 54 | - * @return self | |
| 55 | - */ | |
| 56 | - public function filters(array $filters) | |
| 73 | + public function setFilters($filters = []) | |
| 57 | 74 | { |
| 58 | 75 | $this->filters = $filters; |
| 59 | - return $this; | |
| 60 | 76 | } |
| 61 | 77 | |
| 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) | |
| 78 | + public function setOffset($offset = null) | |
| 70 | 79 | { |
| 71 | - $this->limit = (int) $limit; | |
| 72 | 80 | $this->offset = (int) $offset; |
| 73 | - return $this; | |
| 74 | 81 | } |
| 75 | 82 | |
| 76 | - /** | |
| 77 | - * Fluent interface for setting limit | |
| 78 | - * | |
| 79 | - * @param int|null $limit | |
| 80 | - * @return self | |
| 81 | - */ | |
| 82 | - public function limit($limit) | |
| 83 | + public function setLimit($limit = null) | |
| 83 | 84 | { |
| 84 | - $this->limit = $limit !== null ? (int) $limit : null; | |
| 85 | - return $this; | |
| 85 | + $this->limit = (int) $limit; | |
| 86 | 86 | } |
| 87 | 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 | 88 | public function getData($type = 'list') |
| 107 | 89 | { |
| 108 | - if (empty($this->metrics)) { | |
| 109 | - return []; | |
| 110 | - } | |
| 90 | + $data = array_fill_keys($this->metrics, []); | |
| 111 | 91 | |
| 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); | |
| 92 | + foreach ($data as $metric_slug => &$metric_data) | |
| 93 | + { | |
| 94 | + // Validate the given metric name and abort if unknown | |
| 95 | + if (!$class_name = \FireBox\Core\Analytics\Helpers\Metrics::getClassFromSlug($metric_slug)) | |
| 96 | + { | |
| 97 | + unset($data[$metric_slug]); | |
| 98 | + continue; | |
| 120 | 99 | } |
| 121 | - } | |
| 122 | 100 | |
| 123 | - return $data; | |
| 124 | - } | |
| 101 | + $class = '\FireBox\Core\Analytics\Metrics\\' . $class_name; | |
| 102 | + $class = new $class($this->start_date, $this->end_date, $type, $this->options); | |
| 125 | 103 | |
| 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 = []; | |
| 104 | + if ($this->filters) | |
| 105 | + { | |
| 106 | + $class->setFilters($this->filters); | |
| 107 | + } | |
| 135 | 108 | |
| 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; | |
| 109 | + if ($this->offset) | |
| 110 | + { | |
| 111 | + $class->setOffset($this->offset); | |
| 141 | 112 | } |
| 142 | 113 | |
| 143 | - $class_path = '\FireBox\Core\Analytics\Metrics\\' . $class_name; | |
| 144 | - | |
| 145 | - if (!class_exists($class_path)) { | |
| 146 | - continue; | |
| 114 | + if ($this->limit) | |
| 115 | + { | |
| 116 | + $class->setLimit($this->limit); | |
| 147 | 117 | } |
| 148 | 118 | |
| 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 | - ]; | |
| 119 | + $metric_data = $class->getData(); | |
| 120 | + | |
| 121 | + $class->onAfterGetData($metric_data); | |
| 159 | 122 | } |
| 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 | 123 | |
| 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 | 124 | 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 | 125 | } |
| 234 | 126 | } |