button.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * DokuWiki Plugin lsb (Syntax Component)
  4. *
  5. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  6. * @author Fernando Ribeiro <pinguim.ribeiro@gmail.com>
  7. */
  8. // must be run within Dokuwiki
  9. if (!defined('DOKU_INC')) die();
  10. class syntax_plugin_lsb_button extends DokuWiki_Syntax_Plugin {
  11. /**
  12. * @return string Syntax mode type
  13. */
  14. public function getType() {
  15. return 'substition';
  16. }
  17. /**
  18. * @return string Paragraph type
  19. */
  20. public function getPType() {
  21. return 'normal';
  22. }
  23. /**
  24. * @return int Sort order - Low numbers go before high numbers
  25. */
  26. public function getSort() {
  27. return 999;
  28. }
  29. /**
  30. * Connect lookup pattern to lexer.
  31. *
  32. * @param string $mode Parser mode
  33. */
  34. public function connectTo($mode) {
  35. $this->Lexer->addSpecialPattern('~~LSB\b.*?~~',$mode,'plugin_lsb_button');
  36. // $this->Lexer->addEntryPattern('<FIXME>',$mode,'plugin_lsb_button');
  37. }
  38. // public function postConnect() {
  39. // $this->Lexer->addExitPattern('</FIXME>','plugin_lsb_button');
  40. // }
  41. /**
  42. * Handle matches of the lsb syntax
  43. *
  44. * @param string $match The match of the syntax
  45. * @param int $state The state of the handler
  46. * @param int $pos The position in the document
  47. * @param Doku_Handler $handler The handler
  48. * @return array Data for the renderer
  49. */
  50. public function handle($match, $state, $pos, Doku_Handler &$handler){
  51. $match = strtolower(trim(substr($match, 5, -2))); // strip markup
  52. if (empty($match)) {
  53. $match = strtolower(trim($this->getConf('networks')));
  54. }
  55. return explode(' ', $match);
  56. }
  57. /**
  58. * Render xhtml output or metadata
  59. *
  60. * @param string $mode Renderer mode (supported modes: xhtml)
  61. * @param Doku_Renderer $renderer The renderer
  62. * @param array $data The data from the handler() function
  63. * @return bool If rendering was successful.
  64. */
  65. public function render($mode, Doku_Renderer &$renderer, $data) {
  66. if($mode != 'xhtml') return false;
  67. $renderer->doc .= '<ul class="lsb">';
  68. // validation list of available social networks
  69. $protected = array('twitter', 'facebook', 'google+', 'linkedin', 'pinterest', 'tumblr', 'reddit');
  70. foreach ($data as $network) {
  71. if (in_array($network, $protected)) {
  72. $renderer->doc .= $this->lsb_button($network);
  73. }
  74. }
  75. $renderer->doc .= '</ul>';
  76. return true;
  77. }
  78. /**
  79. * Render xhtml output for facebook share button
  80. *
  81. * @param string $network The social network to render the button to
  82. * @return string Xhtml code for button.
  83. */
  84. protected function lsb_button ($network) {
  85. global $ID;
  86. global $INFO;
  87. $url = rawurlencode(wl($ID,'',true));
  88. $title = rawurlencode(($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta);
  89. $abstract = rawurlencode($INFO['meta']['description']['abstract']);
  90. $text = $this->lsb_getText($network);
  91. $class = 'ico-' . $network;
  92. // see: https://github.com/cferdinandi/social-sharing
  93. // <a href="https://twitter.com/intent/tweet?text=YOUR-TITLE&url=YOUR-URL&via=TWITTER-HANDLE">Tweet</a>
  94. // <a href="https://www.facebook.com/sharer/sharer.php?u=YOUR-URL">Share on Facebook</a>
  95. // <a href="https://plus.google.com/share?url=YOUR-URL">Plus on Google+</a>
  96. // <a href="https://www.linkedin.com/shareArticle?mini=true&url=YOUR-URL&title=YOUR-TITLE&summary=YOUR-SUMMARY&source=YOUR-URL">Share on LinkedIn</a>
  97. // <a href="https://pinterest.com/pin/create/button/?url=YOUR-URL&description=YOUR-DESCRIPTION&media=YOUR-IMAGE-SRC">Pin on Pinterest</a>
  98. // <a href="https://vk.com/share.php?url=YOUR-URL&title=YOUR-TITLE&description=YOUR-DESCRIPTION&image=YOUR-IMAGE-SRC&noparse=true">Share on VK</a>
  99. // <a href="https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url=YOUR-URL">Share on Xing</a>
  100. // <a href="http://www.tumblr.com/share/link?url=YOUR-URL&description=YOUR-DESCRIPTION">Share on Tumblr</a>
  101. // <a href="http://www.reddit.com/submit?url=YOUR_URL&title=YOUR_TITLE">Share on Reddit</a>
  102. switch ($network) {
  103. case 'twitter':
  104. $href = 'https://twitter.com/intent/tweet?url=' . $url . '&text='. $title .'&via=TWITTER-HANDLE';
  105. break;
  106. case 'facebook':
  107. $href = 'http://www.facebook.com/sharer.php?u='. $url;
  108. break;
  109. case 'google+':
  110. $href = 'https://plus.google.com/share?url='. $url;
  111. break;
  112. case 'linkedin':
  113. $href = 'href="https://www.linkedin.com/shareArticle?url='. $url .'&title=' . $title . '&summary=' . $abstract . '&mini=true&source=' . $url;
  114. break;
  115. case 'pinterest':
  116. $href = 'https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $abstract . '&media=YOUR-IMAGE-SRC';
  117. break;
  118. case 'tumblr':
  119. $href = 'http://www.tumblr.com/share/link?url='. $url .'&description=' . $abstract;
  120. break;
  121. case 'reddit':
  122. $href = 'http://www.reddit.com/submit?url='. $url .'&title=' . $title;
  123. break;
  124. }
  125. $xhtml = '<li class="lsb-item">';
  126. $xhtml .= '<a class="lsb-link ' . $class . '" href="' . $href . '">' . $text . '</a>';
  127. $xhtml .= '</li>';
  128. return $xhtml;
  129. }
  130. protected function lsb_getText($network) {
  131. $display = $this->getConf('display');
  132. if ($display == 'name') {
  133. return $network;
  134. } elseif ($display == 'text') {
  135. return $this->getLang($network.'_text');
  136. } else {
  137. return '';
  138. }
  139. }
  140. }