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 public $targets = array(
32 array('StatusNet', array('baseurl' => 'http://identi.ca'))
35 public function onEndShowStylesheets(Action $action) {
36 $action->cssLink($this->path('css/sharenotice.css'));
40 function onStartShowNoticeItem($item)
42 $notice = $item->notice;
45 $out->elementStart('ul', array('class' => 'notice-share'));
46 foreach ($this->targets as $data) {
48 $args = (count($data) > 1) ? $data[1] : array();
49 $target = $this->getShareTarget($type, $notice, $args);
50 $this->showShareTarget($out, $target);
52 $out->elementEnd('ul');
55 private function getShareTarget($type, $notice, $args)
57 $class = ucfirst($type) . 'ShareTarget';
59 return new $class($notice, $args);
62 private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
64 $class = $target->getClass();
65 $text = $target->getText();
66 $url = $target->targetUrl();
68 $out->elementStart('li', array('class' => 'notice-share-' . $class));
69 $out->elementStart('a', array(
74 $out->element('span', array(), $text);
75 $out->elementEnd('a');
76 $out->elementEnd('li');
80 abstract class NoticeShareTarget
84 public function __construct($notice)
86 $this->notice = $notice;
89 public abstract function getClass();
91 public abstract function getText();
93 public abstract function targetUrl();
96 abstract class GenericNoticeShareTarget extends NoticeShareTarget
98 protected function maxLength()
100 return 140; // typical
103 protected function statusText()
105 // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
106 $pattern = _m('"%s"');
107 $url = $this->notice->getUrl();
108 $suffix = ' ' . $url;
109 $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
111 $content = $this->notice->content;
112 // TRANS: Truncation symbol.
113 $truncation_symbol = _m('…');
114 $truncation_symbol_length = mb_strlen($truncation_symbol);
115 if (mb_strlen($content) > $room) {
116 $content = mb_substr($content, 0, $room - $truncation_symbol_length) . $truncation_symbol;
119 return sprintf($pattern, $content) . $suffix;
123 class TwitterShareTarget extends GenericNoticeShareTarget
125 public function getClass()
130 public function getText()
132 // TRANS: Tooltip for image to share a notice on Twitter.
133 return _m('Share on Twitter');
136 public function targetUrl()
139 'status' => $this->statusText()
141 return 'http://twitter.com/home?' .
142 http_build_query($args, null, '&');
146 class StatusNetShareTarget extends GenericNoticeShareTarget
150 public function __construct($notice, $args)
152 parent::__construct($notice);
153 $this->baseurl = $args['baseurl'];
156 public function getClass()
161 public function getText()
163 $host = parse_url($this->baseurl, PHP_URL_HOST);
164 // TRANS: Tooltip for image to share a notice on another platform (other than Twitter or Facebook).
165 // TRANS: %s is a host name.
166 return sprintf(_m('Share on %s'), $host);
169 public function targetUrl()
172 'status_textarea' => $this->statusText()
174 return $this->baseurl . '/notice/new?' .
175 http_build_query($args, null, '&');
179 class FacebookShareTarget extends NoticeShareTarget
181 public function getClass()
186 public function getText()
188 // TRANS: Tooltip for image to share a notice on Facebook.
189 return _m('Share on Facebook');
192 public function targetUrl()
195 'u' => $this->notice->getUrl(),
196 // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
197 't' => sprintf(_m('"%s"'), $this->notice->content),
199 return 'http://www.facebook.com/sharer.php?' .
200 http_build_query($args, null, '&');
204 * Provide plugin version information.
206 * This data is used when showing the version page.
208 * @param array &$versions array of version data arrays; see EVENTS.txt
210 * @return boolean hook value
212 function onPluginVersion(array &$versions)
214 $url = 'http://status.net/wiki/Plugin:ShareNotice';
216 $versions[] = array('name' => 'ShareNotice',
217 'version' => GNUSOCIAL_VERSION,
218 'author' => 'Brion Vibber',
221 // TRANS: Plugin description.
222 _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));