button.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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', 'delicious',
  78. 'stumbleupon', '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 to render the button to
  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. $class = $display . '-' . $network;
  107. // see: http://builtbyboon.com/blog/simple-social-sharing-buttons
  108. // see: https://github.com/cferdinandi/social-sharing
  109. // see: https://github.com/bradvin/social-share-urls
  110. // see: http://brandcolors.net/
  111. switch ($network) {
  112. case 'twitter':
  113. $name = 'Twitter';
  114. $href = 'https://twitter.com/intent/tweet?url=' . $url . '&text='. $title;
  115. if (!empty($this->getConf('twitter_user'))) { $href .= "&via=" . rawurlencode($this->getConf('twitter_user')); }
  116. break;
  117. case 'facebook':
  118. $name = 'Facebook';
  119. $href = 'http://www.facebook.com/sharer.php?u='. $url;
  120. break;
  121. case 'googleplus':
  122. $name = 'Google+';
  123. $href = 'https://plus.google.com/share?url='. $url;
  124. break;
  125. case 'linkedin':
  126. $name = 'LinkedIn';
  127. $href = 'https://www.linkedin.com/shareArticle?url='. $url .'&title=' . $title . '&summary=' . $abstract . '&mini=true&source=' . $url;
  128. break;
  129. case 'pinterest':
  130. $name = 'Pinterest';
  131. $href = 'https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $title;
  132. break;
  133. case 'tumblr':
  134. $name = 'Tumblr';
  135. $href = 'http://www.tumblr.com/share/link?url='. $url .'&description=' . $title;
  136. break;
  137. case 'reddit':
  138. $name = 'Reddit';
  139. $href = 'http://www.reddit.com/submit?url='. $url .'&title=' . $title;
  140. break;
  141. case 'taringa':
  142. $name = 'Taringa';
  143. $href = 'http://www.taringa.net/widgets/share.php?url='. $url .'&body=' . $title;
  144. break;
  145. case 'delicious':
  146. $name = 'Delicious';
  147. $href = 'https://delicious.com/save?v=5&noui&jump=close&url='. $url .'&title=' . $title;
  148. if (!empty($this->getConf('delicious_provider'))) { $href .= "&provider=" . rawurlencode($this->getConf('delicious_provider')); }
  149. break;
  150. case 'stumbleupon':
  151. $name = 'StumbleUpon';
  152. $href = 'http://www.stumbleupon.com/submit?url='. $url .'&title=' . $title;
  153. break;
  154. case 'xing':
  155. $name = 'Xing';
  156. $href = 'https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url='. $url;
  157. break;
  158. case 'vk':
  159. $name = 'Vk';
  160. $href = 'https://vk.com/share.php?url='. $url .'&title=' . $title . '&description=' . $abstract . '&noparse=true';
  161. break;
  162. case 'email':
  163. $name = 'Email';
  164. $href = 'mailto:?subject='. $title .'&body=' . $url . ': '. $abstract;
  165. break;
  166. }
  167. $xhtml = '<li class="socialite-item-' . $class . '">';
  168. $xhtml .= '<a class="socialite-link-' . $class . '" href="' . $href . '">' . $name . '</a>';
  169. $xhtml .= '</li>';
  170. return $xhtml;
  171. }
  172. }