diff options
author | Peter Zhang <peter@innocraft.com> | 2022-02-14 14:01:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-14 14:01:16 +0300 |
commit | 65383e52a22cb5d1d5e1497f646a27d21c780034 (patch) | |
tree | ad6c239d5c103678d153c40c09676175ba2b0288 | |
parent | 48020c27e137c7e9ce9db6c20794cdfd2ddefa8b (diff) |
[Hotfix]add date, period params into common function (#18686)
* Update Common.php
add array check in common request
* Update Common.php
test
* add common period and date function
add common period and date function
* Update core/Piwik.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* Update core/Piwik.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* update function
update function
* Update core/Piwik.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* Update Controller.php
update isset check
* Update core/Plugin/Controller.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* Update core/Piwik.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* update isset check
update isset check
* Update Piwik.php
update phpdocs
* Update core/Visualization/Sparkline.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* Update Controller.php
pass param
* Update core/Plugin/Controller.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
Co-authored-by: Stefan Giehl <stefan@matomo.org>
-rw-r--r-- | core/Piwik.php | 30 | ||||
-rw-r--r-- | core/Plugin/Controller.php | 25 | ||||
-rw-r--r-- | plugins/MultiSites/Controller.php | 11 | ||||
-rw-r--r-- | plugins/VisitsSummary/Reports/Get.php | 2 |
4 files changed, 44 insertions, 24 deletions
diff --git a/core/Piwik.php b/core/Piwik.php index e0c25d81d3..0c9c1ff66a 100644 --- a/core/Piwik.php +++ b/core/Piwik.php @@ -185,7 +185,7 @@ class Piwik $user = APIUsersManager::getInstance()->getUser(Piwik::getCurrentUserLogin()); return $user['date_registered'] ?? ''; } - + /** * Returns the current user's Last Seen. * @@ -889,4 +889,32 @@ class Piwik return $translator->translate($translationId, $args, $language); } + + /** + * Returns the period provided in the current request. + * If no $default is provided, this method will throw an Exception if `period` can't be found in the request + * + * @param string|null $default default value to use + * @throws Exception + * @return string + * @api + */ + public static function getPeriod($default = null) + { + return Common::getRequestVar('period', $default, 'string'); + } + + /** + * Returns the date provided in the current request. + * If no $default is provided, this method will throw an Exception if `date` can't be found in the request + * + * @param string|null $default default value to use + * @throws Exception + * @return string + * @api + */ + public static function getDate($default = null) + { + return Common::getRequestVar('date', $default, 'string'); + } } diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php index ca2f169895..2d0093912a 100644 --- a/core/Plugin/Controller.php +++ b/core/Plugin/Controller.php @@ -476,25 +476,14 @@ abstract class Controller */ protected function getGraphParamsModified($paramsToSet = array()) { - if (!isset($paramsToSet['period'])) { - $period = Common::getRequestVar('period'); - } else { - $period = $paramsToSet['period']; - } + $period = $paramsToSet['period'] ?? Piwik::getPeriod(); + if ($period === 'range') { return $paramsToSet; } - if (!isset($paramsToSet['range'])) { - $range = 'last30'; - } else { - $range = $paramsToSet['range']; - } - if (!isset($paramsToSet['date'])) { - $endDate = $this->strDate; - } else { - $endDate = $paramsToSet['date']; - } + $range = isset($paramsToSet['range']) ? $paramsToSet['range'] : 'last30'; + $endDate = isset($paramsToSet['date']) ? $paramsToSet['date'] : $this->strDate; if (is_null($this->site)) { throw new NoAccessException("Website not initialized, check that you are logged in and/or using the correct token_auth."); @@ -640,10 +629,10 @@ abstract class Controller $maxDate = Date::factory('now', $siteTimezone); $this->setMaxDateView($maxDate, $view); - $rawDate = Common::getRequestVar('date'); + $rawDate = Piwik::getDate(); Period::checkDateFormat($rawDate); - $periodStr = Common::getRequestVar('period'); + $periodStr = Piwik::getPeriod(); if ($periodStr !== 'range') { $date = Date::factory($this->strDate); @@ -923,7 +912,7 @@ abstract class Controller $periodValidator = new PeriodValidator(); - $currentPeriod = Common::getRequestVar('period'); + $currentPeriod = Piwik::getPeriod(); $availablePeriods = $periodValidator->getPeriodsAllowedForUI(); if (! $periodValidator->isPeriodAllowedForUI($currentPeriod)) { diff --git a/plugins/MultiSites/Controller.php b/plugins/MultiSites/Controller.php index 86792958f6..715c59a732 100644 --- a/plugins/MultiSites/Controller.php +++ b/plugins/MultiSites/Controller.php @@ -70,12 +70,15 @@ class Controller extends \Piwik\Plugin\Controller return json_encode($response); } + /** + * @throws \Piwik\NoAccessException + */ public function getSitesInfo($isWidgetized = false) { Piwik::checkUserHasSomeViewAccess(); - $date = Common::getRequestVar('date', 'today'); - $period = Common::getRequestVar('period', 'day'); + $date = Piwik::getDate('today'); + $period = Piwik::getPeriod('day'); $view = new View("@MultiSites/getSitesInfo"); @@ -93,8 +96,8 @@ class Controller extends \Piwik\Plugin\Controller ) { $view->autoRefreshTodayReport = Config::getInstance()->General['multisites_refresh_after_seconds']; } - - $params = $this->getGraphParamsModified(); + $paramsToSet = ['period' => $period, 'date' => $date]; + $params = $this->getGraphParamsModified($paramsToSet); $view->dateSparkline = $period == 'range' ? $date : $params['date']; $this->setGeneralVariablesView($view); diff --git a/plugins/VisitsSummary/Reports/Get.php b/plugins/VisitsSummary/Reports/Get.php index 8ffcac2d9f..3c5f703ec1 100644 --- a/plugins/VisitsSummary/Reports/Get.php +++ b/plugins/VisitsSummary/Reports/Get.php @@ -49,7 +49,7 @@ class Get extends \Piwik\Plugin\Report 'max_actions' ); - $period = Common::getRequestVar('period', 'day'); + $period = Piwik::getPeriod('day'); if (SettingsPiwik::isUniqueVisitorsEnabled($period)) { $this->metrics = array_merge(['nb_uniq_visitors'], $this->metrics); } |