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 // 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);
147 $action->elementEnd('noscript');
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');
160 * Add Disqus comment count script to the end of the scripts section
162 * @param Action $action the current action
165 function onEndShowScripts($action)
168 $script = <<<ENDOFSCRIPT
169 var disqus_shortname = '%s';
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);
176 $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
181 * Tack on a Disqus comments link to the notice options stanza
182 * (the link displays the total number of comments for each notice)
184 * @param NoticeListItem $noticeListItem
187 function onEndShowNoticeInfo($noticeListItem)
189 // Don't enable commenting for remote notices
190 if (empty($noticeListItem->notice->is_local)) {
194 $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
196 if ($this->isAllowedRichEdit($profile)) {
197 $noticeUrl = $noticeListItem->notice->bestUrl();
198 $noticeUrl .= '#disqus_thread';
200 $noticeListItem->out->element(
202 array('href' => $noticeUrl, 'class' => 'disqus_count'),
203 // TRANS: Plugin supplied feature for Disqus comments to notices.
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.
214 * @fixme make that more sanely configurable :)
216 * @param Profile $profile the profile to check
220 private function isAllowedRichEdit($profile)
222 if ($this->restricted) {
223 $user = User::staticGet($profile->id);
224 return !empty($user) && $user->hasRole('richedit');
233 * @param &$versions Array of current plugins
235 * @return boolean true
237 function onPluginVersion(&$versions)
239 $versions[] = array('name' => 'Disqus',
240 'version' => STATUSNET_VERSION,
241 'author' => 'Zach Copley',
242 'homepage' => 'http://status.net/wiki/Plugin:Disqus',
244 // TRANS: Plugin description.
245 _m('Use <a href="http://disqus.com/">Disqus</a>'.
246 ' to add commenting to notice pages.'));