demo_plugin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /*
  49. * Links in toolbar:
  50. * A link is an array of its attributes (key="value"),
  51. * and a mandatory `html` key, which contains its value.
  52. */
  53. $button = array(
  54. 'attr' => array (
  55. 'href' => '#',
  56. 'class' => 'mybutton',
  57. 'title' => 'hover me',
  58. ),
  59. 'html' => 'DEMO buttons toolbar',
  60. );
  61. $data['buttons_toolbar'][] = $button;
  62. }
  63. /*
  64. * Add additional input fields in the tools.
  65. * A field is an array containing:
  66. * [
  67. * 'form-attribute-1' => 'form attribute 1 value',
  68. * 'form-attribute-2' => 'form attribute 2 value',
  69. * 'inputs' => [
  70. * [
  71. * 'input-1-attribute-1 => 'input 1 attribute 1 value',
  72. * 'input-1-attribute-2 => 'input 1 attribute 2 value',
  73. * ],
  74. * [
  75. * 'input-2-attribute-1 => 'input 2 attribute 1 value',
  76. * ],
  77. * ],
  78. * ]
  79. * This example renders as:
  80. * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
  81. * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
  82. * <input input-2-attribute-1="input 2 attribute 1 value">
  83. * </form>
  84. */
  85. $form = array(
  86. 'attr' => array(
  87. 'method' => 'GET',
  88. 'action' => '?',
  89. 'class' => 'addform',
  90. ),
  91. 'inputs' => array(
  92. array(
  93. 'type' => 'text',
  94. 'name' => 'demo',
  95. 'placeholder' => 'demo',
  96. )
  97. )
  98. );
  99. $data['fields_toolbar'][] = $form;
  100. }
  101. // Another button always displayed
  102. $button = array(
  103. 'attr' => array(
  104. 'href' => '#',
  105. ),
  106. 'html' => 'Demo',
  107. );
  108. $data['buttons_toolbar'][] = $button;
  109. return $data;
  110. }
  111. /**
  112. * Hook render_includes.
  113. * Executed on every page redering.
  114. *
  115. * Template placeholders:
  116. * - css_files
  117. *
  118. * Data:
  119. * - _PAGE_: current page
  120. * - _LOGGEDIN_: true/false
  121. *
  122. * @param array $data data passed to plugin
  123. *
  124. * @return array altered $data.
  125. */
  126. function hook_demo_plugin_render_includes($data)
  127. {
  128. // List of plugin's CSS files.
  129. // Note that you just need to specify CSS path.
  130. $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
  131. return $data;
  132. }
  133. /**
  134. * Hook render_footer.
  135. * Executed on every page redering.
  136. *
  137. * Template placeholders:
  138. * - text
  139. * - endofpage
  140. * - js_files
  141. *
  142. * Data:
  143. * - _PAGE_: current page
  144. * - _LOGGEDIN_: true/false
  145. *
  146. * @param array $data data passed to plugin
  147. *
  148. * @return array altered $data.
  149. */
  150. function hook_demo_plugin_render_footer($data)
  151. {
  152. // footer text
  153. $data['text'][] = 'Shaarli is now enhanced by the awesome demo_plugin.';
  154. // Free elements at the end of the page.
  155. $data['endofpage'][] = '<marquee id="demo_marquee">' .
  156. 'DEMO: it\'s 1999 all over again!' .
  157. '</marquee>';
  158. // List of plugin's JS files.
  159. // Note that you just need to specify CSS path.
  160. $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
  161. return $data;
  162. }
  163. /*
  164. * SPECIFIC PAGES
  165. */
  166. /**
  167. * Hook render_linklist.
  168. *
  169. * Template placeholders:
  170. * - action_plugin: next to 'private only' button.
  171. * - plugin_start_zone: page start
  172. * - plugin_end_zone: page end
  173. * - link_plugin: icons below each links.
  174. *
  175. * Data:
  176. * - _LOGGEDIN_: true/false
  177. *
  178. * @param array $data data passed to plugin
  179. *
  180. * @return array altered $data.
  181. */
  182. function hook_demo_plugin_render_linklist($data)
  183. {
  184. /*
  185. * Action links (action_plugin):
  186. * A link is an array of its attributes (key="value"),
  187. * and a mandatory `html` key, which contains its value.
  188. * It's also recommended to add key 'on' or 'off' for theme rendering.
  189. */
  190. $action = array(
  191. 'attr' => array(
  192. 'href' => '?up',
  193. 'title' => 'Uppercase!',
  194. ),
  195. 'html' => '←',
  196. );
  197. if (isset($_GET['up'])) {
  198. // Manipulate link data
  199. foreach ($data['links'] as &$value) {
  200. $value['description'] = strtoupper($value['description']);
  201. $value['title'] = strtoupper($value['title']);
  202. }
  203. $action['on'] = true;
  204. } else {
  205. $action['off'] = true;
  206. }
  207. $data['action_plugin'][] = $action;
  208. // link_plugin (for each link)
  209. foreach ($data['links'] as &$value) {
  210. $value['link_plugin'][] = ' DEMO \o/';
  211. }
  212. // plugin_start_zone
  213. $data['plugin_start_zone'][] = '<center>BEFORE</center>';
  214. // plugin_start_zone
  215. $data['plugin_end_zone'][] = '<center>AFTER</center>';
  216. return $data;
  217. }
  218. /**
  219. * Hook render_editlink.
  220. *
  221. * Template placeholders:
  222. * - field_plugin: add link fields after tags.
  223. *
  224. * @param array $data data passed to plugin
  225. *
  226. * @return array altered $data.
  227. */
  228. function hook_demo_plugin_render_editlink($data)
  229. {
  230. // Load HTML into a string
  231. $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html');
  232. // replace value in HTML if it exists in $data
  233. if (!empty($data['link']['stuff'])) {
  234. $html = sprintf($html, $data['link']['stuff']);
  235. } else {
  236. $html = sprintf($html, '');
  237. }
  238. // field_plugin
  239. $data['edit_link_plugin'][] = $html;
  240. return $data;
  241. }
  242. /**
  243. * Hook render_tools.
  244. *
  245. * Template placeholders:
  246. * - tools_plugin: after other tools.
  247. *
  248. * @param array $data data passed to plugin
  249. *
  250. * @return array altered $data.
  251. */
  252. function hook_demo_plugin_render_tools($data)
  253. {
  254. // field_plugin
  255. $data['tools_plugin'][] = 'tools_plugin';
  256. return $data;
  257. }
  258. /**
  259. * Hook render_picwall.
  260. *
  261. * Template placeholders:
  262. * - plugin_start_zone: page start.
  263. * - plugin_end_zone: page end.
  264. *
  265. * Data:
  266. * - _LOGGEDIN_: true/false
  267. *
  268. * @param array $data data passed to plugin
  269. *
  270. * @return array altered $data.
  271. */
  272. function hook_demo_plugin_render_picwall($data)
  273. {
  274. // plugin_start_zone
  275. $data['plugin_start_zone'][] = '<center>BEFORE</center>';
  276. // plugin_end_zone
  277. $data['plugin_end_zone'][] = '<center>AFTER</center>';
  278. return $data;
  279. }
  280. /**
  281. * Hook render_tagcloud.
  282. *
  283. * Template placeholders:
  284. * - plugin_start_zone: page start.
  285. * - plugin_end_zone: page end.
  286. *
  287. * Data:
  288. * - _LOGGEDIN_: true/false
  289. *
  290. * @param array $data data passed to plugin
  291. *
  292. * @return array altered $data.
  293. */
  294. function hook_demo_plugin_render_tagcloud($data)
  295. {
  296. // plugin_start_zone
  297. $data['plugin_start_zone'][] = '<center>BEFORE</center>';
  298. // plugin_end_zone
  299. $data['plugin_end_zone'][] = '<center>AFTER</center>';
  300. return $data;
  301. }
  302. /**
  303. * Hook render_daily.
  304. *
  305. * Template placeholders:
  306. * - plugin_start_zone: page start.
  307. * - plugin_end_zone: page end.
  308. *
  309. * Data:
  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_daily($data)
  317. {
  318. // plugin_start_zone
  319. $data['plugin_start_zone'][] = '<center>BEFORE</center>';
  320. // plugin_end_zone
  321. $data['plugin_end_zone'][] = '<center>AFTER</center>';
  322. // Manipulate columns data
  323. foreach ($data['cols'] as &$value) {
  324. foreach ($value as &$value2) {
  325. $value2['formatedDescription'] .= ' ಠ_ಠ';
  326. }
  327. }
  328. // Add plugin content at the end of each link
  329. foreach ($data['cols'] as &$value) {
  330. foreach ($value as &$value2) {
  331. $value2['link_plugin'][] = 'DEMO';
  332. }
  333. }
  334. return $data;
  335. }
  336. /*
  337. * DATA SAVING HOOK.
  338. */
  339. /**
  340. * Hook savelink.
  341. *
  342. * Triggered when a link is save (new or edit).
  343. * All new links now contain a 'stuff' value.
  344. *
  345. * @param array $data contains the new link data.
  346. *
  347. * @return array altered $data.
  348. */
  349. function hook_demo_plugin_save_link($data)
  350. {
  351. // Save stuff added in editlink field
  352. if (!empty($_POST['lf_stuff'])) {
  353. $data['stuff'] = escape($_POST['lf_stuff']);
  354. }
  355. return $data;
  356. }
  357. /**
  358. * Hook delete_link.
  359. *
  360. * Triggered when a link is deleted.
  361. *
  362. * @param array $data contains the link to be deleted.
  363. *
  364. * @return array altered data.
  365. */
  366. function hook_demo_plugin_delete_link($data)
  367. {
  368. if (strpos($data['url'], 'youtube.com') !== false) {
  369. exit('You can not delete a YouTube link. Don\'t ask.');
  370. }
  371. }
  372. /**
  373. * Execute render_feed hook.
  374. * Called with ATOM and RSS feed.
  375. *
  376. * Special data keys:
  377. * - _PAGE_: current page
  378. * - _LOGGEDIN_: true/false
  379. *
  380. * @param array $data data passed to plugin
  381. *
  382. * @return array altered $data.
  383. */
  384. function hook_demo_plugin_render_feed($data)
  385. {
  386. foreach ($data['links'] as &$link) {
  387. if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) {
  388. $link['description'] .= ' - ATOM Feed' ;
  389. }
  390. elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) {
  391. $link['description'] .= ' - RSS Feed';
  392. }
  393. }
  394. return $data;
  395. }