]>
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 | ||
7fde6de1 A |
17 | /** |
18 | * Initialization function. | |
19 | * It will be called when the plugin is loaded. | |
20 | * This function can be used to return a list of initialization errors. | |
21 | * | |
22 | * @param $conf ConfigManager instance. | |
23 | * | |
24 | * @return array List of errors (optional). | |
25 | */ | |
26 | function demo_plugin_init($conf) | |
27 | { | |
28 | $conf->get('toto', 'nope'); | |
29 | ||
30 | $errors[] = 'This a demo init error.'; | |
31 | return $errors; | |
32 | } | |
33 | ||
786ddad9 A |
34 | /** |
35 | * Hook render_header. | |
36 | * Executed on every page redering. | |
37 | * | |
38 | * Template placeholders: | |
39 | * - buttons_toolbar | |
40 | * - fields_toolbar | |
41 | * | |
42 | * @param array $data data passed to plugin | |
43 | * | |
44 | * @return array altered $data. | |
45 | */ | |
46 | function hook_demo_plugin_render_header($data) | |
47 | { | |
48 | // Only execute when linklist is rendered. | |
49 | if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) { | |
50 | ||
51 | // If loggedin | |
52 | if ($data['_LOGGEDIN_'] === true) { | |
53 | // Buttons in toolbar | |
54 | $data['buttons_toolbar'][] = '<li><a href="#">DEMO_buttons_toolbar</a></li>'; | |
55 | } | |
56 | ||
57 | // Fields in toolbar | |
58 | $data['fields_toolbar'][] = 'DEMO_fields_toolbar'; | |
59 | } | |
38603b24 A |
60 | // Another button always displayed |
61 | $data['buttons_toolbar'][] = '<li><a href="#">DEMO</a></li>'; | |
786ddad9 A |
62 | |
63 | return $data; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Hook render_includes. | |
68 | * Executed on every page redering. | |
69 | * | |
70 | * Template placeholders: | |
71 | * - css_files | |
72 | * | |
73 | * Data: | |
74 | * - _PAGE_: current page | |
75 | * - _LOGGEDIN_: true/false | |
76 | * | |
77 | * @param array $data data passed to plugin | |
78 | * | |
79 | * @return array altered $data. | |
80 | */ | |
81 | function hook_demo_plugin_render_includes($data) | |
82 | { | |
83 | // List of plugin's CSS files. | |
84 | // Note that you just need to specify CSS path. | |
85 | $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css'; | |
86 | ||
87 | return $data; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Hook render_footer. | |
92 | * Executed on every page redering. | |
93 | * | |
94 | * Template placeholders: | |
95 | * - text | |
40a5f296 | 96 | * - endofpage |
786ddad9 A |
97 | * - js_files |
98 | * | |
99 | * Data: | |
100 | * - _PAGE_: current page | |
101 | * - _LOGGEDIN_: true/false | |
102 | * | |
103 | * @param array $data data passed to plugin | |
104 | * | |
105 | * @return array altered $data. | |
106 | */ | |
107 | function hook_demo_plugin_render_footer($data) | |
108 | { | |
109 | // footer text | |
110 | $data['text'][] = 'Shaarli is now enhanced by the awesome demo_plugin.'; | |
111 | ||
40a5f296 A |
112 | // Free elements at the end of the page. |
113 | $data['endofpage'][] = '<marquee id="demo_marquee">' . | |
114 | 'DEMO: it\'s 1999 all over again!' . | |
115 | '</marquee>'; | |
116 | ||
786ddad9 A |
117 | // List of plugin's JS files. |
118 | // Note that you just need to specify CSS path. | |
119 | $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js'; | |
120 | ||
121 | return $data; | |
122 | } | |
123 | ||
124 | /* | |
125 | * SPECIFIC PAGES | |
126 | */ | |
127 | ||
128 | /** | |
129 | * Hook render_linklist. | |
130 | * | |
131 | * Template placeholders: | |
132 | * - action_plugin: next to 'private only' button. | |
133 | * - plugin_start_zone: page start | |
134 | * - plugin_end_zone: page end | |
135 | * - link_plugin: icons below each links. | |
136 | * | |
137 | * Data: | |
138 | * - _LOGGEDIN_: true/false | |
139 | * | |
140 | * @param array $data data passed to plugin | |
141 | * | |
142 | * @return array altered $data. | |
143 | */ | |
144 | function hook_demo_plugin_render_linklist($data) | |
145 | { | |
146 | // action_plugin | |
147 | $data['action_plugin'][] = '<div class="upper_plugin_demo"><a href="?up" title="Uppercase!">←</a></div>'; | |
148 | ||
149 | if (isset($_GET['up'])) { | |
150 | // Manipulate link data | |
151 | foreach ($data['links'] as &$value) { | |
152 | $value['description'] = strtoupper($value['description']); | |
153 | $value['title'] = strtoupper($value['title']); | |
154 | } | |
155 | } | |
156 | ||
157 | // link_plugin (for each link) | |
158 | foreach ($data['links'] as &$value) { | |
159 | $value['link_plugin'][] = ' DEMO \o/'; | |
160 | } | |
161 | ||
162 | // plugin_start_zone | |
163 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
164 | // plugin_start_zone | |
165 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
166 | ||
167 | return $data; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Hook render_editlink. | |
172 | * | |
173 | * Template placeholders: | |
174 | * - field_plugin: add link fields after tags. | |
175 | * | |
176 | * @param array $data data passed to plugin | |
177 | * | |
178 | * @return array altered $data. | |
179 | */ | |
180 | function hook_demo_plugin_render_editlink($data) | |
181 | { | |
182 | // Load HTML into a string | |
183 | $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html'); | |
184 | ||
185 | // replace value in HTML if it exists in $data | |
186 | if (!empty($data['link']['stuff'])) { | |
187 | $html = sprintf($html, $data['link']['stuff']); | |
188 | } else { | |
189 | $html = sprintf($html, ''); | |
190 | } | |
191 | ||
192 | // field_plugin | |
193 | $data['edit_link_plugin'][] = $html; | |
194 | ||
195 | return $data; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Hook render_tools. | |
200 | * | |
201 | * Template placeholders: | |
202 | * - tools_plugin: after other tools. | |
203 | * | |
204 | * @param array $data data passed to plugin | |
205 | * | |
206 | * @return array altered $data. | |
207 | */ | |
208 | function hook_demo_plugin_render_tools($data) | |
209 | { | |
210 | // field_plugin | |
211 | $data['tools_plugin'][] = 'tools_plugin'; | |
212 | ||
213 | return $data; | |
214 | } | |
215 | ||
216 | /** | |
217 | * Hook render_picwall. | |
218 | * | |
219 | * Template placeholders: | |
220 | * - plugin_start_zone: page start. | |
221 | * - plugin_end_zone: page end. | |
222 | * | |
223 | * Data: | |
224 | * - _LOGGEDIN_: true/false | |
225 | * | |
226 | * @param array $data data passed to plugin | |
227 | * | |
228 | * @return array altered $data. | |
229 | */ | |
230 | function hook_demo_plugin_render_picwall($data) | |
231 | { | |
232 | // plugin_start_zone | |
233 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
234 | // plugin_end_zone | |
235 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
236 | ||
237 | return $data; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Hook render_tagcloud. | |
242 | * | |
243 | * Template placeholders: | |
244 | * - plugin_start_zone: page start. | |
245 | * - plugin_end_zone: page end. | |
246 | * | |
247 | * Data: | |
248 | * - _LOGGEDIN_: true/false | |
249 | * | |
250 | * @param array $data data passed to plugin | |
251 | * | |
252 | * @return array altered $data. | |
253 | */ | |
254 | function hook_demo_plugin_render_tagcloud($data) | |
255 | { | |
256 | // plugin_start_zone | |
257 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
258 | // plugin_end_zone | |
259 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
260 | ||
261 | return $data; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Hook render_daily. | |
266 | * | |
267 | * Template placeholders: | |
268 | * - plugin_start_zone: page start. | |
269 | * - plugin_end_zone: page end. | |
270 | * | |
271 | * Data: | |
272 | * - _LOGGEDIN_: true/false | |
273 | * | |
274 | * @param array $data data passed to plugin | |
275 | * | |
276 | * @return array altered $data. | |
277 | */ | |
278 | function hook_demo_plugin_render_daily($data) | |
279 | { | |
280 | // plugin_start_zone | |
281 | $data['plugin_start_zone'][] = '<center>BEFORE</center>'; | |
282 | // plugin_end_zone | |
283 | $data['plugin_end_zone'][] = '<center>AFTER</center>'; | |
284 | ||
285 | ||
286 | // Manipulate columns data | |
287 | foreach ($data['cols'] as &$value) { | |
288 | foreach ($value as &$value2) { | |
289 | $value2['formatedDescription'] .= ' ಠ_ಠ'; | |
290 | } | |
291 | } | |
292 | ||
293 | // Add plugin content at the end of each link | |
294 | foreach ($data['cols'] as &$value) { | |
295 | foreach ($value as &$value2) { | |
296 | $value2['link_plugin'][] = 'DEMO'; | |
297 | } | |
298 | } | |
299 | ||
300 | return $data; | |
301 | } | |
302 | ||
303 | /* | |
304 | * DATA SAVING HOOK. | |
305 | */ | |
306 | ||
307 | /** | |
308 | * Hook savelink. | |
309 | * | |
310 | * Triggered when a link is save (new or edit). | |
311 | * All new links now contain a 'stuff' value. | |
312 | * | |
313 | * @param array $data contains the new link data. | |
314 | * | |
315 | * @return array altered $data. | |
316 | */ | |
317 | function hook_demo_plugin_save_link($data) | |
318 | { | |
319 | ||
320 | // Save stuff added in editlink field | |
321 | if (!empty($_POST['lf_stuff'])) { | |
322 | $data['stuff'] = escape($_POST['lf_stuff']); | |
323 | } | |
324 | ||
325 | return $data; | |
326 | } | |
327 | ||
328 | /** | |
329 | * Hook delete_link. | |
330 | * | |
331 | * Triggered when a link is deleted. | |
332 | * | |
333 | * @param array $data contains the link to be deleted. | |
334 | * | |
335 | * @return array altered data. | |
336 | */ | |
337 | function hook_demo_plugin_delete_link($data) | |
338 | { | |
339 | if (strpos($data['url'], 'youtube.com') !== false) { | |
340 | exit('You can not delete a YouTube link. Don\'t ask.'); | |
341 | } | |
bcd078bf A |
342 | } |
343 | ||
344 | /** | |
345 | * Execute render_feed hook. | |
346 | * Called with ATOM and RSS feed. | |
347 | * | |
348 | * Special data keys: | |
349 | * - _PAGE_: current page | |
350 | * - _LOGGEDIN_: true/false | |
351 | * | |
352 | * @param array $data data passed to plugin | |
353 | * | |
354 | * @return array altered $data. | |
355 | */ | |
356 | function hook_demo_plugin_render_feed($data) | |
357 | { | |
358 | foreach ($data['links'] as &$link) { | |
359 | if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) { | |
360 | $link['description'] .= ' - ATOM Feed' ; | |
361 | } | |
362 | elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) { | |
363 | $link['description'] .= ' - RSS Feed'; | |
364 | } | |
365 | } | |
366 | return $data; | |
367 | } |