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) {
78 if ($data['_LOGGEDIN_'] === true) {
81 * A link is an array of its attributes (key="value"),
82 * and a mandatory `html` key, which contains its value.
87 'class' => 'mybutton',
88 'title' => 'hover me',
90 'html' => 'DEMO buttons toolbar',
92 $data['buttons_toolbar'][] = $button;
96 * Add additional input fields in the tools.
97 * A field is an array containing:
99 * 'form-attribute-1' => 'form attribute 1 value',
100 * 'form-attribute-2' => 'form attribute 2 value',
103 * 'input-1-attribute-1 => 'input 1 attribute 1 value',
104 * 'input-1-attribute-2 => 'input 1 attribute 2 value',
107 * 'input-2-attribute-1 => 'input 2 attribute 1 value',
111 * This example renders as:
112 * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
113 * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
114 * <input input-2-attribute-1="input 2 attribute 1 value">
121 'class' => 'addform',
127 'placeholder' => 'demo',
131 $data['fields_toolbar'][] = $form;
133 // Another button always displayed
140 $data['buttons_toolbar'][] = $button;
146 * Hook render_includes.
147 * Executed on every page redering.
149 * Template placeholders:
153 * - _PAGE_: current page
154 * - _LOGGEDIN_: true/false
156 * @param array $data data passed to plugin
158 * @return array altered $data.
160 function hook_demo_plugin_render_includes($data)
162 // List of plugin's CSS files.
163 // Note that you just need to specify CSS path.
164 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
170 * Hook render_footer.
171 * Executed on every page redering.
173 * Template placeholders:
179 * - _PAGE_: current page
180 * - _LOGGEDIN_: true/false
182 * @param array $data data passed to plugin
184 * @return array altered $data.
186 function hook_demo_plugin_render_footer($data)
189 $data['text'][] = '<br>'. demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
191 // Free elements at the end of the page.
192 $data['endofpage'][] = '<marquee id="demo_marquee">' .
193 'DEMO: it\'s 1999 all over again!' .
196 // List of plugin's JS files.
197 // Note that you just need to specify CSS path.
198 $data['js_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
208 * Hook render_linklist.
210 * Template placeholders:
211 * - action_plugin: next to 'private only' button.
212 * - plugin_start_zone: page start
213 * - plugin_end_zone: page end
214 * - link_plugin: icons below each links.
217 * - _LOGGEDIN_: true/false
219 * @param array $data data passed to plugin
221 * @return array altered $data.
223 function hook_demo_plugin_render_linklist($data)
226 * Action links (action_plugin):
227 * A link is an array of its attributes (key="value"),
228 * and a mandatory `html` key, which contains its value.
229 * It's also recommended to add key 'on' or 'off' for theme rendering.
234 'title' => 'Uppercase!',
239 if (isset($_GET['up'])) {
240 // Manipulate link data
241 foreach ($data['links'] as &$value) {
242 $value['description'] = strtoupper($value['description']);
243 $value['title'] = strtoupper($value['title']);
245 $action['on'] = true;
247 $action['off'] = true;
249 $data['action_plugin'][] = $action;
251 // link_plugin (for each link)
252 foreach ($data['links'] as &$value) {
253 $value['link_plugin'][] = ' DEMO \o/';
257 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
259 $data['plugin_end_zone'][] = '<center>AFTER</center>';
265 * Hook render_editlink.
267 * Template placeholders:
268 * - field_plugin: add link fields after tags.
270 * @param array $data data passed to plugin
272 * @return array altered $data.
274 function hook_demo_plugin_render_editlink($data)
276 // Load HTML into a string
277 $html = file_get_contents(PluginManager
::$PLUGINS_PATH .'/demo_plugin/field.html');
279 // replace value in HTML if it exists in $data
280 if (!empty($data['link']['stuff'])) {
281 $html = sprintf($html, $data['link']['stuff']);
283 $html = sprintf($html, '');
287 $data['edit_link_plugin'][] = $html;
295 * Template placeholders:
296 * - tools_plugin: after other tools.
298 * @param array $data data passed to plugin
300 * @return array altered $data.
302 function hook_demo_plugin_render_tools($data)
305 $data['tools_plugin'][] = 'tools_plugin';
311 * Hook render_picwall.
313 * Template placeholders:
314 * - plugin_start_zone: page start.
315 * - plugin_end_zone: page end.
318 * - _LOGGEDIN_: true/false
320 * @param array $data data passed to plugin
322 * @return array altered $data.
324 function hook_demo_plugin_render_picwall($data)
327 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
329 $data['plugin_end_zone'][] = '<center>AFTER</center>';
335 * Hook render_tagcloud.
337 * Template placeholders:
338 * - plugin_start_zone: page start.
339 * - plugin_end_zone: page end.
342 * - _LOGGEDIN_: true/false
344 * @param array $data data passed to plugin
346 * @return array altered $data.
348 function hook_demo_plugin_render_tagcloud($data)
351 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
353 $data['plugin_end_zone'][] = '<center>AFTER</center>';
361 * Template placeholders:
362 * - plugin_start_zone: page start.
363 * - plugin_end_zone: page end.
366 * - _LOGGEDIN_: true/false
368 * @param array $data data passed to plugin
370 * @return array altered $data.
372 function hook_demo_plugin_render_daily($data)
375 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
377 $data['plugin_end_zone'][] = '<center>AFTER</center>';
380 // Manipulate columns data
381 foreach ($data['cols'] as &$value) {
382 foreach ($value as &$value2) {
383 $value2['formatedDescription'] .= ' ಠ_ಠ';
387 // Add plugin content at the end of each link
388 foreach ($data['cols'] as &$value) {
389 foreach ($value as &$value2) {
390 $value2['link_plugin'][] = 'DEMO';
404 * Triggered when a link is save (new or edit).
405 * All new links now contain a 'stuff' value.
407 * @param array $data contains the new link data.
409 * @return array altered $data.
411 function hook_demo_plugin_save_link($data)
414 // Save stuff added in editlink field
415 if (!empty($_POST['lf_stuff'])) {
416 $data['stuff'] = escape($_POST['lf_stuff']);
425 * Triggered when a link is deleted.
427 * @param array $data contains the link to be deleted.
429 * @return array altered data.
431 function hook_demo_plugin_delete_link($data)
433 if (strpos($data['url'], 'youtube.com') !== false) {
434 exit('You can not delete a YouTube link. Don\'t ask.');
439 * Execute render_feed hook.
440 * Called with ATOM and RSS feed.
443 * - _PAGE_: current page
444 * - _LOGGEDIN_: true/false
446 * @param array $data data passed to plugin
448 * @return array altered $data.
450 function hook_demo_plugin_render_feed($data)
452 foreach ($data['links'] as &$link) {
453 if ($data['_PAGE_'] == Router
::$PAGE_FEED_ATOM) {
454 $link['description'] .= ' - ATOM Feed' ;
456 elseif ($data['_PAGE_'] == Router
::$PAGE_FEED_RSS) {
457 $link['description'] .= ' - RSS Feed';
464 * This function is never called, but contains translation calls for GNU gettext extraction.
466 function demo_dummy_translation()
469 t('A demo plugin covering all use cases for template designers and plugin developers.');