demo_plugin.php 7.1 KB

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