5 ### What can I do with plugins?
7 The plugin system lets you:
9 - insert content into specific places across templates.
10 - alter data before templates rendering.
11 - alter data before saving new links.
14 ### How can I create a plugin for Shaarli?
16 First, chose a plugin name, such as `demo_plugin`.
18 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.
20 You should have the following tree view:
26 | |---| demo_plugin.meta
27 | |---| demo_plugin.php
31 ### Plugin initialization
33 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.
35 <plugin_name>_init($conf)
37 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.
39 The plugin system also looks for a `description` variable in the <plugin_name>.meta file, to be displayed in the plugin administration page.
41 description="The plugin does this and that."
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.
69 Every hook function has a `$data` parameter. Its content differs for each hooks.
71 **This parameter needs to be returned every time**, otherwise data is lost.
77 Special additional data are passed to every hook through the
78 `$data` parameter to give you access to additional context, and services.
82 * `_PAGE_` (string): if the current hook is used to render a template, its name is passed through this additional parameter.
83 * `_LOGGEDIN_` (bool): whether the user is logged in or not.
84 * `_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/`).
85 * `_BOOKMARK_SERVICE_` (`BookmarkServiceInterface`): bookmark service instance, for advanced usage.
90 if ($data['_PAGE_'] === TemplatePage::LINKLIST && $data['LOGGEDIN'] === true) {
91 // Do something for logged in users when the link list is rendered
95 #### Filling templates placeholder
97 Template placeholders are displayed in template in specific places.
99 RainTPL displays every element contained in the placeholder's array. These element can be added by plugins.
101 For example, let's add a value in the placeholder `top_placeholder` which is displayed at the top of my page:
104 $data['top_placeholder'][] = 'My content';
106 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 ### Understanding relative paths
144 Because Shaarli is a self-hosted tool, an instance can either be installed at the root directory, or under a subfolder.
145 This means that you can *never* use absolute paths (eg `/plugins/mything/file.png`).
147 If a file needs to be included in server end, use simple relative path:
148 `PluginManager::$PLUGINS_PATH . '/mything/template.html'`.
150 If it needs to be included in front end side (e.g. an image),
151 the relative path must be prefixed with special data `_BASE_PATH_`:
152 `($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH . '/mything/picture.png`.
154 Note that special placeholders for CSS and JS files (respectively `css_files` and `js_files`) are already prefixed
155 with the base path in template files.
157 ### It's not working!
159 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
161 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
166 | Hooks | Description |
167 | ------------- |:-------------:|
168 | [render_header](#render_header) | Allow plugin to add content in page headers. |
169 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
170 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
171 | [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. |
172 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
173 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
174 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
175 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
176 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
177 | [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. |
178 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
179 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
180 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
181 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
186 Triggered on every page - allows plugins to add content in page headers.
191 `$data` is an array containing:
193 - [Special data](#special-data)
195 ##### Template placeholders
197 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
199 List of placeholders:
201 - `buttons_toolbar`: after the list of buttons in the header.
203 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
205 - `fields_toolbar`: after search fields in the header.
207 > Note: This will only be called in linklist.
209 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
214 Triggered on every page - allows plugins to include their own CSS files.
218 `$data` is an array containing:
220 - [Special data](#special-data)
222 ##### Template placeholders
224 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
226 List of placeholders:
228 - `css_files`: called after loading default CSS.
230 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
235 Triggered on every page.
237 Allow plugin to add content in page footer and include their own JS files.
241 `$data` is an array containing:
243 - [Special data](#special-data)
245 ##### Template placeholders
247 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
249 List of placeholders:
251 - `text`: called after the end of the footer text.
252 - `endofpage`: called at the end of the page.
254 ![text_example](http://i.imgur.com/L5S2YEH.png)
256 - `js_files`: called at the end of the page, to include custom JS scripts.
258 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
263 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
265 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
269 `$data` is an array containing:
271 - All templates data, including links.
272 - [Special data](#special-data)
274 ##### template placeholders
276 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
278 List of placeholders:
280 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
282 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
284 - `link_plugin`: for every link, between permalink and link URL.
286 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
288 - `plugin_start_zone`: before displaying the template content.
290 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
292 - `plugin_end_zone`: after displaying the template content.
294 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
299 Triggered when the link edition form is displayed.
301 Allow to add fields in the form, or display elements.
305 `$data` is an array containing:
307 - All templates data.
308 - [Special data](#special-data)
310 ##### template placeholders
312 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
314 List of placeholders:
316 - `edit_link_plugin`: after tags field.
318 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
323 Triggered when the "tools" page is displayed.
325 Allow to add content at the end of the page.
329 `$data` is an array containing:
331 - All templates data.
332 - [Special data](#special-data)
334 ##### template placeholders
336 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
338 List of placeholders:
340 - `tools_plugin`: at the end of the page.
342 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
347 Triggered when picwall is displayed.
349 Allow to add content at the top and bottom of the page.
353 `$data` is an array containing:
355 - All templates data.
356 - [Special data](#special-data)
358 ##### template placeholders
360 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
362 List of placeholders:
364 - `plugin_start_zone`: before displaying the template content.
365 - `plugin_end_zone`: after displaying the template content.
367 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
372 Triggered when tagcloud is displayed.
374 Allow to add content at the top and bottom of the page.
378 `$data` is an array containing:
380 - All templates data.
381 - [Special data](#special-data)
383 ##### Template placeholders
385 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
387 List of placeholders:
389 - `plugin_start_zone`: before displaying the template content.
390 - `plugin_end_zone`: after displaying the template content.
392 For each tag, the following placeholder can be used:
394 - `tag_plugin`: after each tag
396 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
401 Triggered when taglist is displayed - allows to add content at the top and bottom of the page.
405 `$data` is an array containing:
407 - All templates data.
408 - [Special data](#special-data)
410 ##### Template placeholders
412 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
414 List of placeholders:
416 - `plugin_start_zone`: before displaying the template content.
417 - `plugin_end_zone`: after displaying the template content.
419 For each tag, the following placeholder can be used:
421 - `tag_plugin`: after each tag
425 Triggered when tagcloud is displayed.
427 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
432 `$data` is an array containing:
434 - All templates data, including links.
435 - [Special data](#special-data)
437 ##### Template placeholders
439 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
441 List of placeholders:
443 - `link_plugin`: used at bottom of each link.
445 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
447 - `plugin_start_zone`: before displaying the template content.
448 - `plugin_end_zone`: after displaying the template content.
453 Triggered when the ATOM or RSS feed is displayed.
455 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
459 `$data` is an array containing:
461 - All templates data, including links.
462 - [Special data](#special-data)
464 ##### Template placeholders
466 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
468 List of placeholders:
470 - `feed_plugins_header`: used as a header tag in the feed.
474 - `feed_plugins`: additional tag for every link entry.
479 Triggered when a link is save (new link or edit).
481 Allow to alter the link being saved in the datastore.
485 `$data` is an array containing the link being saved:
497 Also [special data](#special-data).
502 Triggered when a link is deleted.
504 Allow to execute any action before the link is actually removed from the datastore
508 `$data` is an array containing the link being deleted:
520 Also [special data](#special-data).
522 #### save_plugin_parameters
524 Triggered when the plugin parameters are saved from the plugin administration page.
526 Plugins can perform an action every times their settings are updated.
527 For example it is used to update the CSS file of the `default_colors` plugins.
531 `$data` input contains the `$_POST` array.
533 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
534 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
536 Also [special data](#special-data).
538 ## Guide for template designers
540 ### Plugin administration
542 Your theme must include a plugin administration page: `pluginsadmin.html`.
544 > Note: repo's template link needs to be added when the PR is merged.
546 Use the default one as an example.
548 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
550 - you're using a table.
551 - you call orderUp() and orderUp() onclick on arrows.
552 - you add data-line and data-order to your rows.
554 Otherwise, you can use your own JS as long as this field is send by the form:
556 <input type="hidden" name="order_{$key}" value="{$counter}">
558 ### Placeholder system
560 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
562 It's a RainTPL loop like this:
564 {loop="$plugin_variable"}
568 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
570 ### List of placeholders
574 At the end of the menu:
576 {loop="$plugins_header.buttons_toolbar"}
580 At the end of file, before clearing floating blocks:
582 {if="!empty($plugin_errors) && $is_logged_in"}
584 {loop="plugin_errors"}
592 At the end of the file:
595 {loop="$plugins_includes.css_files"}
596 <link type="text/css" rel="stylesheet" href="{$value}#"/>
602 At the end of your footer notes:
605 {loop="$plugins_footer.text"}
613 {loop="$plugins_footer.js_files"}
614 <script src="{$value}#"></script>
623 {loop="$plugins_header.fields_toolbar"}
628 Before displaying the link list (after paging):
631 {loop="$plugin_start_zone"}
636 For every links (icons):
639 {loop="$value.link_plugin"}
640 <span>{$value}</span>
647 {loop="$plugin_end_zone"}
652 **linklist.paging.html**
654 After the "private only" icon:
657 {loop="$action_plugin"}
667 {loop="$edit_link_plugin"}
677 {loop="$tools_plugin"}
687 <div id="plugin_zone_start_picwall" class="plugin_zone">
688 {loop="$plugin_start_zone"}
697 <div id="plugin_zone_end_picwall" class="plugin_zone">
698 {loop="$plugin_end_zone"}
709 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
710 {loop="$plugin_start_zone"}
719 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
720 {loop="$plugin_end_zone"}
731 <div id="plugin_zone_start_picwall" class="plugin_zone">
732 {loop="$plugin_start_zone"}
741 <div class="dailyEntryFooter">
742 {loop="$link.link_plugin"}
751 <div id="plugin_zone_end_picwall" class="plugin_zone">
752 {loop="$plugin_end_zone"}
758 **feed.atom.xml** and **feed.rss.xml**:
760 In headers tags section:
762 {loop="$feed_plugins_header"}
769 {loop="$value.feed_plugins"}