Skip to content

Commit

Permalink
Add helper methods to return valid query params
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Nov 13, 2024
1 parent 6cfc369 commit 328244c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
12 changes: 4 additions & 8 deletions src/controllers/GeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions src/drivers/generators/BaseCacheGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions src/helpers/QueryStringHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/

namespace putyourlightson\blitz\helpers;

use Craft;

class QueryStringHelper
{
/**
* Returns the valid query string params of the URI.
*/
public static function getValidQueryStringParams(string $uri): array
{
$queryString = parse_url($uri, PHP_URL_QUERY) ?: '';
parse_str($queryString, $queryParams);

return static::getValidQueryParams($queryParams);
}

/**
* Returns only valid query params.
*/
public static function getValidQueryParams(array $queryParams): array
{
$generalConfig = Craft::$app->getConfig()->getGeneral();
$invalidParams = [$generalConfig->pathParam, $generalConfig->tokenParam];
foreach ($invalidParams as $invalidParam) {
if (isset($queryParams[$invalidParam])) {
unset($queryParams[$invalidParam]);
}
}

return $queryParams;
}
}
31 changes: 6 additions & 25 deletions src/services/CacheRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -398,20 +399,16 @@ 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
* and which breaks our Pest tests.
*
* @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
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions src/variables/BlitzVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 328244c

Please sign in to comment.