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 ### Understanding hooks
45 A plugin is a set of functions. Each function will be triggered by the plugin system at certain point in Shaarli execution.
47 These functions need to be named with this pattern:
50 hook_<plugin_name>_<hook_name>($data, $conf)
55 - data: see [$data section](https://shaarli.readthedocs.io/en/master/Plugin-System/#plugins-data)
56 - conf: the `ConfigManager` instance.
58 For example, if my plugin want to add data to the header, this function is needed:
60 hook_demo_plugin_render_header
62 If this function is declared, and the plugin enabled, it will be called every time Shaarli is rendering the header.
68 Every hook function has a `$data` parameter. Its content differs for each hooks.
70 **This parameter needs to be returned every time**, otherwise data is lost.
74 #### Filling templates placeholder
76 Template placeholders are displayed in template in specific places.
78 RainTPL displays every element contained in the placeholder's array. These element can be added by plugins.
80 For example, let's add a value in the placeholder `top_placeholder` which is displayed at the top of my page:
83 $data['top_placeholder'][] = 'My content';
85 array_push($data['top_placeholder'], 'My', 'content');
90 #### Data manipulation
92 When a page is displayed, every variable send to the template engine is passed to plugins before that in `$data`.
94 The data contained by this array can be altered before template rendering.
96 For exemple, in linklist, it is possible to alter every title:
99 // mind the reference if you want $data to be altered
100 foreach ($data['links'] as &$value) {
101 // String reverse every title.
102 $value['title'] = strrev($value['title']);
110 Every plugin needs a `<plugin_name>.meta` file, which is in fact an `.ini` file (`KEY="VALUE"`), to be listed in plugin administration.
112 Each file contain two keys:
114 - `description`: plugin description
115 - `parameters`: user parameter names, separated by a `;`.
116 - `parameter.<PARAMETER_NAME>`: add a text description the specified parameter.
118 > Note: In PHP, `parse_ini_file()` seems to want strings to be between by quotes `"` in the ini file.
120 ### It's not working!
122 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
124 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
128 | Hooks | Description |
129 | ------------- |:-------------:|
130 | [render_header](#render_header) | Allow plugin to add content in page headers. |
131 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
132 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
133 | [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. |
134 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
135 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
136 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
137 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
138 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
139 | [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. |
140 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
141 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
142 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
143 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
149 Triggered on every page.
151 Allow plugin to add content in page headers.
155 `$data` is an array containing:
157 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
158 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
160 ##### Template placeholders
162 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
164 List of placeholders:
166 - `buttons_toolbar`: after the list of buttons in the header.
168 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
170 - `fields_toolbar`: after search fields in the header.
172 > Note: This will only be called in linklist.
174 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
178 Triggered on every page.
180 Allow plugin to include their own CSS files.
184 `$data` is an array containing:
186 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
187 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
189 ##### Template placeholders
191 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
193 List of placeholders:
195 - `css_files`: called after loading default CSS.
197 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
201 Triggered on every page.
203 Allow plugin to add content in page footer and include their own JS files.
207 `$data` is an array containing:
209 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
210 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
212 ##### Template placeholders
214 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
216 List of placeholders:
218 - `text`: called after the end of the footer text.
219 - `endofpage`: called at the end of the page.
221 ![text_example](http://i.imgur.com/L5S2YEH.png)
223 - `js_files`: called at the end of the page, to include custom JS scripts.
225 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
229 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
231 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
235 `$data` is an array containing:
237 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
238 - All templates data, including links.
240 ##### Template placeholders
242 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
244 List of placeholders:
246 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
248 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
250 - `link_plugin`: for every link, between permalink and link URL.
252 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
254 - `plugin_start_zone`: before displaying the template content.
256 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
258 - `plugin_end_zone`: after displaying the template content.
260 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
264 Triggered when the link edition form is displayed.
266 Allow to add fields in the form, or display elements.
270 `$data` is an array containing:
272 - All templates data.
274 ##### Template placeholders
276 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
278 List of placeholders:
280 - `edit_link_plugin`: after tags field.
282 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
286 Triggered when the "tools" page is displayed.
288 Allow to add content at the end of the page.
292 `$data` is an array containing:
294 - All templates data.
296 ##### Template placeholders
298 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
300 List of placeholders:
302 - `tools_plugin`: at the end of the page.
304 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
308 Triggered when picwall is displayed.
310 Allow to add content at the top and bottom of the page.
314 `$data` is an array containing:
316 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
317 - All templates data.
319 ##### Template placeholders
321 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
323 List of placeholders:
325 - `plugin_start_zone`: before displaying the template content.
326 - `plugin_end_zone`: after displaying the template content.
328 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
332 Triggered when tagcloud is displayed.
334 Allow to add content at the top and bottom of the page.
338 `$data` is an array containing:
340 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
341 - All templates data.
343 ##### Template placeholders
345 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
347 List of placeholders:
349 - `plugin_start_zone`: before displaying the template content.
350 - `plugin_end_zone`: after displaying the template content.
352 For each tag, the following placeholder can be used:
354 - `tag_plugin`: after each tag
356 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
361 Triggered when taglist is displayed.
363 Allow to add content at the top and bottom of the page.
367 `$data` is an array containing:
369 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
370 - All templates data.
372 ##### Template placeholders
374 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
376 List of placeholders:
378 - `plugin_start_zone`: before displaying the template content.
379 - `plugin_end_zone`: after displaying the template content.
381 For each tag, the following placeholder can be used:
383 - `tag_plugin`: after each tag
387 Triggered when tagcloud is displayed.
389 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
393 `$data` is an array containing:
395 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
396 - All templates data, including links.
398 ##### Template placeholders
400 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
402 List of placeholders:
404 - `link_plugin`: used at bottom of each link.
406 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
408 - `plugin_start_zone`: before displaying the template content.
409 - `plugin_end_zone`: after displaying the template content.
413 Triggered when the ATOM or RSS feed is displayed.
415 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
419 `$data` is an array containing:
421 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
422 - `_PAGE_`: containing either `rss` or `atom`.
423 - All templates data, including links.
425 ##### Template placeholders
427 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
429 List of placeholders:
431 - `feed_plugins_header`: used as a header tag in the feed.
435 - `feed_plugins`: additional tag for every link entry.
439 Triggered when a link is save (new link or edit).
441 Allow to alter the link being saved in the datastore.
445 `$data` is an array containing the link being saved:
460 Triggered when a link is deleted.
462 Allow to execute any action before the link is actually removed from the datastore
466 `$data` is an array containing the link being saved:
479 #### save_plugin_parameters
481 Triggered when the plugin parameters are saved from the plugin administration page.
483 Plugins can perform an action every times their settings are updated.
484 For example it is used to update the CSS file of the `default_colors` plugins.
488 `$data` input contains the `$_POST` array.
490 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
491 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
494 ## Guide for template designer
496 ### Plugin administration
498 Your theme must include a plugin administration page: `pluginsadmin.html`.
500 > Note: repo's template link needs to be added when the PR is merged.
502 Use the default one as an example.
504 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
506 - you're using a table.
507 - you call orderUp() and orderUp() onclick on arrows.
508 - you add data-line and data-order to your rows.
510 Otherwise, you can use your own JS as long as this field is send by the form:
512 <input type="hidden" name="order_{$key}" value="{$counter}">
514 ### Placeholder system
516 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
518 It's a RainTPL loop like this:
520 {loop="$plugin_variable"}
524 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
526 ### List of placeholders
530 At the end of the menu:
532 {loop="$plugins_header.buttons_toolbar"}
536 At the end of file, before clearing floating blocks:
538 {if="!empty($plugin_errors) && isLoggedIn()"}
540 {loop="plugin_errors"}
548 At the end of the file:
551 {loop="$plugins_includes.css_files"}
552 <link type="text/css" rel="stylesheet" href="{$value}#"/>
558 At the end of your footer notes:
561 {loop="$plugins_footer.text"}
569 {loop="$plugins_footer.js_files"}
570 <script src="{$value}#"></script>
579 {loop="$plugins_header.fields_toolbar"}
584 Before displaying the link list (after paging):
587 {loop="$plugin_start_zone"}
592 For every links (icons):
595 {loop="$value.link_plugin"}
596 <span>{$value}</span>
603 {loop="$plugin_end_zone"}
608 **linklist.paging.html**
610 After the "private only" icon:
613 {loop="$action_plugin"}
623 {loop="$edit_link_plugin"}
633 {loop="$tools_plugin"}
643 <div id="plugin_zone_start_picwall" class="plugin_zone">
644 {loop="$plugin_start_zone"}
653 <div id="plugin_zone_end_picwall" class="plugin_zone">
654 {loop="$plugin_end_zone"}
665 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
666 {loop="$plugin_start_zone"}
675 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
676 {loop="$plugin_end_zone"}
687 <div id="plugin_zone_start_picwall" class="plugin_zone">
688 {loop="$plugin_start_zone"}
697 <div class="dailyEntryFooter">
698 {loop="$link.link_plugin"}
707 <div id="plugin_zone_end_picwall" class="plugin_zone">
708 {loop="$plugin_end_zone"}
714 **feed.atom.xml** and **feed.rss.xml**:
716 In headers tags section:
718 {loop="$feed_plugins_header"}
725 {loop="$value.feed_plugins"}