diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-07-20 09:36:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-20 09:36:39 +0200 |
commit | a9633359d10841d0d503d185e60a445b0da7b886 (patch) | |
tree | 6ae7bd751e126538ea35201bb73607e26d7caa76 /plugins | |
parent | 01ba8a0700ea4730e6dc38c685cb6a9c0fbdcccd (diff) | |
parent | a8fb97a0c37b5008765fa73d30a678418011adc1 (diff) | |
download | Shaarli-a9633359d10841d0d503d185e60a445b0da7b886.tar.gz Shaarli-a9633359d10841d0d503d185e60a445b0da7b886.tar.zst Shaarli-a9633359d10841d0d503d185e60a445b0da7b886.zip |
Merge pull request #1317 from ArthurHoaro/feature/default-colors
Plugin to override default template colors
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/default_colors/default_colors.css.template | 3 | ||||
-rw-r--r-- | plugins/default_colors/default_colors.meta | 5 | ||||
-rw-r--r-- | plugins/default_colors/default_colors.php | 111 | ||||
-rw-r--r-- | plugins/demo_plugin/demo_plugin.meta | 3 | ||||
-rw-r--r-- | plugins/demo_plugin/demo_plugin.php | 19 |
5 files changed, 141 insertions, 0 deletions
diff --git a/plugins/default_colors/default_colors.css.template b/plugins/default_colors/default_colors.css.template new file mode 100644 index 00000000..87e22a08 --- /dev/null +++ b/plugins/default_colors/default_colors.css.template | |||
@@ -0,0 +1,3 @@ | |||
1 | :root { | ||
2 | %s | ||
3 | } | ||
diff --git a/plugins/default_colors/default_colors.meta b/plugins/default_colors/default_colors.meta new file mode 100644 index 00000000..108962c6 --- /dev/null +++ b/plugins/default_colors/default_colors.meta | |||
@@ -0,0 +1,5 @@ | |||
1 | description="Override default theme colors. Use any CSS valid color." | ||
2 | parameters="DEFAULT_COLORS_MAIN;DEFAULT_COLORS_BACKGROUND;DEFAULT_COLORS_DARK_MAIN" | ||
3 | parameter.DEFAULT_COLORS_MAIN="Main color (navbar green)" | ||
4 | parameter.DEFAULT_COLORS_BACKGROUND="Background color (light grey)" | ||
5 | parameter.DEFAULT_COLORS_DARK_MAIN="Dark main color (e.g. visited links)" | ||
diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php new file mode 100644 index 00000000..1928cc9f --- /dev/null +++ b/plugins/default_colors/default_colors.php | |||
@@ -0,0 +1,111 @@ | |||
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 | /** | ||
19 | * Display an error if the plugin is active a no color is configured. | ||
20 | * | ||
21 | * @param $conf ConfigManager instance | ||
22 | * | ||
23 | * @return array|null The errors array or null of there is none. | ||
24 | */ | ||
25 | function default_colors_init($conf) | ||
26 | { | ||
27 | $params = ''; | ||
28 | foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { | ||
29 | $params .= trim($conf->get('plugins.'. $placeholder, '')); | ||
30 | } | ||
31 | |||
32 | if (empty($params)) { | ||
33 | $error = t('Default colors plugin error: '. | ||
34 | 'This plugin is active and no custom color is configured.'); | ||
35 | return array($error); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * When plugin parameters are saved, we regenerate the custom CSS file with provided settings. | ||
41 | * | ||
42 | * @param array $data $_POST array | ||
43 | * | ||
44 | * @return array Updated $_POST array | ||
45 | */ | ||
46 | function hook_default_colors_save_plugin_parameters($data) | ||
47 | { | ||
48 | $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; | ||
49 | $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); | ||
50 | $content = ''; | ||
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 | } | ||
60 | |||
61 | return $data; | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * When linklist is displayed, include default_colors CSS file. | ||
66 | * | ||
67 | * @param array $data - header data. | ||
68 | * | ||
69 | * @return mixed - header data with default_colors CSS file added. | ||
70 | */ | ||
71 | function hook_default_colors_render_includes($data) | ||
72 | { | ||
73 | $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; | ||
74 | if (file_exists($file )) { | ||
75 | $data['css_files'][] = $file ; | ||
76 | } | ||
77 | |||
78 | return $data; | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Create a valid CSS rule from parameters settings and plugin parameter. | ||
83 | * | ||
84 | * @param array $data $_POST array | ||
85 | * @param string $parameter Plugin parameter name | ||
86 | * | ||
87 | * @return string CSS rules for the provided parameter and its matching value. | ||
88 | */ | ||
89 | function default_colors_format_css_rule($data, $parameter) | ||
90 | { | ||
91 | if (empty($data[$parameter])) { | ||
92 | return ''; | ||
93 | } | ||
94 | |||
95 | $key = str_replace('DEFAULT_COLORS_', '', $parameter); | ||
96 | $key = str_replace('_', '-', strtolower($key)) .'-color'; | ||
97 | return ' --'. $key .': '. $data[$parameter]; | ||
98 | } | ||
99 | |||
100 | |||
101 | /** | ||
102 | * This function is never called, but contains translation calls for GNU gettext extraction. | ||
103 | */ | ||
104 | function default_colors_translation() | ||
105 | { | ||
106 | // meta | ||
107 | t('Override default theme colors. Use any CSS valid color.'); | ||
108 | t('Main color (navbar green)'); | ||
109 | t('Background color (light grey)'); | ||
110 | t('Dark main color (e.g. visited links)'); | ||
111 | } | ||
diff --git a/plugins/demo_plugin/demo_plugin.meta b/plugins/demo_plugin/demo_plugin.meta index b063ecb7..cd616441 100644 --- a/plugins/demo_plugin/demo_plugin.meta +++ b/plugins/demo_plugin/demo_plugin.meta | |||
@@ -1 +1,4 @@ | |||
1 | description="A demo plugin covering all use cases for template designers and plugin developers." | 1 | description="A demo plugin covering all use cases for template designers and plugin developers." |
2 | parameters="DEMO_PLUGIN_PARAMETER;DEMO_PLUGIN_OTHER_PARAMETER" | ||
3 | parameter.DEMO_PLUGIN_PARAMETER="This is a parameter dedicated to the demo plugin. It'll be suffixed." | ||
4 | parameter.DEMO_PLUGIN_OTHER_PARAMETER="Other demo parameter" | ||
diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index 95ea7fe2..71ba7495 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php | |||
@@ -457,10 +457,29 @@ function hook_demo_plugin_render_feed($data) | |||
457 | } | 457 | } |
458 | 458 | ||
459 | /** | 459 | /** |
460 | * When plugin parameters are saved. | ||
461 | * | ||
462 | * @param array $data $_POST array | ||
463 | * | ||
464 | * @return array Updated $_POST array | ||
465 | */ | ||
466 | function hook_demo_plugin_save_plugin_parameters($data) | ||
467 | { | ||
468 | // Here we edit the provided value, but we can use this to generate config files, etc. | ||
469 | if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) { | ||
470 | $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX'; | ||
471 | } | ||
472 | |||
473 | return $data; | ||
474 | } | ||
475 | |||
476 | /** | ||
460 | * This function is never called, but contains translation calls for GNU gettext extraction. | 477 | * This function is never called, but contains translation calls for GNU gettext extraction. |
461 | */ | 478 | */ |
462 | function demo_dummy_translation() | 479 | function demo_dummy_translation() |
463 | { | 480 | { |
464 | // meta | 481 | // meta |
465 | t('A demo plugin covering all use cases for template designers and plugin developers.'); | 482 | t('A demo plugin covering all use cases for template designers and plugin developers.'); |
483 | t('This is a parameter dedicated to the demo plugin. It\'ll be suffixed.'); | ||
484 | t('Other demo parameter'); | ||
466 | } | 485 | } |