From 24ab125d28d29a34378cc79fc76b4eaf1b80747f Mon Sep 17 00:00:00 2001 From: slawkens Date: Tue, 16 May 2023 11:44:13 +0200 Subject: [PATCH] Add Settings menu, including all plugins with settings One change included in previous commit, due to missclick --- admin/template/menus.php | 4 ++- system/libs/Settings.php | 67 ++++++++++++++++++++-------------------- system/libs/plugins.php | 64 ++++++++++++++++++++++++++------------ 3 files changed, 81 insertions(+), 54 deletions(-) diff --git a/admin/template/menus.php b/admin/template/menus.php index 5e60b39e..ff67a21e 100644 --- a/admin/template/menus.php +++ b/admin/template/menus.php @@ -2,7 +2,9 @@ return [ ['name' => 'Dashboard', 'icon' => 'tachometer-alt', 'order' => 10, 'link' => 'dashboard'], - ['name' => 'Settings', 'icon' => 'edit', 'order' => 19, 'link' => 'settings&plugin=core'], + ['name' => 'Settings', 'icon' => 'edit', 'order' => 19, 'link' => + require ADMIN . 'includes/settings_menus.php' + ], ['name' => 'News', 'icon' => 'newspaper', 'order' => 20, 'link' => [ ['name' => 'View', 'link' => 'news', 'icon' => 'list', 'order' => 10], diff --git a/system/libs/Settings.php b/system/libs/Settings.php index c05749f3..2be4107b 100644 --- a/system/libs/Settings.php +++ b/system/libs/Settings.php @@ -301,18 +301,18 @@ class Settings implements ArrayAccess $this->loadPlugin($offset); - $pluginName = $this->valuesAsked['pluginName']; + $pluginKeyName = $this->valuesAsked['pluginKeyName']; $key = $this->valuesAsked['key']; // remove whole plugin settings if (!isset($value)) { $this->offsetUnset($offset); - $this->deleteFromDatabase($pluginName, $key); + $this->deleteFromDatabase($pluginKeyName, $key); return; } - $this->settings[$pluginName][$key] = $value; - $this->updateInDatabase($pluginName, $key, $value); + $this->settings[$pluginKeyName][$key] = $value; + $this->updateInDatabase($pluginKeyName, $key, $value); } #[\ReturnTypeWillChange] @@ -320,7 +320,7 @@ class Settings implements ArrayAccess { $this->loadPlugin($offset); - $pluginName = $this->valuesAsked['pluginName']; + $pluginKeyName = $this->valuesAsked['pluginKeyName']; $key = $this->valuesAsked['key']; // remove specified plugin settings (all) @@ -328,7 +328,7 @@ class Settings implements ArrayAccess return isset($this->settings[$offset]); } - return isset($this->settings[$pluginName][$key]); + return isset($this->settings[$pluginKeyName][$key]); } #[\ReturnTypeWillChange] @@ -336,7 +336,7 @@ class Settings implements ArrayAccess { $this->loadPlugin($offset); - $pluginName = $this->valuesAsked['pluginName']; + $pluginKeyName = $this->valuesAsked['pluginKeyName']; $key = $this->valuesAsked['key']; if (isset($this->cache[$offset])) { @@ -345,15 +345,15 @@ class Settings implements ArrayAccess // remove specified plugin settings (all) if(!isset($key)) { - unset($this->settingsArray[$pluginName]); - unset($this->settings[$pluginName]); - $this->deleteFromDatabase($pluginName); + unset($this->settingsArray[$pluginKeyName]); + unset($this->settings[$pluginKeyName]); + $this->deleteFromDatabase($pluginKeyName); return; } - unset($this->settingsArray[$pluginName][$key]); - unset($this->settings[$pluginName][$key]); - $this->deleteFromDatabase($pluginName, $key); + unset($this->settingsArray[$pluginKeyName][$key]); + unset($this->settings[$pluginKeyName][$key]); + $this->deleteFromDatabase($pluginKeyName, $key); } /** @@ -374,26 +374,26 @@ class Settings implements ArrayAccess $this->loadPlugin($offset); - $pluginName = $this->valuesAsked['pluginName']; + $pluginKeyName = $this->valuesAsked['pluginKeyName']; $key = $this->valuesAsked['key']; // return specified plugin settings (all) if(!isset($key)) { - return $this->settingsArray[$pluginName]; + return $this->settingsArray[$pluginKeyName]; } $ret = []; - if(isset($this->settingsArray[$pluginName][$key])) { - $ret = $this->settingsArray[$pluginName][$key]; + if(isset($this->settingsArray[$pluginKeyName][$key])) { + $ret = $this->settingsArray[$pluginKeyName][$key]; } - if(isset($this->settings[$pluginName][$key])) { - $value = $this->settings[$pluginName][$key]; + if(isset($this->settings[$pluginKeyName][$key])) { + $value = $this->settings[$pluginKeyName][$key]; $ret['value'] = $value; } else { - $ret['value'] = $this->settingsArray[$pluginName][$key]['default']; + $ret['value'] = $this->settingsArray[$pluginKeyName][$key]['default']; } if(isset($ret['type'])) { @@ -417,17 +417,17 @@ class Settings implements ArrayAccess private function updateValuesAsked($offset) { - $pluginName = $offset; + $pluginKeyName = $offset; if (strpos($offset, '.')) { $explode = explode('.', $offset, 2); - $pluginName = $explode[0]; + $pluginKeyName = $explode[0]; $key = $explode[1]; - $this->valuesAsked = ['pluginName' => $pluginName, 'key' => $key]; + $this->valuesAsked = ['pluginKeyName' => $pluginKeyName, 'key' => $key]; } else { - $this->valuesAsked = ['pluginName' => $pluginName, 'key' => null]; + $this->valuesAsked = ['pluginKeyName' => $pluginKeyName, 'key' => null]; } } @@ -435,28 +435,29 @@ class Settings implements ArrayAccess { $this->updateValuesAsked($offset); - $pluginName = $this->valuesAsked['pluginName']; + $pluginKeyName = $this->valuesAsked['pluginKeyName']; $key = $this->valuesAsked['key']; - if (!isset($this->settingsArray[$pluginName])) { - if ($pluginName === 'core') { + if (!isset($this->settingsArray[$pluginKeyName])) { + if ($pluginKeyName === 'core') { $settingsFilePath = SYSTEM . 'settings.php'; } else { - $pluginSettings = Plugins::getPluginSettings($pluginName); - if (!$pluginSettings) { - warning("Setting $pluginName does not exist or does not have settings defined."); + //$pluginSettings = Plugins::getPluginSettings($pluginKeyName); + $settings = Plugins::getAllPluginsSettings(); + if (!isset($settings[$pluginKeyName])) { + warning("Setting $pluginKeyName does not exist or does not have settings defined."); return; } - $settingsFilePath = BASE . $pluginSettings; + $settingsFilePath = BASE . $settings[$pluginKeyName]['settingsFilename']; } if (!file_exists($settingsFilePath)) { - throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginName); + throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginKeyName); } $tmp = require $settingsFilePath; - $this->settingsArray[$pluginName] = $tmp['settings']; + $this->settingsArray[$pluginKeyName] = $tmp['settings']; } } } diff --git a/system/libs/plugins.php b/system/libs/plugins.php index a14b6c51..30bbbd35 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -168,6 +168,36 @@ class Plugins { return $hooks; } + public static function getAllPluginsSettings() + { + $cache = Cache::getInstance(); + if ($cache->enabled()) { + $tmp = ''; + if ($cache->fetch('plugins_settings', $tmp)) { + return unserialize($tmp); + } + } + + $settings = []; + foreach (self::getAllPluginsJson() as $plugin) { + if (isset($plugin['settings'])) { + $settingsFile = require BASE . $plugin['settings']; + if (!isset($settingsFile['key'])) { + warning("Settings file for plugin - {$plugin['name']} does not contain 'key' field"); + continue; + } + + $settings[$settingsFile['key']] = ['pluginFilename' => $plugin['filename'], 'settingsFilename' => $plugin['settings']]; + } + } + + if ($cache->enabled()) { + $cache->set('plugins_settings', serialize($settings), 600); // cache for 10 minutes + } + + return $settings; + } + public static function getAllPluginsJson($disabled = false) { $cache = Cache::getInstance(); @@ -180,32 +210,26 @@ class Plugins { $plugins = []; foreach (get_plugins($disabled) as $filename) { - $string = file_get_contents(PLUGINS . $filename . '.json'); - $plugin = json_decode($string, true); - self::$plugin_json = $plugin; - if ($plugin == null) { - self::$warnings[] = 'Cannot load ' . $filename . '.json. File might be not a valid json code.'; - continue; - } - - if (isset($plugin['enabled']) && !getBoolean($plugin['enabled'])) { - self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.'; + $plugin = self::getPluginJson($filename); + + if (!$plugin) { continue; } + $plugin['filename'] = $filename; $plugins[] = $plugin; } if ($cache->enabled()) { - $cache->set('plugins', serialize($plugins), 600); + $cache->set('plugins', serialize($plugins), 600); // cache for 10 minutes } return $plugins; } - public static function getPluginSettings($pluginName) + public static function getPluginSettings($filename) { - $plugin_json = self::getPluginJson($pluginName); + $plugin_json = self::getPluginJson($filename); if (!$plugin_json) { return false; } @@ -217,27 +241,27 @@ class Plugins { return $plugin_json['settings']; } - public static function getPluginJson($name = null) + public static function getPluginJson($filename = null) { - if(!isset($name)) { + if(!isset($filename)) { return self::$plugin_json; } - $pathToPlugin = PLUGINS . $name . '.json'; + $pathToPlugin = PLUGINS . $filename . '.json'; if (!file_exists($pathToPlugin)) { - self::$warnings[] = "Cannot load " . $name . ".json. File doesn't exist."; + self::$warnings[] = "Cannot load $filename.json. File doesn't exist."; return false; } $string = file_get_contents($pathToPlugin); $plugin_json = json_decode($string, true); if ($plugin_json == null) { - self::$warnings[] = 'Cannot load ' . $name . '.json. File might be not a valid json code.'; + self::$warnings[] = "Cannot load $filename.json. File might be not a valid json code."; return false; } - if(isset($plugin_json['enabled']) && $plugin_json['enabled'] === 0) { - self::$warnings[] = 'Skipping ' . $name . '... The plugin is disabled.'; + if (isset($plugin_json['enabled']) && !getBoolean($plugin_json['enabled'])) { + self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.'; return false; }