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 function onEndShowStatusNetStyles($action) {
36 $action->cssLink('plugins/ShareNotice/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: Leave this message unchanged.
106 $pattern = _m('"%s"');
107 $url = $this->notice->bestUrl();
108 $suffix = ' ' . $url;
109 $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
111 $content = $this->notice->content;
112 if (mb_strlen($content) > $room) {
113 $content = mb_substr($content, 0, $room - 1) . '…';
116 return sprintf($pattern, $content) . $suffix;
120 class TwitterShareTarget extends GenericNoticeShareTarget
122 public function getClass()
127 public function getText()
129 // TRANS: Tooltip for image to share a notice on Twitter.
130 return _m('Share on Twitter');
133 public function targetUrl()
136 'status' => $this->statusText()
138 return 'http://twitter.com/home?' .
139 http_build_query($args, null, '&');
143 class StatusNetShareTarget extends GenericNoticeShareTarget
147 public function __construct($notice, $args)
149 parent::__construct($notice);
150 $this->baseurl = $args['baseurl'];
153 public function getClass()
158 public function getText()
160 $host = parse_url($this->baseurl, PHP_URL_HOST);
161 // TRANS: Tooltip for image to share a notice on another platform (other than Twitter or Facebook).
162 // TRANS: %s is a host name.
163 return sprintf(_m('Share on %s'), $host);
166 public function targetUrl()
169 'status_textarea' => $this->statusText()
171 return $this->baseurl . '/notice/new?' .
172 http_build_query($args, null, '&');
176 class FacebookShareTarget extends NoticeShareTarget
178 public function getClass()
183 public function getText()
185 // TRANS: Tooltip for image to share a notice on Facebook.
186 return _m('Share on Facebook');
189 public function targetUrl()
192 'u' => $this->notice->bestUrl(),
193 // TRANS: %s is notice content that is shared on Twitter, Facebook or another platform.
194 't' => sprintf(_m('"%s"'), $this->notice->content),
196 return 'http://www.facebook.com/sharer.php?' .
197 http_build_query($args, null, '&');
201 * Provide plugin version information.
203 * This data is used when showing the version page.
205 * @param array &$versions array of version data arrays; see EVENTS.txt
207 * @return boolean hook value
209 function onPluginVersion(&$versions)
211 $url = 'http://status.net/wiki/Plugin:ShareNotice';
213 $versions[] = array('name' => 'ShareNotice',
214 'version' => STATUSNET_VERSION,
215 'author' => 'Brion Vibber',
218 // TRANS: Plugin description.
219 _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));