Fixed images from plugins/themes disappearing when saving twice

This commit is contained in:
Matias Griese
2021-09-28 13:41:38 +03:00
parent 25f2028a9d
commit 29157a3011
4 changed files with 99 additions and 61 deletions

View File

@@ -38,6 +38,7 @@ use Grav\Framework\Route\RouteFactory;
use Grav\Plugin\AdminPlugin;
use Grav\Plugin\Login\Login;
use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth;
use JsonException;
use PicoFeed\Parser\MalformedXmlException;
use Psr\Http\Message\ServerRequestInterface;
use RocketTheme\Toolbox\Event\Event;
@@ -873,7 +874,7 @@ class Admin
public function data($type, array $post = [])
{
if (!$post) {
$post = $this->grav['uri']->post()['data'] ?? [];
$post = $this->preparePost($this->grav['uri']->post()['data'] ?? []);
}
try {
@@ -2427,4 +2428,66 @@ class Admin
return $changelog;
}
/**
* Prepare and return POST data.
*
* @param array $post
* @return array
*/
public function preparePost($post): array
{
if (!is_array($post)) {
return [];
}
unset($post['task']);
// Decode JSON encoded fields and merge them to data.
if (isset($post['_json'])) {
$post = array_replace_recursive($post, $this->jsonDecode($post['_json']));
unset($post['_json']);
}
return $this->cleanDataKeys($post);
}
/**
* Recursively JSON decode data.
*
* @param array $data
* @return array
* @throws JsonException
*/
private function jsonDecode(array $data): array
{
foreach ($data as &$value) {
if (is_array($value)) {
$value = $this->jsonDecode($value);
} else {
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
}
}
return $data;
}
/**
* @param array $source
* @return array
*/
private function cleanDataKeys(array $source): array
{
$out = [];
foreach ($source as $key => $value) {
$key = str_replace(['%5B', '%5D'], ['[', ']'], $key);
if (is_array($value)) {
$out[$key] = $this->cleanDataKeys($value);
} else {
$out[$key] = $value;
}
}
return $out;
}
}