]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Disqus/DisqusPlugin.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / Disqus / DisqusPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to add Disqus commenting to notice pages
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  *
36  * This plugin adds Disqus commenting to your notices. Enabling this
37  * plugin will make each notice page display the Disqus widget, and
38  * notice lists will display the number of commments each notice has.
39  *
40  * To use this plugin, you need to first register your site with Disqus
41  * and get a Discus 'shortname' for it.
42  *
43  *    http://disqus.com
44  *
45  * To enable the plugin, put the following in you config.php:
46  *
47  * addPlugin(
48  *     'Disqus', array(
49  *         'shortname' => 'YOURSHORTNAME',
50  *         'divStyle'  => 'width:675px; padding-top:10px; position:relative; float:left;'
51  *     )
52  * );
53  *
54  * If you only want to allow commenting on a specific user's notices or
55  * a specific set of users' notices initialize the plugin with the "restricted"
56  * parameter and grant the "richedit" role to those users. E.g.:
57  *
58  * addPlugin(
59  *     'Disqus', array(
60  *         'shortname'  => 'YOURSHORTNAME',
61  *         'divStyle'   => 'width:675px; padding-top:10px; position:relative; float:left;',
62  *         'restricted' => true
63  *     )
64  * );
65  *
66  * $ php userrole.php -s#### -nusername -rrichedit
67  *
68  *
69  * NOTE: the 'divStyle' in an optional parameter that passes in some
70  * inline CSS when creating the Disqus widget. It's a shortcut to make
71  * the widget look OK with the default StatusNet theme. If you leave
72  * it out you'll have to edit your theme CSS files to make the widget
73  * look good.  You can also control the way the widget looks by
74  * adding style rules to your theme.
75  *
76  * See: http://help.disqus.com/entries/100878-css-customization
77  *
78  *
79  * @category Plugin
80  * @package  StatusNet
81  * @author   Zach Copley <zach@status.net>
82  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
83  * @link     http://status.net/
84  *
85  * @see      Event
86  */
87 class DisqusPlugin extends Plugin
88 {
89     public $shortname; // Required 'shortname' for actually triggering Disqus
90     public $divStyle;  // Optional CSS chunk for the main <div>
91
92     // By default, Disqus commenting will be available to all users.
93     // With restricted on, only users who have been granted the
94     // "richedit" role get it.
95     public $restricted = false;
96
97     /**
98      * Add a Disqus commenting section to the end of an individual
99      * notice page's content block
100      *
101      * @param Action $action The current action
102      */
103     function onEndShowContentBlock($action)
104     {
105         if (get_class($action) == 'ShownoticeAction') {
106
107             $profile = Profile::staticGet('id', $action->notice->profile_id);
108
109             if ($this->isAllowedRichEdit($profile)) {
110
111                 $attrs = array();
112                 $attrs['id'] = 'disqus_thread';
113
114                 if ($this->divStyle) {
115                     $attrs['style'] = $this->divStyle;
116                 }
117
118                 $action->element('div', $attrs, null);
119
120                 $script = <<<ENDOFSCRIPT
121     var disqus_identifier = %d;
122       (function() {
123        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
124        dsq.src = 'http://%s.disqus.com/embed.js';
125        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
126       })();
127 ENDOFSCRIPT;
128
129                 $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
130
131                 $attrs = array();
132
133                 $attrs['id'] = 'disqus_thread_footer';
134
135                 if ($this->divStyle) {
136                     $attrs['style'] = $this->divStyle;
137                 }
138
139                 $action->elementStart('div', $attrs);
140                 $action->elementStart('noscript');
141
142                 // TRANS: User notification that JavaScript is required for Disqus comment display.
143                 $noScriptMsg = sprintf(_m("Please enable JavaScript to view the [comments powered by Disqus](http://disqus.com/?ref_noscript=%s)."), $this->shortname);
144                 $output = common_markup_to_html($noScriptMsg);
145                 $action->raw($output);
146
147                 $action->elementEnd('noscript');
148
149                 $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
150                 // TRANS: This message is followed by a Disqus logo. Alt text is "Disqus".
151                 $action->raw(_m('Comments powered by '));
152                 $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
153                 $action->elementEnd('a');
154                 $action->elementEnd('div');
155             }
156         }
157     }
158
159     /**
160      * Add Disqus comment count script to the end of the scripts section
161      *
162      * @param Action $action the current action
163      *
164      */
165     function onEndShowScripts($action)
166     {
167         // fugly
168         $script = <<<ENDOFSCRIPT
169 var disqus_shortname = '%s';
170 (function () {
171   var s = document.createElement('script'); s.async = true;
172   s.src = 'http://disqus.com/forums/%s/count.js';
173   (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
174 }());
175 ENDOFSCRIPT;
176         $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
177
178     }
179
180     /**
181      * Tack on a Disqus comments link to the notice options stanza
182      * (the link displays the total number of comments for each notice)
183      *
184      * @param NoticeListItem $noticeListItem
185      *
186      */
187     function onEndShowNoticeInfo($noticeListItem)
188     {
189         // Don't enable commenting for remote notices
190         if (empty($noticeListItem->notice->is_local)) {
191             return;
192         }
193
194         $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
195
196         if ($this->isAllowedRichEdit($profile)) {
197             $noticeUrl = $noticeListItem->notice->bestUrl();
198             $noticeUrl .= '#disqus_thread';
199
200             $noticeListItem->out->element(
201                 'a',
202                 array('href' => $noticeUrl, 'class' => 'disqus_count'),
203                 // TRANS: Plugin supplied feature for Disqus comments to notices.
204                 _m('Comments')
205             );
206         }
207     }
208
209     /**
210      * Does the current user have permission to use the Disqus plugin?
211      * Always true unless the plugin's "restricted" setting is on, in which
212      * case it's limited to users with the "richedit" role.
213      *
214      * @fixme make that more sanely configurable :)
215      *
216      * @param Profile $profile the profile to check
217      *
218      * @return boolean
219      */
220     private function isAllowedRichEdit($profile)
221     {
222         if ($this->restricted) {
223             $user = User::staticGet($profile->id);
224             return !empty($user) && $user->hasRole('richedit');
225         } else {
226             return true;
227         }
228     }
229
230     /**
231      * Plugin details
232      *
233      * @param &$versions Array of current plugins
234      *
235      * @return boolean true
236      */
237     function onPluginVersion(&$versions)
238     {
239         $versions[] = array('name' => 'Disqus',
240                             'version' => STATUSNET_VERSION,
241                             'author' => 'Zach Copley',
242                             'homepage' => 'http://status.net/wiki/Plugin:Disqus',
243                             'rawdescription' =>
244                             // TRANS: Plugin description.
245                             _m('Use <a href="http://disqus.com/">Disqus</a>'.
246                                ' to add commenting to notice pages.'));
247         return true;
248     }
249 }