aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/default_colors
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-29 09:38:30 +0200
committerArthurHoaro <arthur@hoa.ro>2020-08-29 09:38:30 +0200
commit972daa4513420d3293d46a808ca2d5be0313e78b (patch)
treeaeafedfe73e020c3e798aebd7fd84e536e87c8c7 /plugins/default_colors
parentbea062149ebcb4663861edb1cc0a32faf85b273f (diff)
downloadShaarli-972daa4513420d3293d46a808ca2d5be0313e78b.tar.gz
Shaarli-972daa4513420d3293d46a808ca2d5be0313e78b.tar.zst
Shaarli-972daa4513420d3293d46a808ca2d5be0313e78b.zip
Default colors plugin: generate CSS file during initialization
Current behaviour only generate the custom CSS file when the plugin settings are saved, which can be annoying if the file is deleted but the settings are set. Most common use case is Docker deployment, because the plugin directory is not mounted as a volume.
Diffstat (limited to 'plugins/default_colors')
-rw-r--r--plugins/default_colors/default_colors.php56
1 files changed, 31 insertions, 25 deletions
diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php
index 1928cc9f..e1fd5cfb 100644
--- a/plugins/default_colors/default_colors.php
+++ b/plugins/default_colors/default_colors.php
@@ -15,6 +15,8 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
15 'DEFAULT_COLORS_DARK_MAIN', 15 'DEFAULT_COLORS_DARK_MAIN',
16]; 16];
17 17
18const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css';
19
18/** 20/**
19 * Display an error if the plugin is active a no color is configured. 21 * Display an error if the plugin is active a no color is configured.
20 * 22 *
@@ -24,58 +26,62 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
24 */ 26 */
25function default_colors_init($conf) 27function default_colors_init($conf)
26{ 28{
27 $params = ''; 29 $params = [];
28 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { 30 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
29 $params .= trim($conf->get('plugins.'. $placeholder, '')); 31 $value = trim($conf->get('plugins.'. $placeholder, ''));
32 if (strlen($value) > 0) {
33 $params[$placeholder] = $value;
34 }
30 } 35 }
31 36
32 if (empty($params)) { 37 if (empty($params)) {
33 $error = t('Default colors plugin error: '. 38 $error = t('Default colors plugin error: '.
34 'This plugin is active and no custom color is configured.'); 39 'This plugin is active and no custom color is configured.');
35 return array($error); 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);
36 } 46 }
37} 47}
38 48
39/** 49/**
40 * When plugin parameters are saved, we regenerate the custom CSS file with provided settings. 50 * When linklist is displayed, include default_colors CSS file.
41 * 51 *
42 * @param array $data $_POST array 52 * @param array $data - header data.
43 * 53 *
44 * @return array Updated $_POST array 54 * @return mixed - header data with default_colors CSS file added.
45 */ 55 */
46function hook_default_colors_save_plugin_parameters($data) 56function hook_default_colors_render_includes($data)
47{ 57{
48 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; 58 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
49 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); 59 if (file_exists($file )) {
50 $content = ''; 60 $data['css_files'][] = $file ;
51 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
52 $content .= ! empty($data[$rule])
53 ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
54 : '';
55 }
56
57 if (! empty($content)) {
58 file_put_contents($file, sprintf($template, $content));
59 } 61 }
60 62
61 return $data; 63 return $data;
62} 64}
63 65
64/** 66/**
65 * When linklist is displayed, include default_colors CSS file. 67 * Regenerate the custom CSS file with provided settings.
66 *
67 * @param array $data - header data.
68 * 68 *
69 * @return mixed - header data with default_colors CSS file added. 69 * @param array $params Plugin configuration (CSS rules)
70 */ 70 */
71function hook_default_colors_render_includes($data) 71function default_colors_generate_css_file($params): void
72{ 72{
73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; 73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
74 if (file_exists($file )) { 74 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
75 $data['css_files'][] = $file ; 75 $content = '';
76 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
77 $content .= !empty($params[$rule])
78 ? default_colors_format_css_rule($params, $rule) .';'. PHP_EOL
79 : '';
76 } 80 }
77 81
78 return $data; 82 if (! empty($content)) {
83 file_put_contents($file, sprintf($template, $content));
84 }
79} 85}
80 86
81/** 87/**