button.php 7.1 KB

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