1 [**I am a developer: ** Developer API](#developer-api)
3 [**I am a template designer: ** Guide for template designers](#guide-for-template-designer)
9 ### What can I do with plugins?
11 The plugin system let you:
13 - insert content into specific places across templates.
14 - alter data before templates rendering.
15 - alter data before saving new links.
17 ### How can I create a plugin for Shaarli?
19 First, chose a plugin name, such as `demo_plugin`.
21 Under `plugin` folder, create a folder named with your plugin name. Then create a <plugin_name>.meta file and a <plugin_name>.php file in that folder.
23 You should have the following tree view:
29 | |---| demo_plugin.meta
30 | |---| demo_plugin.php
33 ### Plugin initialization
35 At the beginning of Shaarli execution, all enabled plugins are loaded. At this point, the plugin system looks for an `init()` function in the <plugin_name>.php to execute and run it if it exists. This function must be named this way, and takes the `ConfigManager` as parameter.
37 <plugin_name>_init($conf)
39 This function can be used to create initial data, load default settings, etc. But also to set *plugin errors*. If the initialization function returns an array of strings, they will be understand as errors, and displayed in the header to logged in users.
41 The plugin system also looks for a `description` variable in the <plugin_name>.meta file, to be displayed in the plugin administration page.
43 description="The plugin does this and that."
45 ### Understanding hooks
47 A plugin is a set of functions. Each function will be triggered by the plugin system at certain point in Shaarli execution.
49 These functions need to be named with this pattern:
52 hook_<plugin_name>_<hook_name>($data, $conf)
57 - data: see [$data section](https://shaarli.readthedocs.io/en/master/Plugin-System/#plugins-data)
58 - conf: the `ConfigManager` instance.
60 For example, if my plugin want to add data to the header, this function is needed:
62 hook_demo_plugin_render_header
64 If this function is declared, and the plugin enabled, it will be called every time Shaarli is rendering the header.
70 Every hook function has a `$data` parameter. Its content differs for each hooks.
72 **This parameter needs to be returned every time**, otherwise data is lost.
76 #### Filling templates placeholder
78 Template placeholders are displayed in template in specific places.
80 RainTPL displays every element contained in the placeholder's array. These element can be added by plugins.
82 For example, let's add a value in the placeholder `top_placeholder` which is displayed at the top of my page:
85 $data['top_placeholder'][] = 'My content';
87 array_push($data['top_placeholder'], 'My', 'content');
92 #### Data manipulation
94 When a page is displayed, every variable send to the template engine is passed to plugins before that in `$data`.
96 The data contained by this array can be altered before template rendering.
98 For exemple, in linklist, it is possible to alter every title:
101 // mind the reference if you want $data to be altered
102 foreach ($data['links'] as &$value) {
103 // String reverse every title.
104 $value['title'] = strrev($value['title']);
112 Every plugin needs a `<plugin_name>.meta` file, which is in fact an `.ini` file (`KEY="VALUE"`), to be listed in plugin administration.
114 Each file contain two keys:
116 - `description`: plugin description
117 - `parameters`: user parameter names, separated by a `;`.
118 - `parameter.<PARAMETER_NAME>`: add a text description the specified parameter.
120 > Note: In PHP, `parse_ini_file()` seems to want strings to be between by quotes `"` in the ini file.
122 ### It's not working!
124 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
126 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
130 | Hooks | Description |
131 | ------------- |:-------------:|
132 | [render_header](#render_header) | Allow plugin to add content in page headers. |
133 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
134 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
135 | [render_linklist](#render_linklist) | It allows to add content at the begining and end of the page, after every link displayed and to alter link data. |
136 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
137 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
138 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
139 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
140 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
141 | [render_daily](#render_daily) | Allow to add content at the top and bottom of the page, the bottom of each link and to alter data. |
142 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
143 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
144 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
145 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
151 Triggered on every page.
153 Allow plugin to add content in page headers.
157 `$data` is an array containing:
159 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
160 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
162 ##### Template placeholders
164 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
166 List of placeholders:
168 - `buttons_toolbar`: after the list of buttons in the header.
170 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
172 - `fields_toolbar`: after search fields in the header.
174 > Note: This will only be called in linklist.
176 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
180 Triggered on every page.
182 Allow plugin to include their own CSS files.
186 `$data` is an array containing:
188 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
189 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
191 ##### Template placeholders
193 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
195 List of placeholders:
197 - `css_files`: called after loading default CSS.
199 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
203 Triggered on every page.
205 Allow plugin to add content in page footer and include their own JS files.
209 `$data` is an array containing:
211 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
212 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
214 ##### Template placeholders
216 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
218 List of placeholders:
220 - `text`: called after the end of the footer text.
221 - `endofpage`: called at the end of the page.
223 ![text_example](http://i.imgur.com/L5S2YEH.png)
225 - `js_files`: called at the end of the page, to include custom JS scripts.
227 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
231 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
233 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
237 `$data` is an array containing:
239 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
240 - All templates data, including links.
242 ##### Template placeholders
244 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
246 List of placeholders:
248 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
250 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
252 - `link_plugin`: for every link, between permalink and link URL.
254 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
256 - `plugin_start_zone`: before displaying the template content.
258 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
260 - `plugin_end_zone`: after displaying the template content.
262 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
266 Triggered when the link edition form is displayed.
268 Allow to add fields in the form, or display elements.
272 `$data` is an array containing:
274 - All templates data.
276 ##### Template placeholders
278 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
280 List of placeholders:
282 - `edit_link_plugin`: after tags field.
284 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
288 Triggered when the "tools" page is displayed.
290 Allow to add content at the end of the page.
294 `$data` is an array containing:
296 - All templates data.
298 ##### Template placeholders
300 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
302 List of placeholders:
304 - `tools_plugin`: at the end of the page.
306 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
310 Triggered when picwall is displayed.
312 Allow to add content at the top and bottom of the page.
316 `$data` is an array containing:
318 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
319 - All templates data.
321 ##### Template placeholders
323 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
325 List of placeholders:
327 - `plugin_start_zone`: before displaying the template content.
328 - `plugin_end_zone`: after displaying the template content.
330 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
334 Triggered when tagcloud is displayed.
336 Allow to add content at the top and bottom of the page.
340 `$data` is an array containing:
342 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
343 - All templates data.
345 ##### Template placeholders
347 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
349 List of placeholders:
351 - `plugin_start_zone`: before displaying the template content.
352 - `plugin_end_zone`: after displaying the template content.
354 For each tag, the following placeholder can be used:
356 - `tag_plugin`: after each tag
358 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
363 Triggered when taglist is displayed.
365 Allow to add content at the top and bottom of the page.
369 `$data` is an array containing:
371 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
372 - All templates data.
374 ##### Template placeholders
376 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
378 List of placeholders:
380 - `plugin_start_zone`: before displaying the template content.
381 - `plugin_end_zone`: after displaying the template content.
383 For each tag, the following placeholder can be used:
385 - `tag_plugin`: after each tag
389 Triggered when tagcloud is displayed.
391 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
395 `$data` is an array containing:
397 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
398 - All templates data, including links.
400 ##### Template placeholders
402 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
404 List of placeholders:
406 - `link_plugin`: used at bottom of each link.
408 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
410 - `plugin_start_zone`: before displaying the template content.
411 - `plugin_end_zone`: after displaying the template content.
415 Triggered when the ATOM or RSS feed is displayed.
417 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
421 `$data` is an array containing:
423 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
424 - `_PAGE_`: containing either `rss` or `atom`.
425 - All templates data, including links.
427 ##### Template placeholders
429 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
431 List of placeholders:
433 - `feed_plugins_header`: used as a header tag in the feed.
437 - `feed_plugins`: additional tag for every link entry.
441 Triggered when a link is save (new link or edit).
443 Allow to alter the link being saved in the datastore.
447 `$data` is an array containing the link being saved:
462 Triggered when a link is deleted.
464 Allow to execute any action before the link is actually removed from the datastore
468 `$data` is an array containing the link being saved:
481 #### save_plugin_parameters
483 Triggered when the plugin parameters are saved from the plugin administration page.
485 Plugins can perform an action every times their settings are updated.
486 For example it is used to update the CSS file of the `default_colors` plugins.
490 `$data` input contains the `$_POST` array.
492 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
493 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
496 ## Guide for template designer
498 ### Plugin administration
500 Your theme must include a plugin administration page: `pluginsadmin.html`.
502 > Note: repo's template link needs to be added when the PR is merged.
504 Use the default one as an example.
506 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
508 - you're using a table.
509 - you call orderUp() and orderUp() onclick on arrows.
510 - you add data-line and data-order to your rows.
512 Otherwise, you can use your own JS as long as this field is send by the form:
514 <input type="hidden" name="order_{$key}" value="{$counter}">
516 ### Placeholder system
518 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
520 It's a RainTPL loop like this:
522 {loop="$plugin_variable"}
526 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
528 ### List of placeholders
532 At the end of the menu:
534 {loop="$plugins_header.buttons_toolbar"}
538 At the end of file, before clearing floating blocks:
540 {if="!empty($plugin_errors) && isLoggedIn()"}
542 {loop="plugin_errors"}
550 At the end of the file:
553 {loop="$plugins_includes.css_files"}
554 <link type="text/css" rel="stylesheet" href="{$value}#"/>
560 At the end of your footer notes:
563 {loop="$plugins_footer.text"}
571 {loop="$plugins_footer.js_files"}
572 <script src="{$value}#"></script>
581 {loop="$plugins_header.fields_toolbar"}
586 Before displaying the link list (after paging):
589 {loop="$plugin_start_zone"}
594 For every links (icons):
597 {loop="$value.link_plugin"}
598 <span>{$value}</span>
605 {loop="$plugin_end_zone"}
610 **linklist.paging.html**
612 After the "private only" icon:
615 {loop="$action_plugin"}
625 {loop="$edit_link_plugin"}
635 {loop="$tools_plugin"}
645 <div id="plugin_zone_start_picwall" class="plugin_zone">
646 {loop="$plugin_start_zone"}
655 <div id="plugin_zone_end_picwall" class="plugin_zone">
656 {loop="$plugin_end_zone"}
667 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
668 {loop="$plugin_start_zone"}
677 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
678 {loop="$plugin_end_zone"}
689 <div id="plugin_zone_start_picwall" class="plugin_zone">
690 {loop="$plugin_start_zone"}
699 <div class="dailyEntryFooter">
700 {loop="$link.link_plugin"}
709 <div id="plugin_zone_end_picwall" class="plugin_zone">
710 {loop="$plugin_end_zone"}
716 **feed.atom.xml** and **feed.rss.xml**:
718 In headers tags section:
720 {loop="$feed_plugins_header"}
727 {loop="$value.feed_plugins"}