6 * This plugin tries to completely cover Shaarli's plugin API.
7 * Can be used by plugin developers to make their own plugin.
10 require_once __DIR__
. '/DemoPluginController.php';
13 * RENDER HEADER, INCLUDES, FOOTER
15 * Those hooks are called at every page rendering.
16 * You can filter its execution by checking _PAGE_ value
17 * and check user status with _LOGGEDIN_.
20 use Shaarli\Bookmark\Bookmark
;
21 use Shaarli\Config\ConfigManager
;
22 use Shaarli\Plugin\PluginManager
;
23 use Shaarli\Render\TemplatePage
;
26 * In the footer hook, there is a working example of a translation extension for Shaarli.
28 * The extension must be attached to a new translation domain (i.e. NOT 'shaarli').
29 * Use case: any custom theme or non official plugin can use the translation system.
31 * See the documentation for more information.
33 const EXT_TRANSLATION_DOMAIN
= 'demo';
36 * This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
38 function demo_plugin_t($text, $nText = '', $nb = 1)
40 return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN
);
44 * Initialization function.
45 * It will be called when the plugin is loaded.
46 * This function can be used to return a list of initialization errors.
48 * @param $conf ConfigManager instance.
50 * @return array List of errors (optional).
52 function demo_plugin_init($conf)
54 $conf->get('toto', 'nope');
56 if (! $conf->exists('translation.extensions.demo')) {
57 // Custom translation with the domain 'demo'
58 $conf->set('translation.extensions.demo', 'plugins/demo_plugin/languages/');
62 $errors[] = 'This a demo init error.';
66 function demo_plugin_register_routes(): array
72 'callable' => 'Shaarli\DemoPlugin\DemoPluginController:index',
79 * Executed on every page render.
81 * Template placeholders:
85 * @param array $data data passed to plugin
87 * @return array altered $data.
89 function hook_demo_plugin_render_header($data)
91 // Only execute when linklist is rendered.
92 if ($data['_PAGE_'] == TemplatePage
::LINKLIST
) {
94 if ($data['_LOGGEDIN_'] === true) {
97 * A link is an array of its attributes (key="value"),
98 * and a mandatory `html` key, which contains its value.
103 'class' => 'mybutton',
104 'title' => 'hover me',
106 'html' => 'DEMO buttons toolbar',
108 $data['buttons_toolbar'][] = $button;
112 * Add additional input fields in the tools.
113 * A field is an array containing:
115 * 'form-attribute-1' => 'form attribute 1 value',
116 * 'form-attribute-2' => 'form attribute 2 value',
119 * 'input-1-attribute-1 => 'input 1 attribute 1 value',
120 * 'input-1-attribute-2 => 'input 1 attribute 2 value',
123 * 'input-2-attribute-1 => 'input 2 attribute 1 value',
127 * This example renders as:
128 * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
129 * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
130 * <input input-2-attribute-1="input 2 attribute 1 value">
136 'action' => $data['_BASE_PATH_'] . '/',
137 'class' => 'addform',
143 'placeholder' => 'demo',
147 $data['fields_toolbar'][] = $form;
149 // Another button always displayed
156 $data['buttons_toolbar'][] = $button;
162 * Hook render_includes.
163 * Executed on every page render.
165 * Template placeholders:
169 * - _PAGE_: current page
170 * - _LOGGEDIN_: true/false
172 * @param array $data data passed to plugin
174 * @return array altered $data.
176 function hook_demo_plugin_render_includes($data)
178 // List of plugin's CSS files.
179 // Note that you just need to specify CSS path.
180 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
186 * Hook render_footer.
187 * Executed on every page render.
189 * Template placeholders:
195 * - _PAGE_: current page
196 * - _LOGGEDIN_: true/false
198 * @param array $data data passed to plugin
200 * @return array altered $data.
202 function hook_demo_plugin_render_footer($data)
205 $data['text'][] = '<br>' . demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
207 // Free elements at the end of the page.
208 $data['endofpage'][] = '<marquee id="demo_marquee">' .
209 'DEMO: it\'s 1999 all over again!' .
212 // List of plugin's JS files.
213 // Note that you just need to specify CSS path.
214 $data['js_files'][] = PluginManager
::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
224 * Hook render_linklist.
226 * Template placeholders:
227 * - action_plugin: next to 'private only' button.
228 * - plugin_start_zone: page start
229 * - plugin_end_zone: page end
230 * - link_plugin: icons below each links.
233 * - _LOGGEDIN_: true/false
235 * @param array $data data passed to plugin
237 * @return array altered $data.
239 function hook_demo_plugin_render_linklist($data)
242 * Action links (action_plugin):
243 * A link is an array of its attributes (key="value"),
244 * and a mandatory `html` key, which contains its value.
245 * It's also recommended to add key 'on' or 'off' for theme rendering.
250 'title' => 'Uppercase!',
255 if (isset($_GET['up'])) {
256 // Manipulate link data
257 foreach ($data['links'] as &$value) {
258 $value['description'] = strtoupper($value['description']);
259 $value['title'] = strtoupper($value['title']);
261 $action['on'] = true;
263 $action['off'] = true;
265 $data['action_plugin'][] = $action;
267 // Action to trigger custom filter hiding bookmarks not containing 'e' letter in their description
271 'title' => 'Hide bookmarks without "e" in their description.',
274 'on' => isset($_GET['e'])
276 $data['action_plugin'][] = $action;
278 // link_plugin (for each link)
279 foreach ($data['links'] as &$value) {
280 $value['link_plugin'][] = ' DEMO \o/';
284 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
286 $data['plugin_end_zone'][] = '<center>AFTER</center>';
292 * Hook render_editlink.
294 * Template placeholders:
295 * - field_plugin: add link fields after tags.
297 * @param array $data data passed to plugin
299 * @return array altered $data.
301 function hook_demo_plugin_render_editlink($data)
303 // Load HTML into a string
304 $html = file_get_contents(PluginManager
::$PLUGINS_PATH . '/demo_plugin/field.html');
306 // Replace value in HTML if it exists in $data
307 if (!empty($data['link']['stuff'])) {
308 $html = sprintf($html, $data['link']['stuff']);
310 $html = sprintf($html, '');
314 $data['edit_link_plugin'][] = $html;
322 * Template placeholders:
323 * - tools_plugin: after other tools.
325 * @param array $data data passed to plugin
327 * @return array altered $data.
329 function hook_demo_plugin_render_tools($data)
332 $data['tools_plugin'][] = '<div class="tools-item">
333 <a href="' . $data['_BASE_PATH_'] . '/plugin/demo_plugin/custom">
334 <span class="pure-button pure-u-lg-2-3 pure-u-3-4">Demo Plugin Custom Route</span>
342 * Hook render_picwall.
344 * Template placeholders:
345 * - plugin_start_zone: page start.
346 * - plugin_end_zone: page end.
349 * - _LOGGEDIN_: true/false
351 * @param array $data data passed to plugin
353 * @return array altered $data.
355 function hook_demo_plugin_render_picwall($data)
357 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
358 $data['plugin_end_zone'][] = '<center>AFTER</center>';
364 * Hook render_tagcloud.
366 * Template placeholders:
367 * - plugin_start_zone: page start.
368 * - plugin_end_zone: page end.
371 * - _LOGGEDIN_: true/false
373 * @param array $data data passed to plugin
375 * @return array altered $data.
377 function hook_demo_plugin_render_tagcloud($data)
379 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
380 $data['plugin_end_zone'][] = '<center>AFTER</center>';
388 * Template placeholders:
389 * - plugin_start_zone: page start.
390 * - plugin_end_zone: page end.
393 * - _LOGGEDIN_: true/false
395 * @param array $data data passed to plugin
397 * @return array altered $data.
399 function hook_demo_plugin_render_daily($data)
401 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
402 $data['plugin_end_zone'][] = '<center>AFTER</center>';
405 // Manipulate columns data
406 foreach ($data['linksToDisplay'] as &$value) {
407 $value['formatedDescription'] .= ' ಠ_ಠ';
410 // Add plugin content at the end of each link
411 foreach ($data['linksToDisplay'] as &$value) {
412 $value['link_plugin'][] = 'DEMO';
425 * Triggered when a link is save (new or edit).
426 * All new links now contain a 'stuff' value.
428 * @param array $data contains the new link data.
430 * @return array altered $data.
432 function hook_demo_plugin_save_link($data)
435 // Save stuff added in editlink field
436 if (!empty($_POST['lf_stuff'])) {
437 $data['stuff'] = escape($_POST['lf_stuff']);
446 * Triggered when a link is deleted.
448 * @param array $data contains the link to be deleted.
450 * @return array altered data.
452 function hook_demo_plugin_delete_link($data)
454 if (strpos($data['url'], 'youtube.com') !== false) {
455 exit('You can not delete a YouTube link. Don\'t ask.');
460 * Execute render_feed hook.
461 * Called with ATOM and RSS feed.
464 * - _PAGE_: current page
465 * - _LOGGEDIN_: true/false
467 * @param array $data data passed to plugin
469 * @return array altered $data.
471 function hook_demo_plugin_render_feed($data)
473 foreach ($data['links'] as &$link) {
474 if ($data['_PAGE_'] == TemplatePage
::FEED_ATOM
) {
475 $link['description'] .= ' - ATOM Feed' ;
476 } elseif ($data['_PAGE_'] == TemplatePage
::FEED_RSS
) {
477 $link['description'] .= ' - RSS Feed';
484 * When plugin parameters are saved.
486 * @param array $data $_POST array
488 * @return array Updated $_POST array
490 function hook_demo_plugin_save_plugin_parameters($data)
492 // Here we edit the provided value.
493 // This hook can also be used to generate config files, etc.
494 if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) {
495 $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX';
502 * This hook is called when a search is performed, on every search entry.
503 * It allows to add custom filters, and filter out additional link.
505 * For exemple here, we hide all bookmarks not containing the letter 'e' in their description.
507 * @param Bookmark $bookmark Search entry. Note that this is a Bookmark object, and not a link array.
508 * It should NOT be altered.
509 * @param array $context Additional info on the search performed.
511 * @return bool True if the bookmark should be kept in the search result, false to discard it.
513 function hook_demo_plugin_filter_search_entry(Bookmark
$bookmark, array $context): bool
515 if (isset($_GET['e'])) {
516 return strpos($bookmark->getDescription(), 'e') !== false;
523 * This function is never called, but contains translation calls for GNU gettext extraction.
525 function demo_dummy_translation()
528 t('A demo plugin covering all use cases for template designers and plugin developers.');
529 t('This is a parameter dedicated to the demo plugin. It\'ll be suffixed.');
530 t('Other demo parameter');