diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-03 08:48:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-03 08:48:51 +0200 |
commit | 865f0a0e014bb114cc2f528be10a023de4057ff4 (patch) | |
tree | 6c66e4bdb1bb986283a74f20ba2a7ea4618a8668 /plugins/default_colors | |
parent | 9e6371a6fd2e581790c93a04e0cb2699b8aa0911 (diff) | |
parent | 972daa4513420d3293d46a808ca2d5be0313e78b (diff) | |
download | Shaarli-865f0a0e014bb114cc2f528be10a023de4057ff4.tar.gz Shaarli-865f0a0e014bb114cc2f528be10a023de4057ff4.tar.zst Shaarli-865f0a0e014bb114cc2f528be10a023de4057ff4.zip |
Merge pull request #1523 from ArthurHoaro/fix/default-colors-generation
Default colors plugin: generate CSS file during initialization
Diffstat (limited to 'plugins/default_colors')
-rw-r--r-- | plugins/default_colors/default_colors.php | 56 |
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 | ||
18 | const 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 | */ |
25 | function default_colors_init($conf) | 27 | function 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 | */ |
46 | function hook_default_colors_save_plugin_parameters($data) | 56 | function 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 | */ |
71 | function hook_default_colors_render_includes($data) | 71 | function 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 | /** |