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 user's notices, use the "nicknames" array, e.g.:
59 * 'shortname' => 'YOURSHORTNAME',
60 * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;',
61 * 'nicknames' => array('spock', 'kirk', 'bones')
66 * NOTE: the 'divStyle' in an optional parameter that passes in some
67 * inline CSS when creating the Disqus widget. It's a shortcut to make
68 * the widget look OK with the default StatusNet theme. If you leave
69 * it out you'll have to edit your theme CSS files to make the widget
70 * look good. You can also control the way the widget looks by
71 * adding style rules to your theme.
73 * See: http://help.disqus.com/entries/100878-css-customization
78 * @author Zach Copley <zach@status.net>
79 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
80 * @link http://status.net/
84 class DisqusPlugin extends Plugin
86 public $shortname; // Required 'shortname' for actually triggering Disqus
87 public $divStyle; // Optional CSS chunk for the main <div>
88 public $nicknames; // Optional array of nicks to restrict commenting to (default on for all users)
91 * Add a Disqus commenting section to the end of an individual
92 * notice page's content block
94 * @param Action $action The current action
96 function onEndShowContentBlock($action)
98 if (get_class($action) == 'ShownoticeAction') {
100 $profile = Profile::staticGet('id', $action->notice->profile_id);
102 if ($this->hasCommenting($profile)) {
105 $attrs['id'] = 'disqus_thread';
107 if ($this->divStyle) {
108 $attrs['style'] = $this->divStyle;
111 $action->element('div', $attrs, null);
113 $script = <<<ENDOFSCRIPT
114 var disqus_identifier = %d;
116 var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
117 dsq.src = 'http://%s.disqus.com/embed.js';
118 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
122 $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
126 $attrs['id'] = 'disqus_thread_footer';
128 if ($this->divStyle) {
129 $attrs['style'] = $this->divStyle;
132 $action->elementStart('div', $attrs);
133 $action->elementStart('noscript');
135 $noScriptMsg = sprintf(_m("Please enable JavaScript to view the [comments powered by Disqus](http://disqus.com/?ref_noscript=%s)."), $this->shortname);
136 $output = common_markup_to_html($noScriptMsg);
137 $action->raw($output);
139 $action->elementEnd('noscript');
141 $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
142 $action->raw(_m('Comments powered by '));
143 $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
144 $action->elementEnd('a');
145 $action->elementEnd('div');
151 * Add Disqus comment count script to the end of the scripts section
153 * @param Action $action the current action
156 function onEndShowScripts($action)
159 $script = <<<ENDOFSCRIPT
160 var disqus_shortname = '%s';
162 var s = document.createElement('script'); s.async = true;
163 s.src = 'http://disqus.com/forums/%s/count.js';
164 (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
167 $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
172 * Override the default Notice display to add Disqus comments link
173 * (the link displays the total number of comments for each notice)
175 * @param NoticeListItem $noticeListItem
177 * @return boolean override
179 function onStartShowNoticeItem($noticeListItem)
181 // Don't enable commenting for remote notices
182 if (empty($noticeListItem->notice->is_local)) {
186 $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
188 if ($this->hasCommenting($profile)) {
190 // @todo Refactor individual notice display to have it's own event hooks
192 $noticeListItem->showNotice();
193 $noticeListItem->showNoticeInfo();
195 $noticeUrl = $noticeListItem->notice->bestUrl();
196 $noticeUrl .= '#disqus_thread';
198 $noticeListItem->out->element(
199 'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments'
202 $noticeListItem->showNoticeOptions();
203 Event::handle('EndShowNoticeItem', array($noticeListItem));
212 * Helper to check whether commenting should be enabled
215 * Assumes commenting should be enabled, unless the
216 * nicknames array is populated
218 * @param Profile $profile the profile to check
220 * @return boolean true if yes
222 private function hasCommenting($profile)
224 if (!empty($this->nicknames)) {
225 foreach ($this->nicknames as $nickname) {
226 if ($profile->nickname == $nickname) {
239 * @param &$versions Array of current plugins
241 * @return boolean true
243 function onPluginVersion(&$versions)
245 $versions[] = array('name' => 'Disqus',
246 'version' => STATUSNET_VERSION,
247 'author' => 'Zach Copley',
248 'homepage' => 'http://status.net/wiki/Plugin:Disqus',
250 _m('Use <a href="http://disqus.com/">Disqus</a>'.
251 ' to add commenting to notice pages.'));