3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to add Disqus commenting to notice pages
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
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.
40 * To use this plugin, you need to first register your site with Disqus
41 * and get a Discus 'shortname' for it.
45 * To enable the plugin, put the following in you config.php:
49 * 'shortname' => 'YOURSHORTNAME',
50 * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;'
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.:
60 * 'shortname' => 'YOURSHORTNAME',
61 * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;',
62 * 'restricted' => true
66 * $ php userrole.php -s#### -nusername -rrichedit
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.
76 * See: http://help.disqus.com/entries/100878-css-customization
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/
87 class DisqusPlugin extends Plugin
89 public $shortname; // Required 'shortname' for actually triggering Disqus
90 public $divStyle; // Optional CSS chunk for the main <div>
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;
98 * Add a Disqus commenting section to the end of an individual
99 * notice page's content block
101 * @param Action $action The current action
103 function onEndShowContentBlock($action)
105 if (get_class($action) == 'ShownoticeAction') {
107 $profile = Profile::staticGet('id', $action->notice->profile_id);
109 if ($this->isAllowedRichEdit($profile)) {
112 $attrs['id'] = 'disqus_thread';
114 if ($this->divStyle) {
115 $attrs['style'] = $this->divStyle;
118 $action->element('div', $attrs, null);
120 $script = <<<ENDOFSCRIPT
121 var disqus_identifier = %d;
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);
129 $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
133 $attrs['id'] = 'disqus_thread_footer';
135 if ($this->divStyle) {
136 $attrs['style'] = $this->divStyle;
139 $action->elementStart('div', $attrs);
140 $action->elementStart('noscript');
142 $noScriptMsg = sprintf(_m("Please enable JavaScript to view the [comments powered by Disqus](http://disqus.com/?ref_noscript=%s)."), $this->shortname);
143 $output = common_markup_to_html($noScriptMsg);
144 $action->raw($output);
146 $action->elementEnd('noscript');
148 $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
149 $action->raw(_m('Comments powered by '));
150 $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
151 $action->elementEnd('a');
152 $action->elementEnd('div');
158 * Add Disqus comment count script to the end of the scripts section
160 * @param Action $action the current action
163 function onEndShowScripts($action)
166 $script = <<<ENDOFSCRIPT
167 var disqus_shortname = '%s';
169 var s = document.createElement('script'); s.async = true;
170 s.src = 'http://disqus.com/forums/%s/count.js';
171 (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
174 $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
179 * Tack on a Disqus comments link to the notice options stanza
180 * (the link displays the total number of comments for each notice)
182 * @param NoticeListItem $noticeListItem
185 function onEndShowNoticeInfo($noticeListItem)
187 // Don't enable commenting for remote notices
188 if (empty($noticeListItem->notice->is_local)) {
192 $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
194 if ($this->isAllowedRichEdit($profile)) {
195 $noticeUrl = $noticeListItem->notice->bestUrl();
196 $noticeUrl .= '#disqus_thread';
198 $noticeListItem->out->element(
200 array('href' => $noticeUrl, 'class' => 'disqus_count'),
207 * Does the current user have permission to use the Disqus plugin?
208 * Always true unless the plugin's "restricted" setting is on, in which
209 * case it's limited to users with the "richedit" role.
211 * @fixme make that more sanely configurable :)
213 * @param Profile $profile the profile to check
217 private function isAllowedRichEdit($profile)
219 if ($this->restricted) {
220 $user = User::staticGet($profile->id);
221 return !empty($user) && $user->hasRole('richedit');
230 * @param &$versions Array of current plugins
232 * @return boolean true
234 function onPluginVersion(&$versions)
236 $versions[] = array('name' => 'Disqus',
237 'version' => STATUSNET_VERSION,
238 'author' => 'Zach Copley',
239 'homepage' => 'http://status.net/wiki/Plugin:Disqus',
241 _m('Use <a href="http://disqus.com/">Disqus</a>'.
242 ' to add commenting to notice pages.'));