A free, self-hostable news aggregator…
Add comprehensive user configuration in extensions (#3397)
Alexis Degrugillier8285f1df43ce
diff --git a/docs/en/developers/03_Backend/05_Extensions.md b/docs/en/developers/03_Backend/05_Extensions.mdindex ba3f10e4f..62eaa5410 100644--- a/docs/en/developers/03_Backend/05_Extensions.md+++ b/docs/en/developers/03_Backend/05_Extensions.md@@ -300,6 +300,9 @@ The `Minz_Extension` abstract class defines another set of methods that should n* the `registerViews` method registers the extension views in FreshRSS.* the `registerTranslates` method registers the extension translation files in FreshRSS.* the `registerHook` method registers hook actions in different part of the application.+* the `getUserConfiguration` method retrieves the extension configuration for the current user.+* the `setUserConfiguration` method stores the extension configuration for the current user.+* the `removeUserConfiguration` method removes the extension configuration for the current user.> Note that if you modify the later set of methods, you might break the extension system. Thus making FreshRSS unusable. So it's highly recommended to let those unmodified.diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.phpindex 3cf356a2b..7c60c7567 100644--- a/lib/Minz/Configuration.php+++ b/lib/Minz/Configuration.php@@ -140,6 +140,15 @@ class Minz_Configuration {return $this->configuration_setter;}+ /**+ * Check if a parameter is defined in the configuration+ *+ * @return bool+ */+ public function hasParam(string $key) {+ return isset($this->data[$key]);+ }+/*** Return the value of the given param.*diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.phpindex 6b13f5a4e..e463c03c4 100644--- a/lib/Minz/Extension.php+++ b/lib/Minz/Extension.php@@ -11,6 +11,7 @@ abstract class Minz_Extension {private $description;private $version;private $type;+ private $config_key = 'extensions';public static $authorized_types = array('system',@@ -195,4 +196,76 @@ abstract class Minz_Extension {public function registerHook($hook_name, $hook_function) {Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);}++ /**+ * @return bool+ */+ private function isUserConfigurationEnabled() {+ if (!class_exists('FreshRSS_Context', false)) {+ return false;+ }+ if (null === FreshRSS_Context::$user_conf) {+ return false;+ }+ return true;+ }++ /**+ * @return bool+ */+ private function isExtensionConfigured() {+ if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) {+ return false;+ }++ $extensions = FreshRSS_Context::$user_conf->{$this->config_key};+ return array_key_exists($this->getName(), $extensions);+ }++ /**+ * @return array+ */+ public function getUserConfiguration() {+ if (!$this->isUserConfigurationEnabled()) {+ return [];+ }+ if (!$this->isExtensionConfigured()) {+ return [];+ }++ return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];+ }++ public function setUserConfiguration(array $configuration) {+ if (!$this->isUserConfigurationEnabled()) {+ return;+ }+ if ($this->isExtensionConfigured()) {+ $extensions = FreshRSS_Context::$user_conf->{$this->config_key};+ } else {+ $extensions = [];+ }+ $extensions[$this->getName()] = $configuration;++ FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;+ FreshRSS_Context::$user_conf->save();+ }++ public function removeUserConfiguration(){+ if (!$this->isUserConfigurationEnabled()) {+ return;+ }+ if (!$this->isExtensionConfigured()) {+ return;+ }++ $extensions = FreshRSS_Context::$user_conf->{$this->config_key};+ unset($extensions[$this->getName()]);+ if (empty($extensions)) {+ $extensions = null;+ }++ FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;+ FreshRSS_Context::$user_conf->save();+ }}