demo_plugin.php 8.0 KB

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