]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/default_colors/default_colors.php
Fix default_colors plugin: update CSS file on color change
[github/shaarli/Shaarli.git] / plugins / default_colors / default_colors.php
1 <?php
2
3 /**
4 * Plugin default_colors.
5 *
6 * Allow users to easily overrides colors of the default theme.
7 */
8
9 use Shaarli\Config\ConfigManager;
10 use Shaarli\Plugin\PluginManager;
11
12 const DEFAULT_COLORS_PLACEHOLDERS = [
13 'DEFAULT_COLORS_MAIN',
14 'DEFAULT_COLORS_BACKGROUND',
15 'DEFAULT_COLORS_DARK_MAIN',
16 ];
17
18 const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css';
19
20 /**
21 * Display an error if the plugin is active a no color is configured.
22 *
23 * @param $conf ConfigManager instance
24 *
25 * @return array|null The errors array or null of there is none.
26 */
27 function default_colors_init($conf)
28 {
29 $params = [];
30 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
31 $value = trim($conf->get('plugins.' . $placeholder, ''));
32 if (strlen($value) > 0) {
33 $params[$placeholder] = $value;
34 }
35 }
36
37 if (empty($params)) {
38 $error = t('Default colors plugin error: ' .
39 'This plugin is active and no custom color is configured.');
40 return [$error];
41 }
42
43 // Colors are defined but the custom CSS file does not exist -> generate it
44 if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) {
45 default_colors_generate_css_file($params);
46 }
47 }
48
49 /**
50 * When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
51 *
52 * @param array $data $_POST array
53 *
54 * @return array Updated $_POST array
55 */
56 function hook_default_colors_save_plugin_parameters($data)
57 {
58 default_colors_generate_css_file($data);
59
60 return $data;
61 }
62
63 /**
64 * When linklist is displayed, include default_colors CSS file.
65 *
66 * @param array $data - header data.
67 *
68 * @return mixed - header data with default_colors CSS file added.
69 */
70 function hook_default_colors_render_includes($data)
71 {
72 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
73 if (file_exists($file)) {
74 $data['css_files'][] = $file ;
75 }
76
77 return $data;
78 }
79
80 /**
81 * Regenerate the custom CSS file with provided settings.
82 *
83 * @param array $params Plugin configuration (CSS rules)
84 */
85 function default_colors_generate_css_file($params): void
86 {
87 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
88 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
89 $content = '';
90 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
91 $content .= !empty($params[$rule])
92 ? default_colors_format_css_rule($params, $rule) . ';' . PHP_EOL
93 : '';
94 }
95
96 if (! empty($content)) {
97 file_put_contents($file, sprintf($template, $content));
98 }
99 }
100
101 /**
102 * Create a valid CSS rule from parameters settings and plugin parameter.
103 *
104 * @param array $data $_POST array
105 * @param string $parameter Plugin parameter name
106 *
107 * @return string CSS rules for the provided parameter and its matching value.
108 */
109 function default_colors_format_css_rule($data, $parameter)
110 {
111 if (empty($data[$parameter])) {
112 return '';
113 }
114
115 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
116 $key = str_replace('_', '-', strtolower($key)) . '-color';
117 return ' --' . $key . ': ' . $data[$parameter];
118 }
119
120
121 /**
122 * This function is never called, but contains translation calls for GNU gettext extraction.
123 */
124 function default_colors_translation()
125 {
126 // meta
127 t('Override default theme colors. Use any CSS valid color.');
128 t('Main color (navbar green)');
129 t('Background color (light grey)');
130 t('Dark main color (e.g. visited links)');
131 }