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