| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.6 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\Toolbar; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FireBox\Core\Admin\Analytics\ToolbarItem; |
| 20 |
use FireBox\Core\Admin\Analytics\ToolbarItemInterface; |
| 21 |
|
| 22 |
class Date extends ToolbarItem implements ToolbarItemInterface |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Toolbar Item Type |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $type = 'date'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Toolbar Popup Title |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $popup_title = 'FPF_DATE_RANGE'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Toolbar Popup Title Plural |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $popup_title_plural = 'FPF_DATES'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Total days we are showing |
| 47 |
* |
| 48 |
* @var int |
| 49 |
*/ |
| 50 |
protected $total_days; |
| 51 |
|
| 52 |
/** |
| 53 |
* Start date for the date range |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $start_date; |
| 58 |
|
| 59 |
/** |
| 60 |
* Whether we are fetching data for a single day. |
| 61 |
* |
| 62 |
* @var boolean |
| 63 |
*/ |
| 64 |
protected $single_day; |
| 65 |
|
| 66 |
/** |
| 67 |
* Holds the query data needed to return box log data for this type |
| 68 |
* |
| 69 |
* @param array $current_query_data |
| 70 |
* @param array $payload |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function getQueryData($metric, $current_query_data, $payload) |
| 75 |
{ |
| 76 |
parent::getQueryData($metric, $current_query_data, $payload); |
| 77 |
|
| 78 |
$this->single_day = false; |
| 79 |
|
| 80 |
$data = [ |
| 81 |
'select' => ['count(bl.id) as total', 'DATE_FORMAT(bl.date, "%d/%m/%Y") as label'], |
| 82 |
'from_table_name_as' => ' as bl', |
| 83 |
'where' => $this->getWhere($payload), |
| 84 |
'groupby' => 'date(bl.date)', |
| 85 |
'orderby' => 'bl.date ASC' |
| 86 |
]; |
| 87 |
|
| 88 |
$data = $this->filterTodayData($payload, $data); |
| 89 |
|
| 90 |
// if we received a metric, then run its own query data filtering |
| 91 |
if ($metric) |
| 92 |
{ |
| 93 |
$data = apply_filters('firebox/analytics/metric/' . $metric->getType() . '/filter_date_query_data', $data); |
| 94 |
} |
| 95 |
|
| 96 |
return $data; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Filter the query for `Today` data if we are searching for data today. |
| 101 |
* |
| 102 |
* @param array $payload |
| 103 |
* @param array $data |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
private function filterTodayData($payload, $data) |
| 108 |
{ |
| 109 |
if (!isset($payload['options_key']) && !isset($payload['method']) && !is_array($data)) |
| 110 |
{ |
| 111 |
return []; |
| 112 |
} |
| 113 |
|
| 114 |
if ($payload['options_key'] != 'today') |
| 115 |
{ |
| 116 |
return $data; |
| 117 |
} |
| 118 |
|
| 119 |
$date_selection = 'CONCAT(DATE_FORMAT(bl.date, \'%H\'), \':00\')'; |
| 120 |
|
| 121 |
$data['select'] = [ |
| 122 |
$date_selection . ' as label', |
| 123 |
'count(bl.id) as total' |
| 124 |
]; |
| 125 |
$data['groupby'] = $date_selection; |
| 126 |
|
| 127 |
return $data; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Parses the results of the dates by setting timezone if viewing "today" data. |
| 134 |
* |
| 135 |
* @param string $period |
| 136 |
* @param array $dates |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function parseResults($period, &$dates) |
| 141 |
{ |
| 142 |
if (!$dates) |
| 143 |
{ |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
// When viewing "Today" results, we need only the hour:minutes |
| 148 |
if ($period === 'today') |
| 149 |
{ |
| 150 |
$offset = get_option('gmt_offset'); |
| 151 |
$offset = $offset > 0 ? '+' . $offset : $offset; |
| 152 |
$tz = new \DateTimeZone($offset); |
| 153 |
|
| 154 |
foreach ($dates as $key => &$value) |
| 155 |
{ |
| 156 |
$date = (new \DateTime($value->label))->setTimezone($tz); |
| 157 |
$value->label = $date->format('H:00'); |
| 158 |
|
| 159 |
} |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
// Other periods display the full date |
| 164 |
foreach ($dates as $key => &$value) |
| 165 |
{ |
| 166 |
$value->label = (new \DateTime($value->label))->format('Y-m-d'); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns the WHERE for "filter" |
| 172 |
* |
| 173 |
* @param array $payload |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
protected function getFilterWhere($payload) |
| 178 |
{ |
| 179 |
if (!$payload || !is_array($payload)) |
| 180 |
{ |
| 181 |
return []; |
| 182 |
} |
| 183 |
|
| 184 |
if (!isset($payload['options_key']) || !isset($payload['value']) || !is_string($payload['options_key'])) |
| 185 |
{ |
| 186 |
return []; |
| 187 |
} |
| 188 |
|
| 189 |
$where = []; |
| 190 |
|
| 191 |
$currDate = $this->calculateSQLCurrentDate(); |
| 192 |
|
| 193 |
$current_day = date('d'); |
| 194 |
$year = date('Y'); |
| 195 |
|
| 196 |
switch ($payload['options_key']) |
| 197 |
{ |
| 198 |
|
| 199 |
|
| 200 |
case 'this_month': |
| 201 |
default: |
| 202 |
$where = [ |
| 203 |
'MONTH(bl.date)' => ' = MONTH(CURRENT_DATE())', |
| 204 |
'YEAR(bl.date)' => ' = YEAR(CURRENT_DATE())' |
| 205 |
]; |
| 206 |
|
| 207 |
$month = date('m'); |
| 208 |
$this->total_days = cal_days_in_month(CAL_GREGORIAN, $month, $year); |
| 209 |
$this->start_date = '01/' . $month . '/' . $year; |
| 210 |
break; |
| 211 |
|
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
return $where; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns the WHERE for "compare" |
| 220 |
* |
| 221 |
* @param array $payload |
| 222 |
* |
| 223 |
* @return array |
| 224 |
*/ |
| 225 |
protected function getCompareWhere($payload) |
| 226 |
{ |
| 227 |
$where = []; |
| 228 |
|
| 229 |
$current_day = date('d'); |
| 230 |
$year = date('Y'); |
| 231 |
|
| 232 |
switch ($payload['options_key']) |
| 233 |
{ |
| 234 |
|
| 235 |
|
| 236 |
// Compare This Month |
| 237 |
case 'compare_this_month': |
| 238 |
case 'compare_this_month_yoy': |
| 239 |
default: |
| 240 |
$where = [ |
| 241 |
'MONTH(bl.date)' => ' = MONTH(CURRENT_DATE())', |
| 242 |
'YEAR(bl.date)' => ' = YEAR(CURRENT_DATE())' |
| 243 |
]; |
| 244 |
|
| 245 |
$month = date('m'); |
| 246 |
$this->total_days = cal_days_in_month(CAL_GREGORIAN, $month, $year); |
| 247 |
$this->start_date = '01/' . $month . '/' . $year; |
| 248 |
break; |
| 249 |
|
| 250 |
case 'compare_this_month_prev_period': |
| 251 |
$currDate = $this->getSQLCurrentDateOrWithMinusInterval1Month(); |
| 252 |
|
| 253 |
$where = [ |
| 254 |
'MONTH(bl.date)' => ' = MONTH(' . $currDate . ')', |
| 255 |
'YEAR(bl.date)' => ' = YEAR(' . $currDate . ')' |
| 256 |
]; |
| 257 |
|
| 258 |
$month = date('m', strtotime('-1 month')); |
| 259 |
$this->total_days = cal_days_in_month(CAL_GREGORIAN, $month, $year); |
| 260 |
$this->start_date = '01/' . $month . '/' . $year; |
| 261 |
break; |
| 262 |
|
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
return $where; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Sets the Custom WHERE for filter/compare |
| 271 |
* |
| 272 |
* @param string $start_date |
| 273 |
* @param string $end_date |
| 274 |
* @param string $where |
| 275 |
* |
| 276 |
* @return void |
| 277 |
*/ |
| 278 |
private function setCustomWhere($start_date, $end_date, &$where) |
| 279 |
{ |
| 280 |
global $wpdb; |
| 281 |
|
| 282 |
$where['DATE(bl.date)'] = ' BETWEEN ' . $wpdb->prepare('%s', $start_date) . ' AND ' . $wpdb->prepare('%s', $end_date); |
| 283 |
|
| 284 |
// find diference between the 2 dates |
| 285 |
if (strtotime($start_date) !== false) |
| 286 |
{ |
| 287 |
$start = new \DateTime($start_date); |
| 288 |
$end = new \DateTime($end_date); |
| 289 |
$this->total_days = $end->diff($start)->days; |
| 290 |
$this->start_date = $start->format('d/m/Y'); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Sets ORDER BY for this type |
| 296 |
* |
| 297 |
* @param array $data |
| 298 |
* @param array $payload |
| 299 |
* |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
private function setupOrderBy(&$data, $payload) |
| 303 |
{ |
| 304 |
if ($payload['options_key'] == 'last_7_days') |
| 305 |
{ |
| 306 |
$data['orderby'] = 'bl.date ASC'; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* Returns the date toolbar object that retrieve previous period data |
| 314 |
* |
| 315 |
* @param array $date_toolbar_item |
| 316 |
* |
| 317 |
* @return array |
| 318 |
*/ |
| 319 |
public function getPreviousPeriodDateObject($date_toolbar_item) |
| 320 |
{ |
| 321 |
if (!$date_toolbar_item) |
| 322 |
{ |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
$current_period = $date_toolbar_item['options_key']; |
| 327 |
|
| 328 |
$previous_period = $current_period; |
| 329 |
$value = ''; |
| 330 |
$method = 'compare'; |
| 331 |
|
| 332 |
switch ($current_period) |
| 333 |
{ |
| 334 |
case 'today': |
| 335 |
case 'yesterday': |
| 336 |
$previous_period = 'yesterday'; |
| 337 |
$method = 'filter'; |
| 338 |
break; |
| 339 |
|
| 340 |
case 'last_7_days': |
| 341 |
case 'compare_last_7_days': |
| 342 |
$previous_period = 'compare_last_7_days_prev_period'; |
| 343 |
break; |
| 344 |
|
| 345 |
case 'compare_last_7_days_yoy': |
| 346 |
$previous_period = 'compare_last_7_days_yoy_prev_period'; |
| 347 |
break; |
| 348 |
|
| 349 |
case 'this_month': |
| 350 |
case 'compare_this_month': |
| 351 |
$previous_period = 'compare_this_month_prev_period'; |
| 352 |
break; |
| 353 |
|
| 354 |
case 'compare_this_month_yoy': |
| 355 |
$previous_period = 'compare_this_month_yoy_prev_period'; |
| 356 |
break; |
| 357 |
|
| 358 |
case 'last_3_months': |
| 359 |
$previous_period = 'compare_last_3_months'; |
| 360 |
break; |
| 361 |
case 'compare_last_3_months': |
| 362 |
$previous_period = 'compare_last_3_months_prev_period'; |
| 363 |
break; |
| 364 |
|
| 365 |
case 'compare_last_3_months_yoy': |
| 366 |
$previous_period = 'compare_last_3_months_yoy_prev_period'; |
| 367 |
break; |
| 368 |
|
| 369 |
case 'last_6_months': |
| 370 |
$previous_period = 'compare_last_6_months'; |
| 371 |
break; |
| 372 |
case 'compare_last_6_months': |
| 373 |
$previous_period = 'compare_last_6_months_prev_period'; |
| 374 |
break; |
| 375 |
|
| 376 |
case 'last_12_months': |
| 377 |
$previous_period = 'compare_last_12_months'; |
| 378 |
break; |
| 379 |
case 'compare_last_12_months': |
| 380 |
$previous_period = 'compare_last_12_months_prev_period'; |
| 381 |
break; |
| 382 |
|
| 383 |
case 'last_16_months': |
| 384 |
$previous_period = 'compare_last_16_months'; |
| 385 |
break; |
| 386 |
case 'compare_last_16_months': |
| 387 |
$previous_period = 'compare_last_16_months_prev_period'; |
| 388 |
break; |
| 389 |
|
| 390 |
case 'custom': |
| 391 |
$method = isset($date_toolbar_item['method']) ? $date_toolbar_item['method'] : 'filter'; |
| 392 |
$previous_period = 'custom_prev_period'; |
| 393 |
break; |
| 394 |
} |
| 395 |
|
| 396 |
$date_toolbar_item['options_key'] = $previous_period; |
| 397 |
$date_toolbar_item['method'] = $method; |
| 398 |
|
| 399 |
return $date_toolbar_item; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Retrieves the toolbar item value in filter mode |
| 404 |
* |
| 405 |
* @param int $value |
| 406 |
* |
| 407 |
* @return array |
| 408 |
*/ |
| 409 |
public function getFilterToolbarItemWithValue($value) |
| 410 |
{ |
| 411 |
return [ |
| 412 |
'method' => 'filter', |
| 413 |
'options_key' => $value, |
| 414 |
]; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public function getOptionsKey() |
| 420 |
{ |
| 421 |
return $this->selectedToolbarItems['date']['options_key']; |
| 422 |
} |
| 423 |
|
| 424 |
public function getTotalDays() |
| 425 |
{ |
| 426 |
return $this->total_days; |
| 427 |
} |
| 428 |
|
| 429 |
public function getStartDate() |
| 430 |
{ |
| 431 |
return $this->start_date; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
} |