From e976aa0f5f33a27974473d545480542ef5e16eff Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 16 Jan 2019 14:21:56 -0700 Subject: [PATCH] Fixed some issues with Medium querystring + timestamps --- CHANGELOG.md | 2 + .../Grav/Common/Page/Medium/ImageMedium.php | 4 +- system/src/Grav/Common/Page/Medium/Medium.php | 56 +++++++++++++------ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c2dce6c4..b9d09c447 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ 1. [](#bugfix) * Fixed environment getting port added [#2284](https://github.com/getgrav/grav/issues/2284) * Fixed `FlexForm::updateObject()` to update array values when they are empty in the form + * Fixed some issues related to Medium objects losing query string attributes + * Broke out Medium timestamp so it's not cleared on `reset()s` # v1.6.0-beta.7 ## 12/14/2018 diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 72f23b4fd..030e4b619 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -185,7 +185,7 @@ class ImageMedium extends Medium $this->reset(); } - return trim(Grav::instance()['base_url'] . '/' . ltrim($output . $this->querystring() . $this->urlHash(), '/'), '\\'); + return trim(Grav::instance()['base_url'] . '/' . $this->urlQuerystring($output), '\\'); } /** @@ -358,7 +358,7 @@ class ImageMedium extends Medium if ($this->image) { $this->image(); - $this->querystring(''); + $this->medium_querystring = []; $this->filter(); $this->clearAlternatives(); } diff --git a/system/src/Grav/Common/Page/Medium/Medium.php b/system/src/Grav/Common/Page/Medium/Medium.php index 0b8738120..5ed616700 100644 --- a/system/src/Grav/Common/Page/Medium/Medium.php +++ b/system/src/Grav/Common/Page/Medium/Medium.php @@ -13,6 +13,7 @@ use Grav\Common\Grav; use Grav\Common\Data\Data; use Grav\Common\Data\Blueprint; use Grav\Common\Media\Interfaces\MediaObjectInterface; +use Grav\Common\Utils; class Medium extends Data implements RenderableInterface, MediaObjectInterface { @@ -55,6 +56,13 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface */ protected $metadata = []; + /** + * @var array + */ + protected $medium_querystring = []; + + protected $timestamp; + /** * Construct. * @@ -66,7 +74,7 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface parent::__construct($items, $blueprint); if (Grav::instance()['config']->get('system.media.enable_media_timestamp', true)) { - $this->querystring('&' . Grav::instance()['cache']->getKey()); + $this->timestamp = Grav::instance()['cache']->getKey(); } $this->def('mime', 'application/octet-stream'); @@ -136,7 +144,7 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface */ public function setTimestamp($timestamp = null) { - $this->set('querystring', (string)($timestamp ?? $this->modified())); + $this->timestamp = (string)($timestamp ?? $this->modified()); return $this; } @@ -246,7 +254,7 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface $this->reset(); } - return trim(Grav::instance()['base_url'] . '/' . ltrim($output . $this->querystring() . $this->urlHash(), '/'), '\\'); + return trim(Grav::instance()['base_url'] . '/' . $this->urlQuerystring($output), '\\'); } /** @@ -259,20 +267,40 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface public function querystring($querystring = null, $withQuestionmark = true) { if (!is_null($querystring)) { - $this->set('querystring', ltrim($querystring, '?&')); - + $this->medium_querystring[] = ltrim($querystring, '?&'); foreach ($this->alternatives as $alt) { $alt->querystring($querystring, $withQuestionmark); } } - $querystring = $this->get('querystring', ''); - - if ($withQuestionmark && !empty($querystring)) { - return '?' . $querystring; - } else { - return $querystring; + if (empty($this->medium_querystring)) { + return ''; } + + // join the strings + $querystring = implode("&", $this->medium_querystring); + // explode all strings + $query_parts = explode("&", $querystring); + // Join them again now ensure the elements are unique + $querystring = implode("&", array_unique($query_parts)); + + return $withQuestionmark ? ('?' . $querystring) : $querystring; + } + + /** + * Get the URL with full querystring + * + * @param $url + * @return string + */ + public function urlQuerystring($url) + { + $querystring = $this->querystring(); + if (isset($this->timestamp) && !Utils::contains($querystring, $this->timestamp)) { + $querystring = empty($querystring) ? ('?' . $this->timestamp) : ($querystring . '&' . $this->timestamp); + } + + return ltrim($url . $querystring . $this->urlHash(), '/'); } /** @@ -290,11 +318,7 @@ class Medium extends Data implements RenderableInterface, MediaObjectInterface $hash = $this->get('urlHash', ''); - if ($withHash && !empty($hash)) { - return '#' . $hash; - } else { - return $hash; - } + return $withHash && !empty($hash) ? '#' . $hash : $hash; } /**