From 925dbf7cbf08355c224ed8b2cb171bca418e818e Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 7 Feb 2022 11:55:14 +0200 Subject: [PATCH] Fixed disabling/enabling plugin from the list breaking the plugin configuration --- .editorconfig | 6 ++--- CHANGELOG.md | 1 + classes/plugin/AdminController.php | 38 +++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/.editorconfig b/.editorconfig index edc1aafc..6375a81e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,6 +12,6 @@ insert_final_newline = true indent_style = space indent_size = 2 -# 2 space indentation -[*.{yaml,.yml}] -indent_size = 2 +# 4 space indentation +[*.php] +indent_size = 4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cdcb189..15eee3fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * Fixed persistent focus on Folder field when Adding page (Safari) [#2209](https://github.com/getgrav/grav-plugin-admin/issues/2209) * Fixed performance of Plugins / Themes sort in the installation table * Fixed list field with key/value pairs throwing an exception due to bad value [#2199](https://github.com/getgrav/grav-plugin-admin/issues/2199) + * Fixed disabling/enabling plugin from the list breaking the plugin configuration # v1.10.29 ## 01/28/2022 diff --git a/classes/plugin/AdminController.php b/classes/plugin/AdminController.php index 61dbceb0..1a837532 100644 --- a/classes/plugin/AdminController.php +++ b/classes/plugin/AdminController.php @@ -22,6 +22,7 @@ use Grav\Common\Page\Medium\Medium; use Grav\Common\Page\Page; use Grav\Common\Page\Pages; use Grav\Common\Page\Collection; +use Grav\Common\Plugins; use Grav\Common\Security; use Grav\Common\User\Interfaces\UserCollectionInterface; use Grav\Common\User\Interfaces\UserInterface; @@ -35,6 +36,7 @@ use PicoFeed\Parser\MalformedXmlException; use Psr\Http\Message\ResponseInterface; use RocketTheme\Toolbox\Event\Event; use RocketTheme\Toolbox\File\File; +use RocketTheme\Toolbox\File\YamlFile; use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; use Twig\Loader\FilesystemLoader; @@ -629,10 +631,8 @@ class AdminController extends AdminBaseController return false; } - // Filter value and save it. - $this->post = ['enabled' => true]; - $obj = $this->prepareData($this->post); - $obj->save(); + $type = $this->getDataType(); + $this->updatePluginState($type, ['enabled' => true]); $this->post = ['_redirect' => 'plugins']; if ($this->grav['uri']->param('redirect')) { @@ -662,10 +662,8 @@ class AdminController extends AdminBaseController return false; } - // Filter value and save it. - $this->post = ['enabled' => false]; - $obj = $this->prepareData($this->post); - $obj->save(); + $type = $this->getDataType(); + $this->updatePluginState($type, ['enabled' => false]); $this->post = ['_redirect' => 'plugins']; $this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.SUCCESSFULLY_DISABLED_PLUGIN'), 'info'); @@ -675,6 +673,30 @@ class AdminController extends AdminBaseController return true; } + /** + * @param string $type + * @param array $value + * @return void + */ + protected function updatePluginState(string $type, array $value): void + { + $obj = Plugins::get(preg_replace('|plugins/|', '', $type)); + if (null === $obj) { + throw new \RuntimeException("Plugin '{$type}' doesn't exist!"); + } + + /** @var UniformResourceLocator $locator */ + $locator = $this->grav['locator']; + + // Configuration file will be saved to the existing config stream. + $filename = $locator->findResource('config://') . "/{$type}.yaml"; + + $file = YamlFile::instance($filename); + $contents = $value + $file->content(); + + $file->save($contents); + } + /** * Set the default theme. *