diff --git a/.github/workflows/trigger-skeletons.yml b/.github/workflows/trigger-skeletons.yml index 08c6d636f..bc4f1a0c1 100644 --- a/.github/workflows/trigger-skeletons.yml +++ b/.github/workflows/trigger-skeletons.yml @@ -1,8 +1,6 @@ name: Trigger Skeletons Build on: - release: - types: [ published ] workflow_dispatch: inputs: version: diff --git a/CHANGELOG.md b/CHANGELOG.md index 9176ad7a3..633fbe070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,20 @@ -# v1.7.8 +# v1.7.9 ## mm/dd/2021 +1. [](#new) + * Added `Media::hide()` method to hide files from media + * Added `Utils::getPathFromToken()` method which works also with `Flex Objects` + * Added `FlexMediaTrait::getMediaField()`, which can be used to access custom media set in the blueprint fields + * Added `FlexMediaTrait::getFieldSettings()`, which can be used to get media field settings +1. [](#improved) + * Method `Utils::getPagePathFromToken()` now calls the more generic `Utils::getPathFromToken()` +1. [](#bugfix) + * Fixed broken media upload in `Flex` with `@self/path`, `@page` and `@theme` destinations [#3275](https://github.com/getgrav/grav/issues/3275) + * Fixed media fields excluding newly deleted files before saving the object + +# v1.7.8 +## 03/17/2021 + 1. [](#new) * Added `ControllerResponseTrait::createDownloadResponse()` method * Added full blueprint support to theme if you move existing files in `blueprints/` to `blueprints/pages/` folder [#3255](https://github.com/getgrav/grav/issues/3255) @@ -24,6 +38,7 @@ * Fixed site redirect with redirect code failing when redirecting to sub-pages [#3035](https://github.com/getgrav/grav/pull/3035/files) * Fixed `Uncaught ValueError: Path cannot be empty` when failing to upload a file [#3265](https://github.com/getgrav/grav/issues/3265) * Fixed `Path cannot be empty` when viewing non-existent log file [#3270](https://github.com/getgrav/grav/issues/3270) + * Fixed `onAdminSave` original page having empty header [#3259](https://github.com/getgrav/grav/issues/3259) # v1.7.7 ## 02/23/2021 diff --git a/composer.lock b/composer.lock index 6ea862f92..13fa1823e 100644 --- a/composer.lock +++ b/composer.lock @@ -381,16 +381,16 @@ }, { "name": "donatj/phpuseragentparser", - "version": "v1.3.0", + "version": "v1.4.0", "source": { "type": "git", "url": "https://github.com/donatj/PhpUserAgent.git", - "reference": "f9a521726b2ce4c5173281ceaab5a02c05b691ef" + "reference": "246c1cf0a44f07168c702203bf30d5f48f17bab0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/donatj/PhpUserAgent/zipball/f9a521726b2ce4c5173281ceaab5a02c05b691ef", - "reference": "f9a521726b2ce4c5173281ceaab5a02c05b691ef", + "url": "https://api.github.com/repos/donatj/PhpUserAgent/zipball/246c1cf0a44f07168c702203bf30d5f48f17bab0", + "reference": "246c1cf0a44f07168c702203bf30d5f48f17bab0", "shasum": "" }, "require": { @@ -433,7 +433,7 @@ ], "support": { "issues": "https://github.com/donatj/PhpUserAgent/issues", - "source": "https://github.com/donatj/PhpUserAgent/tree/v1.3.0" + "source": "https://github.com/donatj/PhpUserAgent/tree/v1.4.0" }, "funding": [ { @@ -445,7 +445,7 @@ "type": "github" } ], - "time": "2021-02-18T04:30:49+00:00" + "time": "2021-03-16T16:25:14+00:00" }, { "name": "dragonmantank/cron-expression", @@ -642,16 +642,16 @@ }, { "name": "filp/whoops", - "version": "2.9.2", + "version": "2.10.0", "source": { "type": "git", "url": "https://github.com/filp/whoops.git", - "reference": "df7933820090489623ce0be5e85c7e693638e536" + "reference": "6ecda5217bf048088b891f7403b262906be5a957" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filp/whoops/zipball/df7933820090489623ce0be5e85c7e693638e536", - "reference": "df7933820090489623ce0be5e85c7e693638e536", + "url": "https://api.github.com/repos/filp/whoops/zipball/6ecda5217bf048088b891f7403b262906be5a957", + "reference": "6ecda5217bf048088b891f7403b262906be5a957", "shasum": "" }, "require": { @@ -701,7 +701,7 @@ ], "support": { "issues": "https://github.com/filp/whoops/issues", - "source": "https://github.com/filp/whoops/tree/2.9.2" + "source": "https://github.com/filp/whoops/tree/2.10.0" }, "funding": [ { @@ -709,7 +709,7 @@ "type": "github" } ], - "time": "2021-01-24T12:00:00+00:00" + "time": "2021-03-16T12:00:00+00:00" }, { "name": "gregwar/cache", @@ -4929,16 +4929,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.2", + "version": "9.5.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4" + "reference": "27241ac75fc37ecf862b6e002bf713b6566cbe41" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f661659747f2f87f9e72095bb207bceb0f151cb4", - "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/27241ac75fc37ecf862b6e002bf713b6566cbe41", + "reference": "27241ac75fc37ecf862b6e002bf713b6566cbe41", "shasum": "" }, "require": { @@ -5016,7 +5016,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.2" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.3" }, "funding": [ { @@ -5028,7 +5028,7 @@ "type": "github" } ], - "time": "2021-02-02T14:45:58+00:00" + "time": "2021-03-17T07:30:34+00:00" }, { "name": "psr/http-client", diff --git a/system/defines.php b/system/defines.php index d0fd87357..6c4b8124f 100644 --- a/system/defines.php +++ b/system/defines.php @@ -8,7 +8,7 @@ // Some standard defines define('GRAV', true); -define('GRAV_VERSION', '1.7.7'); +define('GRAV_VERSION', '1.7.8'); define('GRAV_SCHEMA', '1.7.0_2020-11-20_1'); define('GRAV_TESTING', false); diff --git a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php index cc9473061..2d45b910b 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php +++ b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php @@ -62,7 +62,6 @@ class PageObject extends FlexPageObject /** @var string Language code, eg: 'en' */ protected $language; - /** @var string File format, eg. 'md' */ protected $format; @@ -295,6 +294,9 @@ public function save($reorder = true) $grav->fireEvent('onAdminAfterSave', new Event(['type' => 'flex', 'directory' => $this->getFlexDirectory(), 'object' => $this])); } + // Reset original after save events have all been called. + $this->_original = null; + return $instance; } @@ -314,6 +316,7 @@ public function move(PageInterface $parent) $this->_reorder = []; $this->setProperty('parent_key', $parent->getStorageKey()); + $this->storeOriginal(); return $this; } diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index 06b44a77c..db81ddd4a 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -198,6 +198,17 @@ public function add($name, $file) } } + /** + * @param string $name + * @return void + */ + public function hide($name) + { + $this->offsetUnset($name); + + unset($this->images[$name], $this->videos[$name], $this->audios[$name], $this->files[$name]); + } + /** * Create Medium from a file. * diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index a076f0aaa..dac5cf7a4 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -12,11 +12,15 @@ use DateTime; use DateTimeZone; use Exception; +use Grav\Common\Flex\Types\Pages\PageObject; use Grav\Common\Helpers\Truncator; use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Markdown\Parsedown; use Grav\Common\Markdown\ParsedownExtra; use Grav\Common\Page\Markdown\Excerpts; +use Grav\Common\Page\Pages; +use Grav\Framework\Flex\Flex; +use Grav\Framework\Flex\Interfaces\FlexObjectInterface; use InvalidArgumentException; use Negotiation\Accept; use Negotiation\Negotiator; @@ -1520,7 +1524,7 @@ public static function sortArrayByKey($array, $array_key, $direction = SORT_DESC } /** - * Get path based on a token + * Get relative page path based on a token. * * @param string $path * @param PageInterface|null $page @@ -1529,47 +1533,122 @@ public static function sortArrayByKey($array, $array_key, $direction = SORT_DESC */ public static function getPagePathFromToken($path, PageInterface $page = null) { - $path_parts = pathinfo($path); - $grav = Grav::instance(); + return static::getPathFromToken($path, $page); + } - $basename = ''; - if (isset($path_parts['extension'])) { - $basename = '/' . $path_parts['basename']; - $path = rtrim($path_parts['dirname'], ':'); + /** + * Get relative path based on a token. + * + * Path supports following syntaxes: + * + * 'self@', 'self@/path' + * 'page@:/route', 'page@:/route/filename.ext' + * 'theme@:', 'theme@:/path' + * + * @param string $path + * @param FlexObjectInterface|PageInterface|null $object + * @return string + * @throws RuntimeException + */ + public static function getPathFromToken($path, $object = null) + { + $matches = static::resolveTokenPath($path); + if (null === $matches) { + return $path; } - $regex = '/(@self|self@)|((?:@page|page@):(?:.*))|((?:@theme|theme@):(?:.*))/'; - preg_match($regex, $path, $matches); + $grav = Grav::instance(); - if ($matches) { - if ($matches[1]) { - if (null === $page) { - throw new RuntimeException('Page not available for this self@ reference'); + switch ($matches[0]) { + case 'self': + if (null === $object) { + throw new RuntimeException(sprintf('Page not available for self@ reference: %s', $path)); } - } elseif ($matches[2]) { - // page@ - $parts = explode(':', $path); - $route = $parts[1]; - $page = $grav['page']->find($route); - } elseif ($matches[3]) { - // theme@ - $parts = explode(':', $path); - $route = $parts[1]; - $theme = str_replace(ROOT_DIR, '', $grav['locator']->findResource('theme://')); - - return $theme . $route . $basename; - } - } else { - return $path . $basename; - } - if (!$page) { - throw new RuntimeException('Page route not found: ' . $path); + if ($matches[2] === '') { + if ($object->exists()) { + $route = '/' . $matches[1]; + + if ($object instanceof PageInterface) { + return trim($object->relativePagePath() . $route, '/'); + } + + $folder = $object->getMediaFolder(); + if ($folder) { + return trim($folder . $route, '/'); + } + } else { + return ''; + } + } + + break; + case 'page': + if ($matches[1] === '') { + $route = '/' . $matches[2]; + + // Exclude filename from the page lookup. + if (pathinfo($route, PATHINFO_EXTENSION)) { + $basename = '/' . basename($route); + $route = \dirname($route); + } else { + $basename = ''; + } + + $key = trim($route === '/' ? $grav['config']->get('system.home.alias') : $route, '/'); + if ($object instanceof PageObject) { + $object = $object->getFlexDirectory()->getObject($key); + } elseif (static::isAdminPlugin()) { + /** @var Flex|null $flex */ + $flex = $grav['flex'] ?? null; + $object = $flex ? $flex->getObject($key, 'pages') : null; + } else { + /** @var Pages $pages */ + $pages = $grav['pages']; + $object = $pages->find($route); + } + + if ($object instanceof PageInterface) { + return trim($object->relativePagePath() . $basename, '/'); + } + } + + break; + case 'theme': + if ($matches[1] === '') { + $route = '/' . $matches[2]; + $theme = $grav['locator']->findResource('theme://', false); + if (false !== $theme) { + return trim($theme . $route, '/'); + } + } + + break; } - $path = str_replace($matches[0], rtrim($page->relativePagePath(), '/'), $path); + throw new RuntimeException(sprintf('Token path not found: %s', $path)); + } + + /** + * Returns [token, route, path] from '@token/route:/path'. Route and path are optional. If pattern does not match, return null. + * + * @param string $path + * @return string[]|null + */ + private static function resolveTokenPath(string $path): ?array + { + if (strpos($path, '@') !== false) { + $regex = '/^(@\w+|\w+@|@\w+@)([^:]*)(.*)$/u'; + if (preg_match($regex, $path, $matches)) { + return [ + trim($matches[1], '@'), + trim($matches[2], '/'), + trim($matches[3], ':/') + ]; + } + } - return $path . $basename; + return null; } /** diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 9011745ad..09c563825 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -885,6 +885,14 @@ public function __debugInfo() ]; } + /** + * Clone object. + */ + public function __clone() + { + // Allows future compatibility as parent::__clone() works. + } + protected function markAsCopy(): void { $meta = $this->getMetaData(); diff --git a/system/src/Grav/Framework/Flex/Pages/FlexPageObject.php b/system/src/Grav/Framework/Flex/Pages/FlexPageObject.php index 39ac0c423..621dae046 100644 --- a/system/src/Grav/Framework/Flex/Pages/FlexPageObject.php +++ b/system/src/Grav/Framework/Flex/Pages/FlexPageObject.php @@ -16,7 +16,6 @@ use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Page\Traits\PageFormTrait; use Grav\Common\User\Interfaces\UserCollectionInterface; -use Grav\Framework\File\Formatter\YamlFormatter; use Grav\Framework\Flex\FlexObject; use Grav\Framework\Flex\Interfaces\FlexObjectInterface; use Grav\Framework\Flex\Interfaces\FlexTranslateInterface; @@ -50,6 +49,20 @@ class FlexPageObject extends FlexObject implements PageInterface, FlexTranslateI /** @var array|null */ protected $_reorder; + /** @var FlexPageObject|null */ + protected $_original; + + /** + * Clone page. + */ + public function __clone() + { + parent::__clone(); + + if (isset($this->header)) { + $this->header = clone($this->header); + } + } /** * @return array @@ -242,6 +255,32 @@ public function save($reorder = true) return parent::save(); } + /** + * Gets the Page Unmodified (original) version of the page. + * + * Assumes that object has been cloned before modifying it. + * + * @return FlexPageObject|null The original version of the page. + */ + public function getOriginal() + { + return $this->_original; + } + + /** + * Store the Page Unmodified (original) version of the page. + * + * Can be called multiple times, only the first call matters. + * + * @return void + */ + public function storeOriginal(): void + { + if (null === $this->_original) { + $this->_original = clone $this; + } + } + /** * Get display order for the associated media. * diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php index 7320f053b..1c32bf566 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php @@ -277,6 +277,8 @@ public function move(PageInterface $parent) throw new RuntimeException('Failed: Cannot set page parent to a child of current page'); } + $this->storeOriginal(); + // TODO: throw new RuntimeException(__METHOD__ . '(): Not Implemented'); } @@ -292,6 +294,8 @@ public function move(PageInterface $parent) */ public function copy(PageInterface $parent = null) { + $this->storeOriginal(); + $filesystem = Filesystem::getInstance(false); $parentStorageKey = ltrim($filesystem->dirname("/{$this->getMasterKey()}"), '/'); @@ -1087,18 +1091,6 @@ public function folderExists(): bool return $this->exists() || is_dir($this->getStorageFolder() ?? ''); } - /** - * Gets the Page Unmodified (original) version of the page. - * - * Assumes that object has been cloned before modifying it. - * - * @return PageInterface|null The original version of the page. - */ - public function getOriginal() - { - return $this->getFlexDirectory()->getObject($this->getKey()); - } - /** * Gets the action. * diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index c03a23de0..c9ae58057 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -14,8 +14,10 @@ use Grav\Common\Media\Interfaces\MediaCollectionInterface; use Grav\Common\Media\Interfaces\MediaUploadInterface; use Grav\Common\Media\Traits\MediaTrait; +use Grav\Common\Page\Media; use Grav\Common\Page\Medium\Medium; use Grav\Common\Page\Medium\MediumFactory; +use Grav\Common\Utils; use Grav\Framework\Cache\CacheInterface; use Grav\Framework\Filesystem\Filesystem; use Grav\Framework\Flex\FlexDirectory; @@ -23,6 +25,7 @@ use Psr\Http\Message\UploadedFileInterface; use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; use RuntimeException; +use function array_key_exists; use function in_array; use function is_array; use function is_callable; @@ -75,11 +78,40 @@ public function getMedia() return $media; } + /** + * @param string $field + * @return MediaCollectionInterface|null + */ + public function getMediaField(string $field): ?MediaCollectionInterface + { + // Field specific media. + $settings = $this->getFieldSettings($field); + if (!empty($settings['media_field'])) { + $var = 'destination'; + } elseif (!empty($settings['media_picker_field'])) { + $var = 'folder'; + } + + if (empty($var)) { + // Not a media field. + $media = null; + } elseif ($settings['self']) { + // Uses main media. + $media = $this->getMedia(); + } else { + // Uses custom media. + $media = new Media($settings[$var]); + $this->addUpdatedMedia($media); + } + + return $media; + } + /** * @param string $field * @return array|null */ - protected function getFieldSettings(string $field): ?array + public function getFieldSettings(string $field): ?array { if ($field === '') { return null; @@ -88,14 +120,32 @@ protected function getFieldSettings(string $field): ?array // Load settings for the field. $schema = $this->getBlueprint()->schema(); $settings = $field && is_object($schema) ? (array)$schema->getProperty($field) : null; + if (!isset($settings) || !is_array($settings)) { + return null; + } - if (isset($settings['type']) && (in_array($settings['type'], ['avatar', 'file', 'pagemedia']) || !empty($settings['destination']))) { - // Set destination folder. + $type = $settings['type'] ?? ''; + + // Media field. + if (!empty($settings['media_field']) || array_key_exists('destination', $settings) || in_array($type, ['avatar', 'file', 'pagemedia'], true)) { $settings['media_field'] = true; - if (empty($settings['destination']) || in_array($settings['destination'], ['@self', 'self@', '@self@'], true)) { - $settings['destination'] = $this->getMediaFolder(); + $var = 'destination'; + } + + // Media picker field. + if (!empty($settings['media_picker_field']) || in_array($type, ['filepicker', 'pagemediaselect'], true)) { + $settings['media_picker_field'] = true; + $var = 'folder'; + } + + // Set media folder for media fields. + if (isset($var)) { + $folder = $settings[$var] ?? ''; + if (in_array(rtrim($folder, '/'), ['', '@self', 'self@', '@self@'], true)) { + $settings[$var] = $this->getMediaFolder(); $settings['self'] = true; } else { + $settings[$var] = Utils::getPathFromToken($folder, $this); $settings['self'] = false; } } @@ -115,7 +165,6 @@ protected function getMediaFieldSettings(string $field): array return $settings + ['accept' => '*', 'limit' => 1000, 'self' => true]; } - protected function getMediaFields(): array { // Load settings for the field. @@ -206,12 +255,13 @@ public function checkUploadedMediaFile(UploadedFileInterface $uploadedFile, stri */ public function uploadMediaFile(UploadedFileInterface $uploadedFile, string $filename = null, string $field = null): void { - $media = $this->getMedia(); + $settings = $this->getMediaFieldSettings($field ?? ''); + + $media = $field ? $this->getMediaField($field) : $this->getMedia(); if (!$media instanceof MediaUploadInterface) { throw new RuntimeException("Media for {$this->getFlexDirectory()->getFlexType()} doesn't support file uploads."); } - $settings = $this->getMediaFieldSettings($field ?? ''); $filename = $media->checkUploadedFile($uploadedFile, $filename, $settings); $media->copyUploadedFile($uploadedFile, $filename, $settings); $this->clearMediaCache(); @@ -322,13 +372,20 @@ protected function addUpdatedMedia(MediaCollectionInterface $media): void foreach ($this->getUpdatedMedia() as $filename => $upload) { if (is_array($upload)) { // Uses new format with [UploadedFileInterface, array]. - $upload = $upload[0]; + $settings = $upload[1]; + if ($settings['destination'] === $media->getPath()) { + $upload = $upload[0]; + } else { + $upload = false; + } } - if ($upload) { - $medium = MediumFactory::fromUploadedFile($upload); + if (false !== $upload) { + $medium = $upload ? MediumFactory::fromUploadedFile($upload) : null; + $updated = true; if ($medium) { - $updated = true; $media->add($filename, $medium); + } else { + $media->hide($filename); } } } @@ -356,7 +413,6 @@ protected function saveUpdatedMedia(): void return; } - // Upload/delete altered files. /** * @var string $filename diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index 4329a454e..32fd84ce1 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -395,6 +395,12 @@ public function testVerifyNonce(): void self::assertTrue(Utils::verifyNonce(Utils::getNonce('test-action'), 'test-action')); } + public function testGetPagePathFromToken(): void + { + self::assertEquals('', Utils::getPagePathFromToken('')); + self::assertEquals('/test/path', Utils::getPagePathFromToken('/test/path')); + } + public function testUrl(): void { $this->uri->initializeWithUrl('http://testing.dev/path1/path2')->init();