]>
Commit | Line | Data |
---|---|---|
786ddad9 A |
1 | <?php |
2 | /** | |
3 | * Demo Plugin. | |
4 | * | |
5 | * This plugin try to cover Shaarli's plugin API entirely. | |
6 | * Can be used by plugin developper to make their own. | |
7 | */ | |
8 | ||
9 | /* | |
10 | * RENDER HEADER, INCLUDES, FOOTER | |
11 | * | |
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_. | |
15 | */ | |
16 | ||
6a65bc57 | 17 | use Shaarli\Config\ConfigManager; |
e1850388 | 18 | use Shaarli\Plugin\PluginManager; |
a932f486 | 19 | use Shaarli\Router; |
6a65bc57 A |
20 | |
21 | /** | |
22 | * In the footer hook, there is a working example of a translation extension for Shaarli. | |
23 | * | |
24 | * The extension must be attached to a new translation domain (i.e. NOT 'shaarli'). | |
25 | * Use case: any custom theme or non official plugin can use the translation system. | |
26 | * | |
27 | * See the documentation for more information. | |
28 | */ | |
29 | const EXT_TRANSLATION_DOMAIN = 'demo'; | |
30 | ||
31 | /* | |
32 | * This is not necessary, but it's easier if you don't want Poedit to mix up your translations. | |
33 | */ | |
34 | function demo_plugin_t($text, $nText = '', $nb = 1) | |
35 | { | |
36 | return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN); | |
37 | } | |
38 | ||
7fde6de1 A |
39 | /** |
40 | * Initialization function. | |
41 | * It will be called when the plugin is loaded. | |
42 | * This function can be used to return a list of initialization errors. | |
43 | * | |
44 | * @param $conf ConfigManager instance. | |
45 | * | |
46 | * @return array List of errors (optional). | |
47 | */ | |
48 | function demo_plugin_init($conf) | |
49 | { | |
50 | $conf->get('toto', 'nope'); | |
51 | ||
6a65bc57 A |
52 | if (! $conf->exists('translation.extensions.demo')) { |
53 | // Custom translation with the domain 'demo' | |
54 | $conf->set('translation.extensions.demo', 'plugins/demo_plugin/languages/'); | |
55 | $conf->write(true); | |
56 | } | |
57 | ||
7fde6de1 A |
58 | $errors[] = 'This a demo init error.'; |
59 | return $errors; | |
60 | } | |
61 | ||
786ddad9 A |
62 | /** |
63 | * Hook render_header. | |
64 | * Executed on every page redering. | |
65 | * | |
66 | * Template placeholders: | |
67 | * - buttons_toolbar | |
68 | * - fields_toolbar | |
69 | * | |
70 | * @param array $data data passed to plugin | |
71 | * | |
72 | * @return array altered $data. | |
73 | */ | |
74 | function hook_demo_plugin_render_header($data) | |
75 | { | |
76 | // Only execute when linklist is rendered. | |
77 | if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) { | |
786ddad9 A |
78 | // If loggedin |
79 | if ($data['_LOGGEDIN_'] === true) { | |
ba0fd807 A |
80 | /* |
81 | * Links in toolbar: | |
82 | * A link is an array of its attributes (key="value"), | |
83 | * and a mandatory `html` key, which contains its value. | |
84 | */ | |
85 | $button = array( | |
86 | 'attr' => array ( | |
87 | 'href' => '#', | |
88 | 'class' => 'mybutton', | |
89 | 'title' => 'hover me', | |
90 | ), | |
91 | 'html' => 'DEMO buttons toolbar', | |
92 | ); | |
93 | $data['buttons_toolbar'][] = $button; | |
786ddad9 A |
94 | } |
95 | ||
ba0fd807 A |
96 | /* |
97 | * Add additional input fields in the tools. | |
98 | * A field is an array containing: | |
99 | * [ | |
100 | * 'form-attribute-1' => 'form attribute 1 value', | |
101 | * 'form-attribute-2' => 'form attribute 2 value', | |
102 | * 'inputs' => [ | |
103 | * [ | |
104 | * 'input-1-attribute-1 => 'input 1 attribute 1 value', | |
105 | * 'input-1-attribute-2 => 'input 1 attribute 2 value', | |
106 | * ], | |
107 | * [ | |
108 | * 'input-2-attribute-1 => 'input 2 attribute 1 value', | |
109 | * ], | |
110 | * ], | |
111 | * ] | |
112 | * This example renders as: | |
9d9f6d75 V |
113 | * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value"> |
114 | * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value"> | |
115 | * <input input-2-attribute-1="input 2 attribute 1 value"> | |
116 | * </form> | |
ba0fd807 A |
117 | */ |
118 | $form = array( | |
119 | 'attr' => array( | |
120 | 'method' => 'GET', | |
121 | 'action' => '?', | |
122 | 'class' => 'addform', | |
123 | ), | |
124 | 'inputs' => array( | |
125 | array( | |
126 | 'type' => 'text', | |
127 | 'name' => 'demo', | |
128 | 'placeholder' => 'demo', | |
129 | ) | |
130 | ) | |
131 | ); | |
132 | $data['fields_toolbar'][] = $form; | |
786ddad9 | 133 | } |
38603b24 | 134 | // Another button always displayed |
ba0fd807 A |
135 | $button = array( |
136 | 'attr' => array( | |
137 | 'href' => '#', | |
138 | ), | |
139 | 'html' => 'Demo', | |
140 | ); | |
141 | $data['buttons_toolbar'][] = $button; | |
786ddad9 A |
142 | |
143 | return $data; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Hook render_includes. | |
148 | * Executed on every page redering. | |
149 | * | |
150 | * Template placeholders: | |
151 | * - css_files | |
152 | * | |
153 | * Data: | |
154 | * - _PAGE_: current page | |
155 | * - _LOGGEDIN_: true/false | |
156 | * | |
157 | * @param array $data data passed to plugin | |
158 | * | |
159 | * @return array altered $data. | |
160 | */ | |
161 | function hook_demo_plugin_render_includes($data) | |
162 | { | |
163 | // List of plugin's CSS files. | |
164 | // Note that you just need to specify CSS path. | |
165 | $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css'; | |
166 | ||
167 | return $data; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Hook render_footer. | |
172 | * Executed on every page redering. | |
173 | * | |
174 | * Template placeholders: | |
175 | * - text | |
40a5f296 | 176 | * - endofpage |
786ddad9 A |
177 | * - js_files |
178 | * | |
179 | * Data: | |
180 | * - _PAGE_: current page | |
181 | * - _LOGGEDIN_: true/false | |
182 | * | |
183 | * @param array $data data passed to plugin | |
184 | * | |
185 | * @return array altered $data. | |
186 | */ | |
187 | function hook_demo_plugin_render_footer($data) | |
188 | { | |
189 | // footer text | |
6a65bc57 | 190 | $data['text'][] = '<br>'. demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.'); |
786ddad9 | 191 | |
40a5f296 A |
192 | // Free elements at the end of the page. |
193 | $data['endofpage'][] = '<marquee id="demo_marquee">' . | |
194 | 'DEMO: it\'s 1999 all over again!' . | |
195 | '</marquee>'; | |
196 | ||
786ddad9 A |
197 | // List of plugin's JS files. |
198 | // Note that you just need to specify CSS path. | |
199 | $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js'; | |
200 | ||
201 | return $data; | |
202 | } | |
203 | ||
204 | /* | |
205 | * SPECIFIC PAGES | |
206 | */ | |
207 | ||
208 | /** | |
209 | * Hook render_linklist. | |
210 | * | |
211 | * Template placeholders: | |
212 | * - action_plugin: next to 'private only' button. | |
213 | * - plugin_start_zone: page start | |
214 | * - plugin_end_zone: page end | |
215 | * - link_plugin: icons below each links. | |
216 | * | |
217 | * Data: | |
218 | * - _LOGGEDIN_: true/false | |
219 | * | |
220 | * @param array $data data passed to plugin | |
221 | * | |
222 | * @return array altered $data. | |
223 | */ | |
224 | function hook_demo_plugin_render_linklist($data) | |
225 | { | |
ba0fd807 A |
226 | /* |
227 | * Action links (action_plugin): | |
228 | * A link is an array of its attributes (key="value"), | |
229 | * and a mandatory `html` key, which contains its value. | |
230 | * It's also recommended to add key 'on' or 'off' for theme rendering. | |
231 | */ | |
232 | $action = array( | |
233 | 'attr' => array( | |
234 | 'href' => '?up', | |
235 | 'title' => 'Uppercase!', | |
236 | ), | |
237 | 'html' => '←', | |
238 | ); | |
786ddad9 A |
239 | |
240 | if (isset($_GET['up'])) { | |
241 | // Manipulate link data | |
242 | foreach ($data['links'] as &$value) { | |
243 | $value['description'] = strtoupper($value['description']); | |
244 | $value['title'] = strtoupper($value['title']); | |
245 | } | |
ba0fd807 A |
246 | $action['on'] = true; |
247 | } else { | |
248 | $action['off'] = true; | |
786ddad9 | 249 | } |
ba0fd807 | 250 | $data['action_plugin'][] = $action; |
786ddad9 A |
251 | |
252 | // link_plugin (for each link) | |
253 | foreach ($data['links'] as &$value) { | |
254 | $value['link_plugin'][] = ' DEMO \o/'; | |
255 | } | |
256 | ||
257 | // plugin_start_zone | |
258 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
259 | // plugin_start_zone | |
260 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
261 | ||
262 | return $data; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Hook render_editlink. | |
267 | * | |
268 | * Template placeholders: | |
269 | * - field_plugin: add link fields after tags. | |
270 | * | |
271 | * @param array $data data passed to plugin | |
272 | * | |
273 | * @return array altered $data. | |
274 | */ | |
275 | function hook_demo_plugin_render_editlink($data) | |
276 | { | |
277 | // Load HTML into a string | |
278 | $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html'); | |
279 | ||
280 | // replace value in HTML if it exists in $data | |
281 | if (!empty($data['link']['stuff'])) { | |
282 | $html = sprintf($html, $data['link']['stuff']); | |
283 | } else { | |
284 | $html = sprintf($html, ''); | |
285 | } | |
286 | ||
287 | // field_plugin | |
288 | $data['edit_link_plugin'][] = $html; | |
289 | ||
290 | return $data; | |
291 | } | |
292 | ||
293 | /** | |
294 | * Hook render_tools. | |
295 | * | |
296 | * Template placeholders: | |
297 | * - tools_plugin: after other tools. | |
298 | * | |
299 | * @param array $data data passed to plugin | |
300 | * | |
301 | * @return array altered $data. | |
302 | */ | |
303 | function hook_demo_plugin_render_tools($data) | |
304 | { | |
305 | // field_plugin | |
306 | $data['tools_plugin'][] = 'tools_plugin'; | |
307 | ||
308 | return $data; | |
309 | } | |
310 | ||
311 | /** | |
312 | * Hook render_picwall. | |
313 | * | |
314 | * Template placeholders: | |
315 | * - plugin_start_zone: page start. | |
316 | * - plugin_end_zone: page end. | |
317 | * | |
318 | * Data: | |
319 | * - _LOGGEDIN_: true/false | |
320 | * | |
321 | * @param array $data data passed to plugin | |
322 | * | |
323 | * @return array altered $data. | |
324 | */ | |
325 | function hook_demo_plugin_render_picwall($data) | |
326 | { | |
327 | // plugin_start_zone | |
328 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
329 | // plugin_end_zone | |
330 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
331 | ||
332 | return $data; | |
333 | } | |
334 | ||
335 | /** | |
336 | * Hook render_tagcloud. | |
337 | * | |
338 | * Template placeholders: | |
339 | * - plugin_start_zone: page start. | |
340 | * - plugin_end_zone: page end. | |
341 | * | |
342 | * Data: | |
343 | * - _LOGGEDIN_: true/false | |
344 | * | |
345 | * @param array $data data passed to plugin | |
346 | * | |
347 | * @return array altered $data. | |
348 | */ | |
349 | function hook_demo_plugin_render_tagcloud($data) | |
350 | { | |
351 | // plugin_start_zone | |
352 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
353 | // plugin_end_zone | |
354 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
355 | ||
356 | return $data; | |
357 | } | |
358 | ||
359 | /** | |
360 | * Hook render_daily. | |
361 | * | |
362 | * Template placeholders: | |
363 | * - plugin_start_zone: page start. | |
364 | * - plugin_end_zone: page end. | |
365 | * | |
366 | * Data: | |
367 | * - _LOGGEDIN_: true/false | |
368 | * | |
369 | * @param array $data data passed to plugin | |
370 | * | |
371 | * @return array altered $data. | |
372 | */ | |
373 | function hook_demo_plugin_render_daily($data) | |
374 | { | |
375 | // plugin_start_zone | |
376 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
377 | // plugin_end_zone | |
378 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
379 | ||
380 | ||
381 | // Manipulate columns data | |
50142efd | 382 | foreach ($data['linksToDisplay'] as &$value) { |
383 | $value['formatedDescription'] .= ' ಠ_ಠ'; | |
786ddad9 A |
384 | } |
385 | ||
386 | // Add plugin content at the end of each link | |
50142efd | 387 | foreach ($data['linksToDisplay'] as &$value) { |
388 | $value['link_plugin'][] = 'DEMO'; | |
786ddad9 A |
389 | } |
390 | ||
391 | return $data; | |
392 | } | |
393 | ||
394 | /* | |
395 | * DATA SAVING HOOK. | |
396 | */ | |
397 | ||
398 | /** | |
399 | * Hook savelink. | |
400 | * | |
401 | * Triggered when a link is save (new or edit). | |
402 | * All new links now contain a 'stuff' value. | |
403 | * | |
404 | * @param array $data contains the new link data. | |
405 | * | |
406 | * @return array altered $data. | |
407 | */ | |
408 | function hook_demo_plugin_save_link($data) | |
409 | { | |
410 | ||
411 | // Save stuff added in editlink field | |
412 | if (!empty($_POST['lf_stuff'])) { | |
413 | $data['stuff'] = escape($_POST['lf_stuff']); | |
414 | } | |
415 | ||
416 | return $data; | |
417 | } | |
418 | ||
419 | /** | |
420 | * Hook delete_link. | |
421 | * | |
422 | * Triggered when a link is deleted. | |
423 | * | |
424 | * @param array $data contains the link to be deleted. | |
425 | * | |
426 | * @return array altered data. | |
427 | */ | |
428 | function hook_demo_plugin_delete_link($data) | |
429 | { | |
430 | if (strpos($data['url'], 'youtube.com') !== false) { | |
431 | exit('You can not delete a YouTube link. Don\'t ask.'); | |
432 | } | |
bcd078bf A |
433 | } |
434 | ||
435 | /** | |
436 | * Execute render_feed hook. | |
437 | * Called with ATOM and RSS feed. | |
438 | * | |
439 | * Special data keys: | |
440 | * - _PAGE_: current page | |
441 | * - _LOGGEDIN_: true/false | |
442 | * | |
443 | * @param array $data data passed to plugin | |
444 | * | |
445 | * @return array altered $data. | |
446 | */ | |
447 | function hook_demo_plugin_render_feed($data) | |
448 | { | |
449 | foreach ($data['links'] as &$link) { | |
450 | if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) { | |
451 | $link['description'] .= ' - ATOM Feed' ; | |
a0ab3c3f | 452 | } elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) { |
bcd078bf A |
453 | $link['description'] .= ' - RSS Feed'; |
454 | } | |
455 | } | |
456 | return $data; | |
457 | } | |
12266213 | 458 | |
15a61e59 A |
459 | /** |
460 | * When plugin parameters are saved. | |
461 | * | |
462 | * @param array $data $_POST array | |
463 | * | |
464 | * @return array Updated $_POST array | |
465 | */ | |
466 | function hook_demo_plugin_save_plugin_parameters($data) | |
467 | { | |
468 | // Here we edit the provided value, but we can use this to generate config files, etc. | |
469 | if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) { | |
470 | $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX'; | |
471 | } | |
472 | ||
473 | return $data; | |
474 | } | |
475 | ||
12266213 A |
476 | /** |
477 | * This function is never called, but contains translation calls for GNU gettext extraction. | |
478 | */ | |
479 | function demo_dummy_translation() | |
480 | { | |
481 | // meta | |
482 | t('A demo plugin covering all use cases for template designers and plugin developers.'); | |
15a61e59 A |
483 | t('This is a parameter dedicated to the demo plugin. It\'ll be suffixed.'); |
484 | t('Other demo parameter'); | |
12266213 | 485 | } |