diff --git a/src/controllers/GeneratorController.php b/src/controllers/GeneratorController.php index 6dd1518f..cd6cffe6 100644 --- a/src/controllers/GeneratorController.php +++ b/src/controllers/GeneratorController.php @@ -12,6 +12,7 @@ use craft\web\Controller; use craft\web\twig\variables\Paginate; use putyourlightson\blitz\Blitz; +use putyourlightson\blitz\helpers\QueryStringHelper; use putyourlightson\blitz\helpers\SiteUriHelper; use putyourlightson\blitz\records\CacheRecord; use Throwable; @@ -66,21 +67,16 @@ function(Event $event) { */ private function generateResponse(): ?Response { - // Remove the token query param. - $tokenParam = Craft::$app->getConfig()->getGeneral()->tokenParam; $queryParams = $this->request->getQueryParams(); - - if (isset($queryParams[$tokenParam])) { - unset($queryParams[$tokenParam]); - $this->request->setQueryParams($queryParams); - } + $validQueryParams = QueryStringHelper::getValidQueryParams($queryParams); + $this->request->setQueryParams($validQueryParams); /** * Update the query string to avoid the token being added to URLs. * * @see Paginate::getPageUrl() */ - $_SERVER['QUERY_STRING'] = http_build_query($queryParams); + $_SERVER['QUERY_STRING'] = http_build_query($validQueryParams); /** * Unset the token to avoid it being added to URLs. diff --git a/src/drivers/generators/BaseCacheGenerator.php b/src/drivers/generators/BaseCacheGenerator.php index 8d26d26d..8a6a93bf 100755 --- a/src/drivers/generators/BaseCacheGenerator.php +++ b/src/drivers/generators/BaseCacheGenerator.php @@ -11,6 +11,7 @@ use putyourlightson\blitz\Blitz; use putyourlightson\blitz\events\RefreshCacheEvent; use putyourlightson\blitz\helpers\CacheGeneratorHelper; +use putyourlightson\blitz\helpers\QueryStringHelper; use putyourlightson\blitz\helpers\SiteUriHelper; use putyourlightson\blitz\models\SiteUriModel; use putyourlightson\blitz\services\CacheRequestService; @@ -226,8 +227,13 @@ protected function getPageCount(array $siteUris): int protected function outputVerbose(string $url, bool $success = true): void { if (Craft::$app->getRequest()->getIsConsoleRequest() && $this->verbose) { - $tokenParam = Craft::$app->getConfig()->getGeneral()->tokenParam; - $url = preg_replace('/[?&]' . $tokenParam . '.*/', '', $url); + $queryString = parse_url($url, PHP_URL_QUERY) ?: ''; + if ($queryString) { + $validQueryStringParams = QueryStringHelper::getValidQueryStringParams($url); + $validQueryString = http_build_query($validQueryStringParams); + $url = str_replace($queryString, $validQueryString, $url); + $url = trim($url, '?'); + } if ($success) { Console::stdout($url . PHP_EOL); diff --git a/src/helpers/QueryStringHelper.php b/src/helpers/QueryStringHelper.php new file mode 100755 index 00000000..4ba5d3de --- /dev/null +++ b/src/helpers/QueryStringHelper.php @@ -0,0 +1,38 @@ +getConfig()->getGeneral(); + $invalidParams = [$generalConfig->pathParam, $generalConfig->tokenParam]; + foreach ($invalidParams as $invalidParam) { + if (isset($queryParams[$invalidParam])) { + unset($queryParams[$invalidParam]); + } + } + + return $queryParams; + } +} diff --git a/src/services/CacheRequestService.php b/src/services/CacheRequestService.php index 60e0538c..76df7acb 100755 --- a/src/services/CacheRequestService.php +++ b/src/services/CacheRequestService.php @@ -19,6 +19,7 @@ use putyourlightson\blitz\drivers\storage\BaseCacheStorage; use putyourlightson\blitz\enums\HeaderEnum; use putyourlightson\blitz\events\ResponseEvent; +use putyourlightson\blitz\helpers\QueryStringHelper; use putyourlightson\blitz\helpers\SiteUriHelper; use putyourlightson\blitz\models\SettingsModel; use putyourlightson\blitz\models\SiteUriModel; @@ -251,7 +252,7 @@ public function getIsCacheableSiteUri(?SiteUriModel $siteUri): bool } if (Blitz::$plugin->settings->queryStringCaching == SettingsModel::QUERY_STRINGS_DO_NOT_CACHE_URLS) { - $queryStringParams = $this->getQueryStringParamsWithoutToken($siteUri->uri); + $queryStringParams = QueryStringHelper::getValidQueryStringParams($siteUri->uri); if (!empty($queryStringParams)) { Blitz::$plugin->debug('Page not cached because a query string was provided with the query string caching setting disabled.', [], $url); @@ -398,12 +399,6 @@ public function getRequestedCacheableSiteUri(): ?SiteUriModel $site = Craft::$app->getSites()->getCurrentSite(); $uri = Craft::$app->getRequest()->getFullUri(); - $queryParams = Craft::$app->getRequest()->getQueryParams(); - $token = Craft::$app->getConfig()->getGeneral()->tokenParam; - if (isset($queryParams[$token])) { - unset($queryParams[$token]); - } - /** * Build the query string from the query params, so that [[Request::getQueryString()]] * doesn’t get called, which is determined from the `$_SERVER` global variable @@ -411,7 +406,9 @@ public function getRequestedCacheableSiteUri(): ?SiteUriModel * * @see Request::getQueryString() */ - $queryString = http_build_query($queryParams); + $queryParams = Craft::$app->getRequest()->getQueryParams(); + $validQueryParams = QueryStringHelper::getValidQueryParams($queryParams); + $queryString = http_build_query($validQueryParams); /** * Remove the base site path from the full URI @@ -562,22 +559,6 @@ public function matchesQueryStringParams(int $siteId, string $param, array|strin return false; } - /** - * Returns the query string params of the URI without the token param. - */ - public function getQueryStringParamsWithoutToken(string $uri): array - { - $queryString = parse_url($uri, PHP_URL_QUERY) ?: ''; - parse_str($queryString, $queryStringParams); - - $tokenParam = Craft::$app->getConfig()->getGeneral()->tokenParam; - if (isset($queryStringParams[$tokenParam])) { - unset($queryStringParams[$tokenParam]); - } - - return $queryStringParams; - } - /** * Returns the query string after processing the included and excluded query string params. */ @@ -587,7 +568,7 @@ public function getAllowedQueryString(int $siteId, string $uri): string return $this->allowedQueryStrings[$siteId][$uri]; } - $queryStringParams = $this->getQueryStringParamsWithoutToken($uri); + $queryStringParams = QueryStringHelper::getValidQueryStringParams($uri); if (!$this->getIsCachedInclude($uri)) { foreach ($queryStringParams as $key => $value) { diff --git a/src/variables/BlitzVariable.php b/src/variables/BlitzVariable.php index f118bebc..71c0d2c5 100644 --- a/src/variables/BlitzVariable.php +++ b/src/variables/BlitzVariable.php @@ -12,6 +12,7 @@ use putyourlightson\blitz\Blitz; use putyourlightson\blitz\helpers\DiagnosticsHelper; use putyourlightson\blitz\helpers\HintsHelper; +use putyourlightson\blitz\helpers\QueryStringHelper; use putyourlightson\blitz\models\CacheOptionsModel; use putyourlightson\blitz\models\VariableConfigModel; use putyourlightson\blitz\services\CacheRequestService; @@ -220,13 +221,9 @@ private function getUriWithParams(string $uri, array $params): string private function getQueryString(array $params): string { - // Remove the path param if it exists. - $pathParam = Craft::$app->getConfig()->getGeneral()->pathParam; - if ($pathParam && isset($params[$pathParam])) { - unset($params[$pathParam]); - } + $queryStringParams = QueryStringHelper::getValidQueryParams($params); - return http_build_query($params); + return http_build_query($queryStringParams); } /**