5 * This plugin try to cover Shaarli's plugin API entirely.
6 * Can be used by plugin developper to make their own.
10 * RENDER HEADER, INCLUDES, FOOTER
12 * Those hooks are called at every page rendering.
13 * You can filter its execution by checking _PAGE_ value
14 * and check user status with _LOGGEDIN_.
17 use Shaarli\Config\ConfigManager
;
20 * In the footer hook, there is a working example of a translation extension for Shaarli.
22 * The extension must be attached to a new translation domain (i.e. NOT 'shaarli').
23 * Use case: any custom theme or non official plugin can use the translation system.
25 * See the documentation for more information.
27 const EXT_TRANSLATION_DOMAIN
= 'demo';
30 * This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
32 function demo_plugin_t($text, $nText = '', $nb = 1)
34 return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN
);
38 * Initialization function.
39 * It will be called when the plugin is loaded.
40 * This function can be used to return a list of initialization errors.
42 * @param $conf ConfigManager instance.
44 * @return array List of errors (optional).
46 function demo_plugin_init($conf)
48 $conf->get('toto', 'nope');
50 if (! $conf->exists('translation.extensions.demo')) {
51 // Custom translation with the domain 'demo'
52 $conf->set('translation.extensions.demo', 'plugins/demo_plugin/languages/');
56 $errors[] = 'This a demo init error.';
62 * Executed on every page redering.
64 * Template placeholders:
68 * @param array $data data passed to plugin
70 * @return array altered $data.
72 function hook_demo_plugin_render_header($data)
74 // Only execute when linklist is rendered.
75 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST) {
77 if ($data['_LOGGEDIN_'] === true) {
80 * A link is an array of its attributes (key="value"),
81 * and a mandatory `html` key, which contains its value.
86 'class' => 'mybutton',
87 'title' => 'hover me',
89 'html' => 'DEMO buttons toolbar',
91 $data['buttons_toolbar'][] = $button;
95 * Add additional input fields in the tools.
96 * A field is an array containing:
98 * 'form-attribute-1' => 'form attribute 1 value',
99 * 'form-attribute-2' => 'form attribute 2 value',
102 * 'input-1-attribute-1 => 'input 1 attribute 1 value',
103 * 'input-1-attribute-2 => 'input 1 attribute 2 value',
106 * 'input-2-attribute-1 => 'input 2 attribute 1 value',
110 * This example renders as:
111 * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
112 * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
113 * <input input-2-attribute-1="input 2 attribute 1 value">
120 'class' => 'addform',
126 'placeholder' => 'demo',
130 $data['fields_toolbar'][] = $form;
132 // Another button always displayed
139 $data['buttons_toolbar'][] = $button;
145 * Hook render_includes.
146 * Executed on every page redering.
148 * Template placeholders:
152 * - _PAGE_: current page
153 * - _LOGGEDIN_: true/false
155 * @param array $data data passed to plugin
157 * @return array altered $data.
159 function hook_demo_plugin_render_includes($data)
161 // List of plugin's CSS files.
162 // Note that you just need to specify CSS path.
163 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
169 * Hook render_footer.
170 * Executed on every page redering.
172 * Template placeholders:
178 * - _PAGE_: current page
179 * - _LOGGEDIN_: true/false
181 * @param array $data data passed to plugin
183 * @return array altered $data.
185 function hook_demo_plugin_render_footer($data)
188 $data['text'][] = '<br>'. demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
190 // Free elements at the end of the page.
191 $data['endofpage'][] = '<marquee id="demo_marquee">' .
192 'DEMO: it\'s 1999 all over again!' .
195 // List of plugin's JS files.
196 // Note that you just need to specify CSS path.
197 $data['js_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
207 * Hook render_linklist.
209 * Template placeholders:
210 * - action_plugin: next to 'private only' button.
211 * - plugin_start_zone: page start
212 * - plugin_end_zone: page end
213 * - link_plugin: icons below each links.
216 * - _LOGGEDIN_: true/false
218 * @param array $data data passed to plugin
220 * @return array altered $data.
222 function hook_demo_plugin_render_linklist($data)
225 * Action links (action_plugin):
226 * A link is an array of its attributes (key="value"),
227 * and a mandatory `html` key, which contains its value.
228 * It's also recommended to add key 'on' or 'off' for theme rendering.
233 'title' => 'Uppercase!',
238 if (isset($_GET['up'])) {
239 // Manipulate link data
240 foreach ($data['links'] as &$value) {
241 $value['description'] = strtoupper($value['description']);
242 $value['title'] = strtoupper($value['title']);
244 $action['on'] = true;
246 $action['off'] = true;
248 $data['action_plugin'][] = $action;
250 // link_plugin (for each link)
251 foreach ($data['links'] as &$value) {
252 $value['link_plugin'][] = ' DEMO \o/';
256 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
258 $data['plugin_end_zone'][] = '<center>AFTER</center>';
264 * Hook render_editlink.
266 * Template placeholders:
267 * - field_plugin: add link fields after tags.
269 * @param array $data data passed to plugin
271 * @return array altered $data.
273 function hook_demo_plugin_render_editlink($data)
275 // Load HTML into a string
276 $html = file_get_contents(PluginManager
::$PLUGINS_PATH .'/demo_plugin/field.html');
278 // replace value in HTML if it exists in $data
279 if (!empty($data['link']['stuff'])) {
280 $html = sprintf($html, $data['link']['stuff']);
282 $html = sprintf($html, '');
286 $data['edit_link_plugin'][] = $html;
294 * Template placeholders:
295 * - tools_plugin: after other tools.
297 * @param array $data data passed to plugin
299 * @return array altered $data.
301 function hook_demo_plugin_render_tools($data)
304 $data['tools_plugin'][] = 'tools_plugin';
310 * Hook render_picwall.
312 * Template placeholders:
313 * - plugin_start_zone: page start.
314 * - plugin_end_zone: page end.
317 * - _LOGGEDIN_: true/false
319 * @param array $data data passed to plugin
321 * @return array altered $data.
323 function hook_demo_plugin_render_picwall($data)
326 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
328 $data['plugin_end_zone'][] = '<center>AFTER</center>';
334 * Hook render_tagcloud.
336 * Template placeholders:
337 * - plugin_start_zone: page start.
338 * - plugin_end_zone: page end.
341 * - _LOGGEDIN_: true/false
343 * @param array $data data passed to plugin
345 * @return array altered $data.
347 function hook_demo_plugin_render_tagcloud($data)
350 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
352 $data['plugin_end_zone'][] = '<center>AFTER</center>';
360 * Template placeholders:
361 * - plugin_start_zone: page start.
362 * - plugin_end_zone: page end.
365 * - _LOGGEDIN_: true/false
367 * @param array $data data passed to plugin
369 * @return array altered $data.
371 function hook_demo_plugin_render_daily($data)
374 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
376 $data['plugin_end_zone'][] = '<center>AFTER</center>';
379 // Manipulate columns data
380 foreach ($data['linksToDisplay'] as &$value) {
381 $value['formatedDescription'] .= ' ಠ_ಠ';
384 // Add plugin content at the end of each link
385 foreach ($data['linksToDisplay'] as &$value) {
386 $value['link_plugin'][] = 'DEMO';
399 * Triggered when a link is save (new or edit).
400 * All new links now contain a 'stuff' value.
402 * @param array $data contains the new link data.
404 * @return array altered $data.
406 function hook_demo_plugin_save_link($data)
409 // Save stuff added in editlink field
410 if (!empty($_POST['lf_stuff'])) {
411 $data['stuff'] = escape($_POST['lf_stuff']);
420 * Triggered when a link is deleted.
422 * @param array $data contains the link to be deleted.
424 * @return array altered data.
426 function hook_demo_plugin_delete_link($data)
428 if (strpos($data['url'], 'youtube.com') !== false) {
429 exit('You can not delete a YouTube link. Don\'t ask.');
434 * Execute render_feed hook.
435 * Called with ATOM and RSS feed.
438 * - _PAGE_: current page
439 * - _LOGGEDIN_: true/false
441 * @param array $data data passed to plugin
443 * @return array altered $data.
445 function hook_demo_plugin_render_feed($data)
447 foreach ($data['links'] as &$link) {
448 if ($data['_PAGE_'] == Router
::$PAGE_FEED_ATOM) {
449 $link['description'] .= ' - ATOM Feed' ;
450 } elseif ($data['_PAGE_'] == Router
::$PAGE_FEED_RSS) {
451 $link['description'] .= ' - RSS Feed';
458 * This function is never called, but contains translation calls for GNU gettext extraction.
460 function demo_dummy_translation()
463 t('A demo plugin covering all use cases for template designers and plugin developers.');