ShareNoticePlugin.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package ShareNoticePlugin
  21. * @maintainer Brion Vibber <brion@status.net>
  22. */
  23. if (!defined('STATUSNET')) { exit(1); }
  24. class ShareNoticePlugin extends Plugin
  25. {
  26. public $targets = array(
  27. array('Twitter'),
  28. array('Facebook'),
  29. array('StatusNet', array('baseurl' => 'http://identi.ca'))
  30. );
  31. public function onEndShowStylesheets(Action $action) {
  32. $action->cssLink($this->path('css/sharenotice.css'));
  33. return true;
  34. }
  35. function onStartShowNoticeItem($item)
  36. {
  37. $notice = $item->notice;
  38. $out = $item->out;
  39. $out->elementStart('ul', array('class' => 'notice-share'));
  40. foreach ($this->targets as $data) {
  41. $type = $data[0];
  42. $args = (count($data) > 1) ? $data[1] : array();
  43. $target = $this->getShareTarget($type, $notice, $args);
  44. $this->showShareTarget($out, $target);
  45. }
  46. $out->elementEnd('ul');
  47. }
  48. private function getShareTarget($type, $notice, $args)
  49. {
  50. $class = ucfirst($type) . 'ShareTarget';
  51. return new $class($notice, $args);
  52. }
  53. private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
  54. {
  55. $class = $target->getClass();
  56. $text = $target->getText();
  57. $url = $target->targetUrl();
  58. $out->elementStart('li', array('class' => 'notice-share-' . $class));
  59. $out->elementStart('a', array(
  60. 'href' => $url,
  61. 'title' => $text,
  62. 'target' => '_blank'
  63. ));
  64. $out->element('span', array(), $text);
  65. $out->elementEnd('a');
  66. $out->elementEnd('li');
  67. }
  68. }
  69. abstract class NoticeShareTarget
  70. {
  71. protected $notice;
  72. public function __construct($notice)
  73. {
  74. $this->notice = $notice;
  75. }
  76. public abstract function getClass();
  77. public abstract function getText();
  78. public abstract function targetUrl();
  79. }
  80. abstract class GenericNoticeShareTarget extends NoticeShareTarget
  81. {
  82. protected function maxLength()
  83. {
  84. return 140; // typical
  85. }
  86. protected function statusText()
  87. {
  88. // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
  89. $pattern = _m('"%s"');
  90. $url = $this->notice->getUrl();
  91. $suffix = ' ' . $url;
  92. $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
  93. $content = $this->notice->content;
  94. // TRANS: Truncation symbol.
  95. $truncation_symbol = _m('…');
  96. $truncation_symbol_length = mb_strlen($truncation_symbol);
  97. if (mb_strlen($content) > $room) {
  98. $content = mb_substr($content, 0, $room - $truncation_symbol_length) . $truncation_symbol;
  99. }
  100. return sprintf($pattern, $content) . $suffix;
  101. }
  102. }
  103. class TwitterShareTarget extends GenericNoticeShareTarget
  104. {
  105. public function getClass()
  106. {
  107. return 'twitter';
  108. }
  109. public function getText()
  110. {
  111. // TRANS: Tooltip for image to share a notice on Twitter.
  112. return _m('Share on Twitter');
  113. }
  114. public function targetUrl()
  115. {
  116. $args = array(
  117. 'status' => $this->statusText()
  118. );
  119. return 'http://twitter.com/home?' .
  120. http_build_query($args, null, '&');
  121. }
  122. }
  123. class StatusNetShareTarget extends GenericNoticeShareTarget
  124. {
  125. protected $baseurl;
  126. public function __construct($notice, $args)
  127. {
  128. parent::__construct($notice);
  129. $this->baseurl = $args['baseurl'];
  130. }
  131. public function getClass()
  132. {
  133. return 'statusnet';
  134. }
  135. public function getText()
  136. {
  137. $host = parse_url($this->baseurl, PHP_URL_HOST);
  138. // TRANS: Tooltip for image to share a notice on another platform (other than Twitter or Facebook).
  139. // TRANS: %s is a host name.
  140. return sprintf(_m('Share on %s'), $host);
  141. }
  142. public function targetUrl()
  143. {
  144. $args = array(
  145. 'status_textarea' => $this->statusText()
  146. );
  147. return $this->baseurl . '/notice/new?' .
  148. http_build_query($args, null, '&');
  149. }
  150. }
  151. class FacebookShareTarget extends NoticeShareTarget
  152. {
  153. public function getClass()
  154. {
  155. return 'facebook';
  156. }
  157. public function getText()
  158. {
  159. // TRANS: Tooltip for image to share a notice on Facebook.
  160. return _m('Share on Facebook');
  161. }
  162. public function targetUrl()
  163. {
  164. $args = array(
  165. 'u' => $this->notice->getUrl(),
  166. // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
  167. 't' => sprintf(_m('"%s"'), $this->notice->content),
  168. );
  169. return 'http://www.facebook.com/sharer.php?' .
  170. http_build_query($args, null, '&');
  171. }
  172. /**
  173. * Provide plugin version information.
  174. *
  175. * This data is used when showing the version page.
  176. *
  177. * @param array &$versions array of version data arrays; see EVENTS.txt
  178. *
  179. * @return boolean hook value
  180. */
  181. function onPluginVersion(&$versions)
  182. {
  183. $url = 'http://status.net/wiki/Plugin:ShareNotice';
  184. $versions[] = array('name' => 'ShareNotice',
  185. 'version' => GNUSOCIAL_VERSION,
  186. 'author' => 'Brion Vibber',
  187. 'homepage' => $url,
  188. 'rawdescription' =>
  189. // TRANS: Plugin description.
  190. _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));
  191. return true;
  192. }
  193. }