From a5a0c0399bcfea518330c4bad186da77f89ace6e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 8 Jul 2019 23:10:00 +0200 Subject: WIP - Plugin to override default template colors * Adds a new core plugin to override default template colors * Adds a new hook when plugin settings are saved (`save_plugin_parameters`) * Use CSS native variables for main colors instead of SASS variables * Disable SASS sort order rules due to a bug in the plugin Fixes #1312 --- plugins/default_colors/default_colors.php | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 plugins/default_colors/default_colors.php (limited to 'plugins/default_colors/default_colors.php') diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php new file mode 100644 index 00000000..b898814b --- /dev/null +++ b/plugins/default_colors/default_colors.php @@ -0,0 +1,68 @@ + Date: Sat, 13 Jul 2019 10:38:19 +0200 Subject: Default colors plugin - Add unit tests --- plugins/default_colors/default_colors.php | 65 +++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 11 deletions(-) (limited to 'plugins/default_colors/default_colors.php') diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php index b898814b..1928cc9f 100644 --- a/plugins/default_colors/default_colors.php +++ b/plugins/default_colors/default_colors.php @@ -6,6 +6,7 @@ * Allow users to easily overrides colors of the default theme. */ +use Shaarli\Config\ConfigManager; use Shaarli\Plugin\PluginManager; const DEFAULT_COLORS_PLACEHOLDERS = [ @@ -15,7 +16,32 @@ const DEFAULT_COLORS_PLACEHOLDERS = [ ]; /** - * When plugin parameters are saved + * Display an error if the plugin is active a no color is configured. + * + * @param $conf ConfigManager instance + * + * @return array|null The errors array or null of there is none. + */ +function default_colors_init($conf) +{ + $params = ''; + foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { + $params .= trim($conf->get('plugins.'. $placeholder, '')); + } + + if (empty($params)) { + $error = t('Default colors plugin error: '. + 'This plugin is active and no custom color is configured.'); + return array($error); + } +} + +/** + * When plugin parameters are saved, we regenerate the custom CSS file with provided settings. + * + * @param array $data $_POST array + * + * @return array Updated $_POST array */ function hook_default_colors_save_plugin_parameters($data) { @@ -27,16 +53,20 @@ function hook_default_colors_save_plugin_parameters($data) ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL : ''; } - file_put_contents($file, sprintf($template, $content)); + + if (! empty($content)) { + file_put_contents($file, sprintf($template, $content)); + } + return $data; } /** - * When linklist is displayed, include isso CSS file. + * When linklist is displayed, include default_colors CSS file. * * @param array $data - header data. * - * @return mixed - header data with isso CSS file added. + * @return mixed - header data with default_colors CSS file added. */ function hook_default_colors_render_includes($data) { @@ -48,6 +78,26 @@ function hook_default_colors_render_includes($data) return $data; } +/** + * Create a valid CSS rule from parameters settings and plugin parameter. + * + * @param array $data $_POST array + * @param string $parameter Plugin parameter name + * + * @return string CSS rules for the provided parameter and its matching value. + */ +function default_colors_format_css_rule($data, $parameter) +{ + if (empty($data[$parameter])) { + return ''; + } + + $key = str_replace('DEFAULT_COLORS_', '', $parameter); + $key = str_replace('_', '-', strtolower($key)) .'-color'; + return ' --'. $key .': '. $data[$parameter]; +} + + /** * This function is never called, but contains translation calls for GNU gettext extraction. */ @@ -59,10 +109,3 @@ function default_colors_translation() t('Background color (light grey)'); t('Dark main color (e.g. visited links)'); } - -function default_colors_format_css_rule($data, $parameter) -{ - $key = str_replace('DEFAULT_COLORS_', '', $parameter); - $key = str_replace('_', '-', strtolower($key)) .'-color'; - return ' --'. $key .': '. $data[$parameter]; -} -- cgit v1.2.3