ShareNoticePlugin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. const PLUGIN_VERSION = '2.0.0';
  27. public $targets = array(
  28. array('Twitter'),
  29. array('Facebook'),
  30. array('StatusNet', array('baseurl' => 'http://identi.ca'))
  31. );
  32. public function onEndShowStylesheets(Action $action) {
  33. $action->cssLink($this->path('css/sharenotice.css'));
  34. return true;
  35. }
  36. function onStartShowNoticeItem($item)
  37. {
  38. $notice = $item->notice;
  39. $out = $item->out;
  40. $out->elementStart('ul', array('class' => 'notice-share'));
  41. foreach ($this->targets as $data) {
  42. $type = $data[0];
  43. $args = (count($data) > 1) ? $data[1] : array();
  44. $target = $this->getShareTarget($type, $notice, $args);
  45. $this->showShareTarget($out, $target);
  46. }
  47. $out->elementEnd('ul');
  48. }
  49. private function getShareTarget($type, $notice, $args)
  50. {
  51. $class = ucfirst($type) . 'ShareTarget';
  52. return new $class($notice, $args);
  53. }
  54. private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
  55. {
  56. $class = $target->getClass();
  57. $text = $target->getText();
  58. $url = $target->targetUrl();
  59. $out->elementStart('li', array('class' => 'notice-share-' . $class));
  60. $out->elementStart('a', array(
  61. 'href' => $url,
  62. 'title' => $text,
  63. 'target' => '_blank'
  64. ));
  65. $out->element('span', array(), $text);
  66. $out->elementEnd('a');
  67. $out->elementEnd('li');
  68. }
  69. }
  70. abstract class NoticeShareTarget
  71. {
  72. protected $notice;
  73. public function __construct($notice)
  74. {
  75. $this->notice = $notice;
  76. }
  77. public abstract function getClass();
  78. public abstract function getText();
  79. public abstract function targetUrl();
  80. }
  81. abstract class GenericNoticeShareTarget extends NoticeShareTarget
  82. {
  83. protected function maxLength()
  84. {
  85. return 140; // typical
  86. }
  87. protected function statusText()
  88. {
  89. // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
  90. $pattern = _m('"%s"');
  91. $url = $this->notice->getUrl();
  92. $suffix = ' ' . $url;
  93. $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
  94. $content = $this->notice->content;
  95. // TRANS: Truncation symbol.
  96. $truncation_symbol = _m('…');
  97. $truncation_symbol_length = mb_strlen($truncation_symbol);
  98. if (mb_strlen($content) > $room) {
  99. $content = mb_substr($content, 0, $room - $truncation_symbol_length) . $truncation_symbol;
  100. }
  101. return sprintf($pattern, $content) . $suffix;
  102. }
  103. }
  104. class TwitterShareTarget extends GenericNoticeShareTarget
  105. {
  106. public function getClass()
  107. {
  108. return 'twitter';
  109. }
  110. public function getText()
  111. {
  112. // TRANS: Tooltip for image to share a notice on Twitter.
  113. return _m('Share on Twitter');
  114. }
  115. public function targetUrl()
  116. {
  117. $args = array(
  118. 'status' => $this->statusText()
  119. );
  120. return 'http://twitter.com/home?' .
  121. http_build_query($args, null, '&');
  122. }
  123. }
  124. class StatusNetShareTarget extends GenericNoticeShareTarget
  125. {
  126. protected $baseurl;
  127. public function __construct($notice, $args)
  128. {
  129. parent::__construct($notice);
  130. $this->baseurl = $args['baseurl'];
  131. }
  132. public function getClass()
  133. {
  134. return 'statusnet';
  135. }
  136. public function getText()
  137. {
  138. $host = parse_url($this->baseurl, PHP_URL_HOST);
  139. // TRANS: Tooltip for image to share a notice on another platform (other than Twitter or Facebook).
  140. // TRANS: %s is a host name.
  141. return sprintf(_m('Share on %s'), $host);
  142. }
  143. public function targetUrl()
  144. {
  145. $args = array(
  146. 'status_textarea' => $this->statusText()
  147. );
  148. return $this->baseurl . '/notice/new?' .
  149. http_build_query($args, null, '&');
  150. }
  151. }
  152. class FacebookShareTarget extends NoticeShareTarget
  153. {
  154. public function getClass()
  155. {
  156. return 'facebook';
  157. }
  158. public function getText()
  159. {
  160. // TRANS: Tooltip for image to share a notice on Facebook.
  161. return _m('Share on Facebook');
  162. }
  163. public function targetUrl()
  164. {
  165. $args = array(
  166. 'u' => $this->notice->getUrl(),
  167. // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
  168. 't' => sprintf(_m('"%s"'), $this->notice->content),
  169. );
  170. return 'http://www.facebook.com/sharer.php?' .
  171. http_build_query($args, null, '&');
  172. }
  173. /**
  174. * Provide plugin version information.
  175. *
  176. * This data is used when showing the version page.
  177. *
  178. * @param array &$versions array of version data arrays; see EVENTS.txt
  179. *
  180. * @return boolean hook value
  181. */
  182. function onPluginVersion(array &$versions)
  183. {
  184. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ShareNotice';
  185. $versions[] = array('name' => 'ShareNotice',
  186. 'version' => self::PLUGIN_VERSION,
  187. 'author' => 'Brion Vibber',
  188. 'homepage' => $url,
  189. 'rawdescription' =>
  190. // TRANS: Plugin description.
  191. _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));
  192. return true;
  193. }
  194. }