button.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. }
  37. /**
  38. * Handle matches of the lsb syntax
  39. *
  40. * @param string $match The match of the syntax
  41. * @param int $state The state of the handler
  42. * @param int $pos The position in the document
  43. * @param Doku_Handler $handler The handler
  44. * @return array Data for the renderer
  45. */
  46. public function handle($match, $state, $pos, Doku_Handler &$handler){
  47. $match = strtolower(trim(substr($match, 5, -2))); // strip markup
  48. if (empty($match)) {
  49. $match = strtolower(trim($this->getConf('networks')));
  50. }
  51. return explode(' ', $match);
  52. }
  53. /**
  54. * Render xhtml output or metadata
  55. *
  56. * @param string $mode Renderer mode (supported modes: xhtml)
  57. * @param Doku_Renderer $renderer The renderer
  58. * @param array $data The data from the handler() function
  59. * @return bool If rendering was successful.
  60. */
  61. public function render($mode, Doku_Renderer &$renderer, $data) {
  62. if($mode != 'xhtml') return false;
  63. // validation list of available social networks
  64. $protected = array('twitter', 'facebook', 'googleplus', 'linkedin', 'pinterest', 'tumblr', 'reddit', 'taringa', 'email');
  65. $renderer->doc .= '<ul class="lsb">';
  66. foreach ($data as $network) {
  67. if (in_array($network, $protected)) {
  68. $renderer->doc .= $this->lsb_button($network);
  69. }
  70. }
  71. $renderer->doc .= '</ul>';
  72. return true;
  73. }
  74. /**
  75. * Render xhtml output for share buttons
  76. *
  77. * @param string $network The social network to render the button to
  78. * @return string Xhtml code for button.
  79. */
  80. protected function lsb_button ($network) {
  81. global $ID;
  82. global $INFO;
  83. $url = rawurlencode(wl($ID,'',true));
  84. $title = rawurlencode(($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta);
  85. $abstract = rawurlencode($INFO['meta']['description']['abstract']);
  86. //$text = $this->lsb_getText($network);
  87. $class = $this->getConf('display') . '-' . $network;
  88. // see: http://builtbyboon.com/blog/simple-social-sharing-buttons
  89. // see: https://github.com/cferdinandi/social-sharing
  90. // <a href="https://twitter.com/intent/tweet?text=YOUR-TITLE&url=YOUR-URL&via=TWITTER-HANDLE">Tweet</a>
  91. // <a href="https://www.facebook.com/sharer/sharer.php?u=YOUR-URL">Share on Facebook</a>
  92. // <a href="https://plus.google.com/share?url=YOUR-URL">Plus on googleplus</a>
  93. // <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>
  94. // <a href="https://pinterest.com/pin/create/button/?url=YOUR-URL&description=YOUR-DESCRIPTION&media=YOUR-IMAGE-SRC">Pin on Pinterest</a>
  95. // <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>
  96. // <a href="https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url=YOUR-URL">Share on Xing</a>
  97. // <a href="http://www.tumblr.com/share/link?url=YOUR-URL&description=YOUR-DESCRIPTION">Share on Tumblr</a>
  98. // <a href="http://www.reddit.com/submit?url=YOUR_URL&title=YOUR_TITLE">Share on Reddit</a>
  99. // <a href="http://www.taringa.net/widgets/share.php?url=YOUR_URL&body=YOUR-DESCRIPTION">Compartir en Taringa</a>
  100. // <a href="mailto:?subject=YOUR-TITLE&body=YOUR-SUMMARY">Email</a>
  101. switch ($network) {
  102. case 'twitter':
  103. $name = 'Twitter';
  104. $href = 'https://twitter.com/intent/tweet?url=' . $url . '&text='. $title;
  105. break;
  106. case 'facebook':
  107. $name = 'Facebook';
  108. $href = 'http://www.facebook.com/sharer.php?u='. $url;
  109. break;
  110. case 'googleplus':
  111. $name = 'Google+';
  112. $href = 'https://plus.google.com/share?url='. $url;
  113. break;
  114. case 'linkedin':
  115. $name = 'LinkedIn';
  116. $href = 'https://www.linkedin.com/shareArticle?url='. $url .'&title=' . $title . '&summary=' . $abstract . '&mini=true&source=' . $url;
  117. break;
  118. case 'pinterest':
  119. $name = 'Pinterest';
  120. $href = 'https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $abstract;
  121. break;
  122. case 'tumblr':
  123. $name = 'Tumblr';
  124. $href = 'http://www.tumblr.com/share/link?url='. $url .'&description=' . $abstract;
  125. break;
  126. case 'reddit':
  127. $name = 'Reddit';
  128. $href = 'http://www.reddit.com/submit?url='. $url .'&title=' . $title;
  129. break;
  130. case 'taringa':
  131. $name = 'Taringa';
  132. $href = 'http://www.taringa.net/widgets/share.php?url='. $url .'&body=' . $abstract;
  133. break;
  134. case 'email':
  135. $name = 'Email';
  136. $href = 'mailto:?subject='. $title .'&body=' . $url . ': '. $abstract;
  137. break;
  138. }
  139. $xhtml = '<li class="lsb-item-' . $class . '">';
  140. $xhtml .= '<a class="lsb-link-' . $class . '" href="' . $href . '">' . $name . '</a>';
  141. $xhtml .= '</li>';
  142. return $xhtml;
  143. }
  144. /**
  145. * Returns the thext to display in share button
  146. *
  147. * @param string $network The social network to get the text to
  148. * @return string text for button or empty string to show just the icon.
  149. */
  150. // protected function lsb_getText($network) {
  151. // $display = $this->getConf('display');
  152. // if ($display == 'text') {
  153. // return $this->getLang($network . '_text');
  154. // } else {
  155. // // no text, just the icon
  156. // return '';
  157. // }
  158. // }
  159. }