Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:getgrav/grav into bugfix/redirec…
Browse files Browse the repository at this point in the history
…ts-2435

� Conflicts:
�	CHANGELOG.md
  • Loading branch information
mahagr committed Mar 17, 2021
2 parents fad6814 + 86169bb commit 73bb1f3
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 82 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/trigger-skeletons.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Trigger Skeletons Build

on:
release:
types: [ published ]
workflow_dispatch:
inputs:
version:
Expand Down
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
36 changes: 18 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion system/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion system/src/Grav/Common/Flex/Types/Pages/PageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class PageObject extends FlexPageObject

/** @var string Language code, eg: 'en' */
protected $language;

/** @var string File format, eg. 'md' */
protected $format;

Expand Down Expand Up @@ -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;
}

Expand All @@ -314,6 +316,7 @@ public function move(PageInterface $parent)

$this->_reorder = [];
$this->setProperty('parent_key', $parent->getStorageKey());
$this->storeOriginal();

return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions system/src/Grav/Common/Page/Medium/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
145 changes: 112 additions & 33 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
Loading

0 comments on commit 73bb1f3

Please sign in to comment.