button.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * DokuWiki Plugin socialite (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_socialite_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('~~socialite\b.*?~~',$mode,'plugin_socialite_button');
  36. }
  37. /**
  38. * Handle matches of the socialite 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, 11, -2))); // strip markup
  48. // checks if a display mode is passed
  49. if (substr($match, 0, 1) === ':') {
  50. list($display) = explode(' ', $match);
  51. $display = substr($display, 1);
  52. $networks = trim(substr($match, strlen($display) + 1 ));
  53. }
  54. if (empty($networks)) {
  55. $networks = strtolower(trim($this->getConf('networks')));
  56. }
  57. return array(
  58. 'display' => $display,
  59. 'networks' => explode(' ', $networks)
  60. );
  61. }
  62. /**
  63. * Render xhtml output or metadata
  64. *
  65. * @param string $mode Renderer mode (supported modes: xhtml)
  66. * @param Doku_Renderer $renderer The renderer
  67. * @param array $data The data from the handler() function
  68. * @return bool If rendering was successful.
  69. */
  70. public function render($mode, Doku_Renderer $renderer, $data) {
  71. if($mode != 'xhtml') return false;
  72. // validation list of available display modes
  73. $valid_displays = array('name', 'icon', 'color');
  74. // validation list of available social networks
  75. $valid_networks = array('twitter', 'facebook', 'googleplus',
  76. 'linkedin', 'pinterest', 'tumblr',
  77. 'reddit', 'taringa', 'stumbleupon',
  78. 'telegram', 'hackernews', 'xing', 'vk', 'email');
  79. if (in_array($data['display'], $valid_displays)) {
  80. $display = $data['display'];
  81. } else {
  82. $display = $this->getConf('display') ;
  83. }
  84. $renderer->doc .= '<ul class="socialite">';
  85. foreach ($data['networks'] as $network) {
  86. if (in_array($network, $valid_networks)) {
  87. $renderer->doc .= $this->socialite_button($display, $network);
  88. }
  89. }
  90. $renderer->doc .= '</ul>';
  91. return true;
  92. }
  93. /**
  94. * Render xhtml output for share buttons
  95. *
  96. * @param string $display The display mode
  97. * @param string $network The social network
  98. * @return string Xhtml code for button.
  99. */
  100. protected function socialite_button ($display, $network) {
  101. global $ID;
  102. global $INFO;
  103. $url = rawurlencode(wl($ID, '', true));
  104. $title = rawurlencode(($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta);
  105. $abstract = rawurlencode($INFO['meta']['description']['abstract']);
  106. $target = $this->getConf('target') ;
  107. switch ($network) {
  108. case 'twitter':
  109. $name = 'Twitter';
  110. $href = 'https://twitter.com/intent/tweet?url=' . $url . '&text='. $title;
  111. if ($this->getConf('twitter_user')) { $href .= "&via=" . rawurlencode($this->getConf('twitter_user')); }
  112. break;
  113. case 'facebook':
  114. $name = 'Facebook';
  115. $href = 'http://www.facebook.com/sharer.php?u='. $url;
  116. break;
  117. case 'googleplus':
  118. $name = 'Google+';
  119. $href = 'https://plus.google.com/share?url='. $url;
  120. break;
  121. case 'linkedin':
  122. $name = 'LinkedIn';
  123. $href = 'https://www.linkedin.com/shareArticle?url='. $url .'&title=' . $title . '&summary=' . $abstract . '&mini=true&source=' . $url;
  124. break;
  125. case 'pinterest':
  126. $name = 'Pinterest';
  127. $href = 'https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $title;
  128. break;
  129. case 'tumblr':
  130. $name = 'Tumblr';
  131. $href = 'http://www.tumblr.com/share/link?url='. $url .'&description=' . $title;
  132. break;
  133. case 'reddit':
  134. $name = 'Reddit';
  135. $href = 'http://www.reddit.com/submit?url='. $url .'&title=' . $title;
  136. break;
  137. case 'taringa':
  138. $name = 'Taringa';
  139. $href = 'http://www.taringa.net/widgets/share.php?url='. $url .'&body=' . $title;
  140. break;
  141. case 'stumbleupon':
  142. $name = 'StumbleUpon';
  143. $href = 'http://www.stumbleupon.com/submit?url='. $url .'&title=' . $title;
  144. break;
  145. case 'telegram':
  146. $name = 'Telegram';
  147. $href = 'https://telegram.me/share/url?url='. $url .'&text=' . $title;
  148. break;
  149. case 'hackernews':
  150. $name = 'Hacker News';
  151. $href = 'https://news.ycombinator.com/submitlink?u='. $url .'&t=' . $title;
  152. break;
  153. case 'xing':
  154. $name = 'Xing';
  155. $href = 'https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url='. $url;
  156. break;
  157. case 'vk':
  158. $name = 'Vk';
  159. $href = 'https://vk.com/share.php?url='. $url .'&title=' . $title . '&description=' . $abstract . '&noparse=true';
  160. break;
  161. case 'email':
  162. $name = 'Email';
  163. $href = 'mailto:?subject='. $title .'&body=' . $url . ': '. $abstract;
  164. break;
  165. }
  166. $xhtml = '<li class="' . $display . '">';
  167. $xhtml .= '<a class="' . $network . '" target="' . $target . '" href="' . $href . '">' . $name . '</a>';
  168. $xhtml .= '</li>';
  169. return $xhtml;
  170. }
  171. }