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:
153 * if it's a link that will need to be processed by Shaarli, use `_BASE_PATH_`:
154 for e.g. `$data['_BASE_PATH_'] . '/admin/tools`.
155 * if you want to include an asset, you need to add the root URL (base path without `/index.php`, for people using Shaarli without URL rewriting), then use `_ROOT_PATH_`:
157 `$['_ROOT_PATH_'] . '/' . PluginManager::$PLUGINS_PATH . '/mything/picture.png`.
159 Note that special placeholders for CSS and JS files (respectively `css_files` and `js_files`) are already prefixed
160 with the root path in template files.
162 ### It's not working!
164 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
166 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
171 | Hooks | Description |
172 | ------------- |:-------------:|
173 | [render_header](#render_header) | Allow plugin to add content in page headers. |
174 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
175 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
176 | [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. |
177 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
178 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
179 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
180 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
181 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
182 | [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. |
183 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
184 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
185 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
186 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
191 Triggered on every page - allows plugins to add content in page headers.
196 `$data` is an array containing:
198 - [Special data](#special-data)
200 ##### Template placeholders
202 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
204 List of placeholders:
206 - `buttons_toolbar`: after the list of buttons in the header.
208 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
210 - `fields_toolbar`: after search fields in the header.
212 > Note: This will only be called in linklist.
214 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
219 Triggered on every page - allows plugins to include their own CSS files.
223 `$data` is an array containing:
225 - [Special data](#special-data)
227 ##### Template placeholders
229 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
231 List of placeholders:
233 - `css_files`: called after loading default CSS.
235 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
240 Triggered on every page.
242 Allow plugin to add content in page footer and include their own JS files.
246 `$data` is an array containing:
248 - [Special data](#special-data)
250 ##### Template placeholders
252 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
254 List of placeholders:
256 - `text`: called after the end of the footer text.
257 - `endofpage`: called at the end of the page.
259 ![text_example](http://i.imgur.com/L5S2YEH.png)
261 - `js_files`: called at the end of the page, to include custom JS scripts.
263 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
268 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
270 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
274 `$data` is an array containing:
276 - All templates data, including links.
277 - [Special data](#special-data)
279 ##### template placeholders
281 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
283 List of placeholders:
285 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
287 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
289 - `link_plugin`: for every link, between permalink and link URL.
291 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
293 - `plugin_start_zone`: before displaying the template content.
295 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
297 - `plugin_end_zone`: after displaying the template content.
299 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
304 Triggered when the link edition form is displayed.
306 Allow to add fields in the form, or display elements.
310 `$data` is an array containing:
312 - All templates data.
313 - [Special data](#special-data)
315 ##### template placeholders
317 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
319 List of placeholders:
321 - `edit_link_plugin`: after tags field.
323 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
328 Triggered when the "tools" page is displayed.
330 Allow to add content at the end of the page.
334 `$data` is an array containing:
336 - All templates data.
337 - [Special data](#special-data)
339 ##### template placeholders
341 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
343 List of placeholders:
345 - `tools_plugin`: at the end of the page.
347 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
352 Triggered when picwall is displayed.
354 Allow to add content at the top and bottom of the page.
358 `$data` is an array containing:
360 - All templates data.
361 - [Special data](#special-data)
363 ##### template placeholders
365 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
367 List of placeholders:
369 - `plugin_start_zone`: before displaying the template content.
370 - `plugin_end_zone`: after displaying the template content.
372 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
377 Triggered when tagcloud is displayed.
379 Allow to add content at the top and bottom of the page.
383 `$data` is an array containing:
385 - All templates data.
386 - [Special data](#special-data)
388 ##### Template placeholders
390 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
392 List of placeholders:
394 - `plugin_start_zone`: before displaying the template content.
395 - `plugin_end_zone`: after displaying the template content.
397 For each tag, the following placeholder can be used:
399 - `tag_plugin`: after each tag
401 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
406 Triggered when taglist is displayed - allows to add content at the top and bottom of the page.
410 `$data` is an array containing:
412 - All templates data.
413 - [Special data](#special-data)
415 ##### Template placeholders
417 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
419 List of placeholders:
421 - `plugin_start_zone`: before displaying the template content.
422 - `plugin_end_zone`: after displaying the template content.
424 For each tag, the following placeholder can be used:
426 - `tag_plugin`: after each tag
430 Triggered when tagcloud is displayed.
432 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
437 `$data` is an array containing:
439 - All templates data, including links.
440 - [Special data](#special-data)
442 ##### Template placeholders
444 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
446 List of placeholders:
448 - `link_plugin`: used at bottom of each link.
450 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
452 - `plugin_start_zone`: before displaying the template content.
453 - `plugin_end_zone`: after displaying the template content.
458 Triggered when the ATOM or RSS feed is displayed.
460 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
464 `$data` is an array containing:
466 - All templates data, including links.
467 - [Special data](#special-data)
469 ##### Template placeholders
471 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
473 List of placeholders:
475 - `feed_plugins_header`: used as a header tag in the feed.
479 - `feed_plugins`: additional tag for every link entry.
484 Triggered when a link is save (new link or edit).
486 Allow to alter the link being saved in the datastore.
490 `$data` is an array containing the link being saved:
502 Also [special data](#special-data).
507 Triggered when a link is deleted.
509 Allow to execute any action before the link is actually removed from the datastore
513 `$data` is an array containing the link being deleted:
525 Also [special data](#special-data).
527 #### save_plugin_parameters
529 Triggered when the plugin parameters are saved from the plugin administration page.
531 Plugins can perform an action every times their settings are updated.
532 For example it is used to update the CSS file of the `default_colors` plugins.
536 `$data` input contains the `$_POST` array.
538 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
539 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
541 Also [special data](#special-data).
543 ## Guide for template designers
545 ### Plugin administration
547 Your theme must include a plugin administration page: `pluginsadmin.html`.
549 > Note: repo's template link needs to be added when the PR is merged.
551 Use the default one as an example.
553 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
555 - you're using a table.
556 - you call orderUp() and orderUp() onclick on arrows.
557 - you add data-line and data-order to your rows.
559 Otherwise, you can use your own JS as long as this field is send by the form:
561 <input type="hidden" name="order_{$key}" value="{$counter}">
563 ### Placeholder system
565 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
567 It's a RainTPL loop like this:
569 {loop="$plugin_variable"}
573 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
575 ### List of placeholders
579 At the end of the menu:
581 {loop="$plugins_header.buttons_toolbar"}
585 At the end of file, before clearing floating blocks:
587 {if="!empty($plugin_errors) && $is_logged_in"}
589 {loop="plugin_errors"}
597 At the end of the file:
600 {loop="$plugins_includes.css_files"}
601 <link type="text/css" rel="stylesheet" href="{$value}#"/>
607 At the end of your footer notes:
610 {loop="$plugins_footer.text"}
618 {loop="$plugins_footer.js_files"}
619 <script src="{$value}#"></script>
628 {loop="$plugins_header.fields_toolbar"}
633 Before displaying the link list (after paging):
636 {loop="$plugin_start_zone"}
641 For every links (icons):
644 {loop="$value.link_plugin"}
645 <span>{$value}</span>
652 {loop="$plugin_end_zone"}
657 **linklist.paging.html**
659 After the "private only" icon:
662 {loop="$action_plugin"}
672 {loop="$edit_link_plugin"}
682 {loop="$tools_plugin"}
692 <div id="plugin_zone_start_picwall" class="plugin_zone">
693 {loop="$plugin_start_zone"}
702 <div id="plugin_zone_end_picwall" class="plugin_zone">
703 {loop="$plugin_end_zone"}
714 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
715 {loop="$plugin_start_zone"}
724 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
725 {loop="$plugin_end_zone"}
736 <div id="plugin_zone_start_picwall" class="plugin_zone">
737 {loop="$plugin_start_zone"}
746 <div class="dailyEntryFooter">
747 {loop="$link.link_plugin"}
756 <div id="plugin_zone_end_picwall" class="plugin_zone">
757 {loop="$plugin_end_zone"}
763 **feed.atom.xml** and **feed.rss.xml**:
765 In headers tags section:
767 {loop="$feed_plugins_header"}
774 {loop="$value.feed_plugins"}