3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
21 * @package ShareNoticePlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET')) { exit(1); }
27 class ShareNoticePlugin extends Plugin
29 const PLUGIN_VERSION = '2.0.0';
31 public $targets = array(
34 array('StatusNet', array('baseurl' => 'http://identi.ca'))
37 public function onEndShowStylesheets(Action $action) {
38 $action->cssLink($this->path('css/sharenotice.css'));
42 function onStartShowNoticeItem($item)
44 $notice = $item->notice;
47 $out->elementStart('ul', array('class' => 'notice-share'));
48 foreach ($this->targets as $data) {
50 $args = (count($data) > 1) ? $data[1] : array();
51 $target = $this->getShareTarget($type, $notice, $args);
52 $this->showShareTarget($out, $target);
54 $out->elementEnd('ul');
57 private function getShareTarget($type, $notice, $args)
59 $class = ucfirst($type) . 'ShareTarget';
61 return new $class($notice, $args);
64 private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
66 $class = $target->getClass();
67 $text = $target->getText();
68 $url = $target->targetUrl();
70 $out->elementStart('li', array('class' => 'notice-share-' . $class));
71 $out->elementStart('a', array(
76 $out->element('span', array(), $text);
77 $out->elementEnd('a');
78 $out->elementEnd('li');
82 abstract class NoticeShareTarget
86 public function __construct($notice)
88 $this->notice = $notice;
91 public abstract function getClass();
93 public abstract function getText();
95 public abstract function targetUrl();
98 abstract class GenericNoticeShareTarget extends NoticeShareTarget
100 protected function maxLength()
102 return 140; // typical
105 protected function statusText()
107 // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
108 $pattern = _m('"%s"');
109 $url = $this->notice->getUrl();
110 $suffix = ' ' . $url;
111 $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
113 $content = $this->notice->content;
114 // TRANS: Truncation symbol.
115 $truncation_symbol = _m('…');
116 $truncation_symbol_length = mb_strlen($truncation_symbol);
117 if (mb_strlen($content) > $room) {
118 $content = mb_substr($content, 0, $room - $truncation_symbol_length) . $truncation_symbol;
121 return sprintf($pattern, $content) . $suffix;
125 class TwitterShareTarget extends GenericNoticeShareTarget
127 public function getClass()
132 public function getText()
134 // TRANS: Tooltip for image to share a notice on Twitter.
135 return _m('Share on Twitter');
138 public function targetUrl()
141 'status' => $this->statusText()
143 return 'http://twitter.com/home?' .
144 http_build_query($args, null, '&');
148 class StatusNetShareTarget extends GenericNoticeShareTarget
152 public function __construct($notice, $args)
154 parent::__construct($notice);
155 $this->baseurl = $args['baseurl'];
158 public function getClass()
163 public function getText()
165 $host = parse_url($this->baseurl, PHP_URL_HOST);
166 // TRANS: Tooltip for image to share a notice on another platform (other than Twitter or Facebook).
167 // TRANS: %s is a host name.
168 return sprintf(_m('Share on %s'), $host);
171 public function targetUrl()
174 'status_textarea' => $this->statusText()
176 return $this->baseurl . '/notice/new?' .
177 http_build_query($args, null, '&');
181 class FacebookShareTarget extends NoticeShareTarget
183 public function getClass()
188 public function getText()
190 // TRANS: Tooltip for image to share a notice on Facebook.
191 return _m('Share on Facebook');
194 public function targetUrl()
197 'u' => $this->notice->getUrl(),
198 // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
199 't' => sprintf(_m('"%s"'), $this->notice->content),
201 return 'http://www.facebook.com/sharer.php?' .
202 http_build_query($args, null, '&');
206 * Provide plugin version information.
208 * This data is used when showing the version page.
210 * @param array &$versions array of version data arrays; see EVENTS.txt
212 * @return boolean hook value
214 function onPluginVersion(array &$versions)
216 $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ShareNotice';
218 $versions[] = array('name' => 'ShareNotice',
219 'version' => self::PLUGIN_VERSION,
220 'author' => 'Brion Vibber',
223 // TRANS: Plugin description.
224 _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));