]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ShareNotice/ShareNoticePlugin.php
Merge remote branch 'origin/pluginstatic' into testing
[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     function onEndShowStatusNetStyles($action) {
36         $action->cssLink($this->path('css/sharenotice.css'));
37         return true;
38     }
39
40     function onStartShowNoticeItem($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: 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'));
110
111         $content = $this->notice->content;
112         if (mb_strlen($content) > $room) {
113             $content = mb_substr($content, 0, $room - 1) . '…';
114         }
115
116         return sprintf($pattern, $content) . $suffix;
117     }
118 }
119
120 class TwitterShareTarget extends GenericNoticeShareTarget
121 {
122     public function getClass()
123     {
124         return 'twitter';
125     }
126
127     public function getText()
128     {
129         // TRANS: Tooltip for image to share a notice on Twitter.
130         return _m('Share on Twitter');
131     }
132
133     public function targetUrl()
134     {
135         $args = array(
136             'status' => $this->statusText()
137         );
138         return 'http://twitter.com/home?' .
139                 http_build_query($args, null, '&');
140     }
141 }
142
143 class StatusNetShareTarget extends GenericNoticeShareTarget
144 {
145     protected $baseurl;
146
147     public function __construct($notice, $args)
148     {
149         parent::__construct($notice);
150         $this->baseurl = $args['baseurl'];
151     }
152
153     public function getClass()
154     {
155         return 'statusnet';
156     }
157
158     public function getText()
159     {
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);
164     }
165
166     public function targetUrl()
167     {
168         $args = array(
169             'status_textarea' => $this->statusText()
170         );
171         return $this->baseurl . '/notice/new?' .
172                 http_build_query($args, null, '&');
173     }
174 }
175
176 class FacebookShareTarget extends NoticeShareTarget
177 {
178     public function getClass()
179     {
180         return 'facebook';
181     }
182
183     public function getText()
184     {
185         // TRANS: Tooltip for image to share a notice on Facebook.
186         return _m('Share on Facebook');
187     }
188
189     public function targetUrl()
190     {
191         $args = array(
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),
195         );
196         return 'http://www.facebook.com/sharer.php?' .
197             http_build_query($args, null, '&');
198     }
199
200     /**
201      * Provide plugin version information.
202      *
203      * This data is used when showing the version page.
204      *
205      * @param array &$versions array of version data arrays; see EVENTS.txt
206      *
207      * @return boolean hook value
208      */
209     function onPluginVersion(&$versions)
210     {
211         $url = 'http://status.net/wiki/Plugin:ShareNotice';
212
213         $versions[] = array('name' => 'ShareNotice',
214             'version' => STATUSNET_VERSION,
215             'author' => 'Brion Vibber',
216             'homepage' => $url,
217             'rawdescription' =>
218             // TRANS: Plugin description.
219             _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));
220
221         return true;
222     }
223 }