]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/demo_plugin/demo_plugin.php
Plugin system: allow plugins to provide custom routes
[github/shaarli/Shaarli.git] / plugins / demo_plugin / demo_plugin.php
CommitLineData
786ddad9 1<?php
53054b2b 2
786ddad9
A
3/**
4 * Demo Plugin.
5 *
83ef0ff1
PB
6 * This plugin tries to completely cover Shaarli's plugin API.
7 * Can be used by plugin developers to make their own plugin.
786ddad9
A
8 */
9
a6e9c084
A
10require_once __DIR__ . '/DemoPluginController.php';
11
786ddad9
A
12/*
13 * RENDER HEADER, INCLUDES, FOOTER
14 *
15 * Those hooks are called at every page rendering.
16 * You can filter its execution by checking _PAGE_ value
17 * and check user status with _LOGGEDIN_.
18 */
19
6a65bc57 20use Shaarli\Config\ConfigManager;
e1850388 21use Shaarli\Plugin\PluginManager;
1a8ac737 22use Shaarli\Render\TemplatePage;
6a65bc57
A
23
24/**
25 * In the footer hook, there is a working example of a translation extension for Shaarli.
26 *
27 * The extension must be attached to a new translation domain (i.e. NOT 'shaarli').
28 * Use case: any custom theme or non official plugin can use the translation system.
29 *
30 * See the documentation for more information.
31 */
32const EXT_TRANSLATION_DOMAIN = 'demo';
33
34/*
35 * This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
36 */
37function demo_plugin_t($text, $nText = '', $nb = 1)
38{
39 return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN);
40}
41
7fde6de1
A
42/**
43 * Initialization function.
44 * It will be called when the plugin is loaded.
45 * This function can be used to return a list of initialization errors.
46 *
47 * @param $conf ConfigManager instance.
48 *
49 * @return array List of errors (optional).
50 */
51function demo_plugin_init($conf)
52{
53 $conf->get('toto', 'nope');
54
6a65bc57
A
55 if (! $conf->exists('translation.extensions.demo')) {
56 // Custom translation with the domain 'demo'
57 $conf->set('translation.extensions.demo', 'plugins/demo_plugin/languages/');
58 $conf->write(true);
59 }
60
7fde6de1
A
61 $errors[] = 'This a demo init error.';
62 return $errors;
63}
64
a6e9c084
A
65function demo_plugin_register_routes(): array
66{
67 return [
68 [
69 'method' => 'GET',
70 'route' => '/custom',
71 'callable' => 'Shaarli\DemoPlugin\DemoPluginController:index',
72 ],
73 ];
74}
75
786ddad9
A
76/**
77 * Hook render_header.
83ef0ff1 78 * Executed on every page render.
786ddad9
A
79 *
80 * Template placeholders:
81 * - buttons_toolbar
82 * - fields_toolbar
83 *
84 * @param array $data data passed to plugin
85 *
86 * @return array altered $data.
87 */
88function hook_demo_plugin_render_header($data)
89{
90 // Only execute when linklist is rendered.
1a8ac737 91 if ($data['_PAGE_'] == TemplatePage::LINKLIST) {
786ddad9
A
92 // If loggedin
93 if ($data['_LOGGEDIN_'] === true) {
ba0fd807
A
94 /*
95 * Links in toolbar:
96 * A link is an array of its attributes (key="value"),
97 * and a mandatory `html` key, which contains its value.
98 */
53054b2b
A
99 $button = [
100 'attr' => [
ba0fd807
A
101 'href' => '#',
102 'class' => 'mybutton',
103 'title' => 'hover me',
53054b2b 104 ],
ba0fd807 105 'html' => 'DEMO buttons toolbar',
53054b2b 106 ];
ba0fd807 107 $data['buttons_toolbar'][] = $button;
786ddad9
A
108 }
109
ba0fd807
A
110 /*
111 * Add additional input fields in the tools.
112 * A field is an array containing:
113 * [
114 * 'form-attribute-1' => 'form attribute 1 value',
115 * 'form-attribute-2' => 'form attribute 2 value',
116 * 'inputs' => [
117 * [
118 * 'input-1-attribute-1 => 'input 1 attribute 1 value',
119 * 'input-1-attribute-2 => 'input 1 attribute 2 value',
120 * ],
121 * [
122 * 'input-2-attribute-1 => 'input 2 attribute 1 value',
123 * ],
124 * ],
125 * ]
126 * This example renders as:
9d9f6d75
V
127 * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
128 * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
129 * <input input-2-attribute-1="input 2 attribute 1 value">
130 * </form>
ba0fd807 131 */
53054b2b
A
132 $form = [
133 'attr' => [
ba0fd807 134 'method' => 'GET',
9fbc4229 135 'action' => $data['_BASE_PATH_'] . '/',
ba0fd807 136 'class' => 'addform',
53054b2b
A
137 ],
138 'inputs' => [
139 [
ba0fd807
A
140 'type' => 'text',
141 'name' => 'demo',
142 'placeholder' => 'demo',
53054b2b
A
143 ]
144 ]
145 ];
ba0fd807 146 $data['fields_toolbar'][] = $form;
786ddad9 147 }
38603b24 148 // Another button always displayed
53054b2b
A
149 $button = [
150 'attr' => [
ba0fd807 151 'href' => '#',
53054b2b 152 ],
ba0fd807 153 'html' => 'Demo',
53054b2b 154 ];
ba0fd807 155 $data['buttons_toolbar'][] = $button;
786ddad9
A
156
157 return $data;
158}
159
160/**
161 * Hook render_includes.
83ef0ff1 162 * Executed on every page render.
786ddad9
A
163 *
164 * Template placeholders:
165 * - css_files
166 *
167 * Data:
168 * - _PAGE_: current page
169 * - _LOGGEDIN_: true/false
170 *
171 * @param array $data data passed to plugin
172 *
173 * @return array altered $data.
174 */
175function hook_demo_plugin_render_includes($data)
176{
177 // List of plugin's CSS files.
178 // Note that you just need to specify CSS path.
179 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
180
181 return $data;
182}
183
184/**
185 * Hook render_footer.
83ef0ff1 186 * Executed on every page render.
786ddad9
A
187 *
188 * Template placeholders:
189 * - text
40a5f296 190 * - endofpage
786ddad9
A
191 * - js_files
192 *
193 * Data:
194 * - _PAGE_: current page
195 * - _LOGGEDIN_: true/false
196 *
197 * @param array $data data passed to plugin
198 *
199 * @return array altered $data.
200 */
201function hook_demo_plugin_render_footer($data)
202{
83ef0ff1 203 // Footer text
53054b2b 204 $data['text'][] = '<br>' . demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
786ddad9 205
40a5f296
A
206 // Free elements at the end of the page.
207 $data['endofpage'][] = '<marquee id="demo_marquee">' .
208 'DEMO: it\'s 1999 all over again!' .
209 '</marquee>';
210
786ddad9
A
211 // List of plugin's JS files.
212 // Note that you just need to specify CSS path.
213 $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
214
215 return $data;
216}
217
218/*
219 * SPECIFIC PAGES
220 */
221
222/**
223 * Hook render_linklist.
224 *
225 * Template placeholders:
226 * - action_plugin: next to 'private only' button.
227 * - plugin_start_zone: page start
228 * - plugin_end_zone: page end
229 * - link_plugin: icons below each links.
230 *
231 * Data:
232 * - _LOGGEDIN_: true/false
233 *
234 * @param array $data data passed to plugin
235 *
236 * @return array altered $data.
237 */
238function hook_demo_plugin_render_linklist($data)
239{
ba0fd807
A
240 /*
241 * Action links (action_plugin):
242 * A link is an array of its attributes (key="value"),
243 * and a mandatory `html` key, which contains its value.
244 * It's also recommended to add key 'on' or 'off' for theme rendering.
245 */
53054b2b
A
246 $action = [
247 'attr' => [
ba0fd807
A
248 'href' => '?up',
249 'title' => 'Uppercase!',
53054b2b 250 ],
ba0fd807 251 'html' => '←',
53054b2b 252 ];
786ddad9
A
253
254 if (isset($_GET['up'])) {
255 // Manipulate link data
256 foreach ($data['links'] as &$value) {
257 $value['description'] = strtoupper($value['description']);
258 $value['title'] = strtoupper($value['title']);
259 }
ba0fd807
A
260 $action['on'] = true;
261 } else {
262 $action['off'] = true;
786ddad9 263 }
ba0fd807 264 $data['action_plugin'][] = $action;
786ddad9
A
265
266 // link_plugin (for each link)
267 foreach ($data['links'] as &$value) {
268 $value['link_plugin'][] = ' DEMO \o/';
269 }
270
271 // plugin_start_zone
272 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
273 // plugin_start_zone
274 $data['plugin_end_zone'][] = '<center>AFTER</center>';
275
276 return $data;
277}
278
279/**
280 * Hook render_editlink.
281 *
282 * Template placeholders:
283 * - field_plugin: add link fields after tags.
284 *
285 * @param array $data data passed to plugin
286 *
287 * @return array altered $data.
288 */
289function hook_demo_plugin_render_editlink($data)
290{
291 // Load HTML into a string
53054b2b 292 $html = file_get_contents(PluginManager::$PLUGINS_PATH . '/demo_plugin/field.html');
786ddad9 293
83ef0ff1 294 // Replace value in HTML if it exists in $data
786ddad9
A
295 if (!empty($data['link']['stuff'])) {
296 $html = sprintf($html, $data['link']['stuff']);
297 } else {
298 $html = sprintf($html, '');
299 }
300
301 // field_plugin
302 $data['edit_link_plugin'][] = $html;
303
304 return $data;
305}
306
307/**
308 * Hook render_tools.
309 *
310 * Template placeholders:
311 * - tools_plugin: after other tools.
312 *
313 * @param array $data data passed to plugin
314 *
315 * @return array altered $data.
316 */
317function hook_demo_plugin_render_tools($data)
318{
319 // field_plugin
a6e9c084
A
320 $data['tools_plugin'][] = '<div class="tools-item">
321 <a href="' . $data['_BASE_PATH_'] . '/plugin/demo_plugin/custom">
322 <span class="pure-button pure-u-lg-2-3 pure-u-3-4">Demo Plugin Custom Route</span>
323 </a>
324 </div>';
786ddad9
A
325
326 return $data;
327}
328
329/**
330 * Hook render_picwall.
331 *
332 * Template placeholders:
333 * - plugin_start_zone: page start.
334 * - plugin_end_zone: page end.
335 *
336 * Data:
337 * - _LOGGEDIN_: true/false
338 *
339 * @param array $data data passed to plugin
340 *
341 * @return array altered $data.
342 */
343function hook_demo_plugin_render_picwall($data)
344{
786ddad9 345 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
786ddad9
A
346 $data['plugin_end_zone'][] = '<center>AFTER</center>';
347
348 return $data;
349}
350
351/**
352 * Hook render_tagcloud.
353 *
354 * Template placeholders:
355 * - plugin_start_zone: page start.
356 * - plugin_end_zone: page end.
357 *
358 * Data:
359 * - _LOGGEDIN_: true/false
360 *
361 * @param array $data data passed to plugin
362 *
363 * @return array altered $data.
364 */
365function hook_demo_plugin_render_tagcloud($data)
366{
786ddad9 367 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
786ddad9
A
368 $data['plugin_end_zone'][] = '<center>AFTER</center>';
369
370 return $data;
371}
372
373/**
374 * Hook render_daily.
375 *
376 * Template placeholders:
377 * - plugin_start_zone: page start.
378 * - plugin_end_zone: page end.
379 *
380 * Data:
381 * - _LOGGEDIN_: true/false
382 *
383 * @param array $data data passed to plugin
384 *
385 * @return array altered $data.
386 */
387function hook_demo_plugin_render_daily($data)
388{
786ddad9 389 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
786ddad9
A
390 $data['plugin_end_zone'][] = '<center>AFTER</center>';
391
392
393 // Manipulate columns data
50142efd 394 foreach ($data['linksToDisplay'] as &$value) {
395 $value['formatedDescription'] .= ' ಠ_ಠ';
786ddad9
A
396 }
397
398 // Add plugin content at the end of each link
50142efd 399 foreach ($data['linksToDisplay'] as &$value) {
400 $value['link_plugin'][] = 'DEMO';
786ddad9
A
401 }
402
403 return $data;
404}
405
406/*
407 * DATA SAVING HOOK.
408 */
409
410/**
411 * Hook savelink.
412 *
413 * Triggered when a link is save (new or edit).
414 * All new links now contain a 'stuff' value.
415 *
416 * @param array $data contains the new link data.
417 *
418 * @return array altered $data.
419 */
420function hook_demo_plugin_save_link($data)
421{
422
423 // Save stuff added in editlink field
424 if (!empty($_POST['lf_stuff'])) {
425 $data['stuff'] = escape($_POST['lf_stuff']);
426 }
427
428 return $data;
429}
430
431/**
432 * Hook delete_link.
433 *
434 * Triggered when a link is deleted.
435 *
436 * @param array $data contains the link to be deleted.
437 *
438 * @return array altered data.
439 */
440function hook_demo_plugin_delete_link($data)
441{
442 if (strpos($data['url'], 'youtube.com') !== false) {
443 exit('You can not delete a YouTube link. Don\'t ask.');
444 }
bcd078bf
A
445}
446
447/**
448 * Execute render_feed hook.
449 * Called with ATOM and RSS feed.
450 *
451 * Special data keys:
452 * - _PAGE_: current page
453 * - _LOGGEDIN_: true/false
454 *
455 * @param array $data data passed to plugin
456 *
457 * @return array altered $data.
458 */
459function hook_demo_plugin_render_feed($data)
460{
461 foreach ($data['links'] as &$link) {
1a8ac737 462 if ($data['_PAGE_'] == TemplatePage::FEED_ATOM) {
bcd078bf 463 $link['description'] .= ' - ATOM Feed' ;
1a8ac737 464 } elseif ($data['_PAGE_'] == TemplatePage::FEED_RSS) {
bcd078bf
A
465 $link['description'] .= ' - RSS Feed';
466 }
467 }
468 return $data;
469}
12266213 470
15a61e59
A
471/**
472 * When plugin parameters are saved.
473 *
474 * @param array $data $_POST array
475 *
476 * @return array Updated $_POST array
477 */
478function hook_demo_plugin_save_plugin_parameters($data)
479{
83ef0ff1
PB
480 // Here we edit the provided value.
481 // This hook can also be used to generate config files, etc.
15a61e59
A
482 if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) {
483 $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX';
484 }
485
486 return $data;
487}
488
12266213
A
489/**
490 * This function is never called, but contains translation calls for GNU gettext extraction.
491 */
492function demo_dummy_translation()
493{
494 // meta
495 t('A demo plugin covering all use cases for template designers and plugin developers.');
15a61e59
A
496 t('This is a parameter dedicated to the demo plugin. It\'ll be suffixed.');
497 t('Other demo parameter');
12266213 498}