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.
78 Special additional data are passed to every hook through the
79 `$data` parameter to give you access to additional context, and services.
83 * `_PAGE_` (string): if the current hook is used to render a template, its name is passed through this additional parameter.
84 * `_LOGGEDIN_` (bool): whether the user is logged in or not.
85 * `_BASE_PATH_` (string): if Shaarli instance is hosted under a subfolder, contains the subfolder path to `index.php` (e.g. `https://domain.tld/shaarli/` -> `/shaarli/`).
86 * `_BOOKMARK_SERVICE_` (`BookmarkServiceInterface`): bookmark service instance, for advanced usage.
91 if ($data['_PAGE_'] === TemplatePage::LINKLIST && $data['LOGGEDIN'] === true) {
92 // Do something for logged in users when the link list is rendered
96 #### Filling templates placeholder
98 Template placeholders are displayed in template in specific places.
100 RainTPL displays every element contained in the placeholder's array. These element can be added by plugins.
102 For example, let's add a value in the placeholder `top_placeholder` which is displayed at the top of my page:
105 $data['top_placeholder'][] = 'My content';
107 array_push($data['top_placeholder'], 'My', 'content');
112 #### Data manipulation
114 When a page is displayed, every variable send to the template engine is passed to plugins before that in `$data`.
116 The data contained by this array can be altered before template rendering.
118 For example, in linklist, it is possible to alter every title:
121 // mind the reference if you want $data to be altered
122 foreach ($data['links'] as &$value) {
123 // String reverse every title.
124 $value['title'] = strrev($value['title']);
132 Every plugin needs a `<plugin_name>.meta` file, which is in fact an `.ini` file (`KEY="VALUE"`), to be listed in plugin administration.
134 Each file contain two keys:
136 - `description`: plugin description
137 - `parameters`: user parameter names, separated by a `;`.
138 - `parameter.<PARAMETER_NAME>`: add a text description the specified parameter.
140 > Note: In PHP, `parse_ini_file()` seems to want strings to be between by quotes `"` in the ini file.
142 ### It's not working!
144 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
146 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
150 | Hooks | Description |
151 | ------------- |:-------------:|
152 | [render_header](#render_header) | Allow plugin to add content in page headers. |
153 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
154 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
155 | [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. |
156 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
157 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
158 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
159 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
160 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
161 | [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. |
162 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
163 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
164 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
165 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
171 Triggered on every page.
173 Allow plugin to add content in page headers.
177 `$data` is an array containing:
179 - [Special data](#special-data)
181 ##### Template placeholders
183 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
185 List of placeholders:
187 - `buttons_toolbar`: after the list of buttons in the header.
189 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
191 - `fields_toolbar`: after search fields in the header.
193 > Note: This will only be called in linklist.
195 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
199 Triggered on every page.
201 Allow plugin to include their own CSS files.
205 `$data` is an array containing:
207 - [Special data](#special-data)
209 ##### Template placeholders
211 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
213 List of placeholders:
215 - `css_files`: called after loading default CSS.
217 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
221 Triggered on every page.
223 Allow plugin to add content in page footer and include their own JS files.
227 `$data` is an array containing:
229 - [Special data](#special-data)
231 ##### Template placeholders
233 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
235 List of placeholders:
237 - `text`: called after the end of the footer text.
238 - `endofpage`: called at the end of the page.
240 ![text_example](http://i.imgur.com/L5S2YEH.png)
242 - `js_files`: called at the end of the page, to include custom JS scripts.
244 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
248 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
250 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
254 `$data` is an array containing:
256 - All templates data, including links.
257 - [Special data](#special-data)
259 ##### Template placeholders
261 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
263 List of placeholders:
265 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
267 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
269 - `link_plugin`: for every link, between permalink and link URL.
271 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
273 - `plugin_start_zone`: before displaying the template content.
275 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
277 - `plugin_end_zone`: after displaying the template content.
279 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
283 Triggered when the link edition form is displayed.
285 Allow to add fields in the form, or display elements.
289 `$data` is an array containing:
291 - All templates data.
292 - [Special data](#special-data)
294 ##### Template placeholders
296 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
298 List of placeholders:
300 - `edit_link_plugin`: after tags field.
302 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
306 Triggered when the "tools" page is displayed.
308 Allow to add content at the end of the page.
312 `$data` is an array containing:
314 - All templates data.
315 - [Special data](#special-data)
317 ##### Template placeholders
319 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
321 List of placeholders:
323 - `tools_plugin`: at the end of the page.
325 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
329 Triggered when picwall is displayed.
331 Allow to add content at the top and bottom of the page.
335 `$data` is an array containing:
337 - All templates data.
338 - [Special data](#special-data)
340 ##### Template placeholders
342 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
344 List of placeholders:
346 - `plugin_start_zone`: before displaying the template content.
347 - `plugin_end_zone`: after displaying the template content.
349 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
353 Triggered when tagcloud is displayed.
355 Allow to add content at the top and bottom of the page.
359 `$data` is an array containing:
361 - All templates data.
362 - [Special data](#special-data)
364 ##### Template placeholders
366 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
368 List of placeholders:
370 - `plugin_start_zone`: before displaying the template content.
371 - `plugin_end_zone`: after displaying the template content.
373 For each tag, the following placeholder can be used:
375 - `tag_plugin`: after each tag
377 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
382 Triggered when taglist is displayed.
384 Allow to add content at the top and bottom of the page.
388 `$data` is an array containing:
390 - All templates data.
391 - [Special data](#special-data)
393 ##### Template placeholders
395 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
397 List of placeholders:
399 - `plugin_start_zone`: before displaying the template content.
400 - `plugin_end_zone`: after displaying the template content.
402 For each tag, the following placeholder can be used:
404 - `tag_plugin`: after each tag
408 Triggered when tagcloud is displayed.
410 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
414 `$data` is an array containing:
416 - All templates data, including links.
417 - [Special data](#special-data)
419 ##### Template placeholders
421 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
423 List of placeholders:
425 - `link_plugin`: used at bottom of each link.
427 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
429 - `plugin_start_zone`: before displaying the template content.
430 - `plugin_end_zone`: after displaying the template content.
434 Triggered when the ATOM or RSS feed is displayed.
436 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
440 `$data` is an array containing:
442 - All templates data, including links.
443 - [Special data](#special-data)
445 ##### Template placeholders
447 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
449 List of placeholders:
451 - `feed_plugins_header`: used as a header tag in the feed.
455 - `feed_plugins`: additional tag for every link entry.
459 Triggered when a link is save (new link or edit).
461 Allow to alter the link being saved in the datastore.
465 `$data` is an array containing the link being saved:
477 Also [special data](#special-data).
482 Triggered when a link is deleted.
484 Allow to execute any action before the link is actually removed from the datastore
488 `$data` is an array containing the link being deleted:
500 Also [special data](#special-data).
502 #### save_plugin_parameters
504 Triggered when the plugin parameters are saved from the plugin administration page.
506 Plugins can perform an action every times their settings are updated.
507 For example it is used to update the CSS file of the `default_colors` plugins.
511 `$data` input contains the `$_POST` array.
513 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
514 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
516 Also [special data](#special-data).
518 ## Guide for template designer
520 ### Plugin administration
522 Your theme must include a plugin administration page: `pluginsadmin.html`.
524 > Note: repo's template link needs to be added when the PR is merged.
526 Use the default one as an example.
528 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
530 - you're using a table.
531 - you call orderUp() and orderUp() onclick on arrows.
532 - you add data-line and data-order to your rows.
534 Otherwise, you can use your own JS as long as this field is send by the form:
536 <input type="hidden" name="order_{$key}" value="{$counter}">
538 ### Placeholder system
540 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
542 It's a RainTPL loop like this:
544 {loop="$plugin_variable"}
548 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
550 ### List of placeholders
554 At the end of the menu:
556 {loop="$plugins_header.buttons_toolbar"}
560 At the end of file, before clearing floating blocks:
562 {if="!empty($plugin_errors) && $is_logged_in"}
564 {loop="plugin_errors"}
572 At the end of the file:
575 {loop="$plugins_includes.css_files"}
576 <link type="text/css" rel="stylesheet" href="{$value}#"/>
582 At the end of your footer notes:
585 {loop="$plugins_footer.text"}
593 {loop="$plugins_footer.js_files"}
594 <script src="{$value}#"></script>
603 {loop="$plugins_header.fields_toolbar"}
608 Before displaying the link list (after paging):
611 {loop="$plugin_start_zone"}
616 For every links (icons):
619 {loop="$value.link_plugin"}
620 <span>{$value}</span>
627 {loop="$plugin_end_zone"}
632 **linklist.paging.html**
634 After the "private only" icon:
637 {loop="$action_plugin"}
647 {loop="$edit_link_plugin"}
657 {loop="$tools_plugin"}
667 <div id="plugin_zone_start_picwall" class="plugin_zone">
668 {loop="$plugin_start_zone"}
677 <div id="plugin_zone_end_picwall" class="plugin_zone">
678 {loop="$plugin_end_zone"}
689 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
690 {loop="$plugin_start_zone"}
699 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
700 {loop="$plugin_end_zone"}
711 <div id="plugin_zone_start_picwall" class="plugin_zone">
712 {loop="$plugin_start_zone"}
721 <div class="dailyEntryFooter">
722 {loop="$link.link_plugin"}
731 <div id="plugin_zone_end_picwall" class="plugin_zone">
732 {loop="$plugin_end_zone"}
738 **feed.atom.xml** and **feed.rss.xml**:
740 In headers tags section:
742 {loop="$feed_plugins_header"}
749 {loop="$value.feed_plugins"}