]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ShareNotice/ShareNoticePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / ShareNotice / ShareNoticePlugin.php
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 /**
21  * @package ShareNoticePlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) { exit(1); }
26
27 class ShareNoticePlugin extends Plugin
28 {
29     public $targets = array(
30         array('Twitter'),
31         array('Facebook'),
32         array('StatusNet', array('baseurl' => 'http://identi.ca'))
33     );
34
35     public function onEndShowStylesheets(Action $action) {
36         $action->cssLink($this->path('css/sharenotice.css'));
37         return true;
38     }
39
40     function onStartShowNoticeItem(NoticeListItem $item)
41     {
42         $notice = $item->notice;
43         $out = $item->out;
44
45         $out->elementStart('ul', array('class' => 'notice-share'));
46         foreach ($this->targets as $data) {
47             $type = $data[0];
48             $args = (count($data) > 1) ? $data[1] : array();
49             $target = $this->getShareTarget($type, $notice, $args);
50             $this->showShareTarget($out, $target);
51         }
52         $out->elementEnd('ul');
53     }
54
55     private function getShareTarget($type, $notice, $args)
56     {
57         $class = ucfirst($type) . 'ShareTarget';
58
59         return new $class($notice, $args);
60     }
61
62     private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
63     {
64         $class = $target->getClass();
65         $text = $target->getText();
66         $url = $target->targetUrl();
67
68         $out->elementStart('li', array('class' => 'notice-share-' . $class));
69         $out->elementStart('a', array(
70             'href' => $url,
71             'title' => $text,
72             'target' => '_blank'
73         ));
74         $out->element('span', array(), $text);
75         $out->elementEnd('a');
76         $out->elementEnd('li');
77     }
78 }
79
80 abstract class NoticeShareTarget
81 {
82     protected $notice;
83
84     public function __construct($notice)
85     {
86         $this->notice = $notice;
87     }
88
89     public abstract function getClass();
90
91     public abstract function getText();
92
93     public abstract function targetUrl();
94 }
95
96 abstract class GenericNoticeShareTarget extends NoticeShareTarget
97 {
98     protected function maxLength()
99     {
100         return 140; // typical
101     }
102
103     protected function statusText()
104     {
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'));
110
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;
117         }
118
119         return sprintf($pattern, $content) . $suffix;
120     }
121 }
122
123 class TwitterShareTarget extends GenericNoticeShareTarget
124 {
125     public function getClass()
126     {
127         return 'twitter';
128     }
129
130     public function getText()
131     {
132         // TRANS: Tooltip for image to share a notice on Twitter.
133         return _m('Share on Twitter');
134     }
135
136     public function targetUrl()
137     {
138         $args = array(
139             'status' => $this->statusText()
140         );
141         return 'http://twitter.com/home?' .
142                 http_build_query($args, null, '&');
143     }
144 }
145
146 class StatusNetShareTarget extends GenericNoticeShareTarget
147 {
148     protected $baseurl;
149
150     public function __construct($notice, $args)
151     {
152         parent::__construct($notice);
153         $this->baseurl = $args['baseurl'];
154     }
155
156     public function getClass()
157     {
158         return 'statusnet';
159     }
160
161     public function getText()
162     {
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);
167     }
168
169     public function targetUrl()
170     {
171         $args = array(
172             'status_textarea' => $this->statusText()
173         );
174         return $this->baseurl . '/notice/new?' .
175                 http_build_query($args, null, '&');
176     }
177 }
178
179 class FacebookShareTarget extends NoticeShareTarget
180 {
181     public function getClass()
182     {
183         return 'facebook';
184     }
185
186     public function getText()
187     {
188         // TRANS: Tooltip for image to share a notice on Facebook.
189         return _m('Share on Facebook');
190     }
191
192     public function targetUrl()
193     {
194         $args = array(
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),
198         );
199         return 'http://www.facebook.com/sharer.php?' .
200             http_build_query($args, null, '&');
201     }
202
203     /**
204      * Provide plugin version information.
205      *
206      * This data is used when showing the version page.
207      *
208      * @param array &$versions array of version data arrays; see EVENTS.txt
209      *
210      * @return boolean hook value
211      */
212     function onPluginVersion(array &$versions)
213     {
214         $url = 'http://status.net/wiki/Plugin:ShareNotice';
215
216         $versions[] = array('name' => 'ShareNotice',
217             'version' => GNUSOCIAL_VERSION,
218             'author' => 'Brion Vibber',
219             'homepage' => $url,
220             'rawdescription' =>
221             // TRANS: Plugin description.
222             _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));
223
224         return true;
225     }
226 }