]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DisqusPlugin.php
dc56f320c3e00baff813f2704ca9e82afce197df
[quix0rs-gnu-social.git] / plugins / 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  *      'div_style' => 'width:675px; padding-top:10px; position:relative; float:left;'
51  *   )
52  * );
53  *
54  * NOTE: the 'div_style' in an optional parameter that passes in some
55  * inline CSS when creating the Disqus widget. It's a shortcut to make
56  * the widget look OK with the default StatusNet theme. If you leave
57  * it out you'll have to edit your theme CSS files to make the widget
58  * look good.  You can also control the way the widget looks by
59  * adding style rules to your theme.
60  *
61  * See: http://help.disqus.com/entries/100878-css-customization
62  *
63  * @category Plugin
64  * @package  StatusNet
65  * @author   Zach Copley <zach@status.net>
66  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
67  * @link     http://status.net/
68  *
69  * @see      Event
70  */
71
72 class DisqusPlugin extends Plugin
73 {
74     function onEndShowContentBlock($action)
75     {
76         if (get_class($action) == 'ShownoticeAction') {
77
78             $attrs = array();
79             $attrs['id'] = 'disqus_thread';
80
81             if ($this->div_style) {
82                 $attrs['style'] = $this->div_style;
83             }
84
85             $action->element('div', $attrs, null);
86
87             $script = <<<ENDOFSCRIPT
88 var disqus_identifier = %d;
89   (function() {
90    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
91    dsq.src = 'http://%s.disqus.com/embed.js';
92    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
93   })();
94 ENDOFSCRIPT;
95
96             $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
97
98             $attrs = array();
99
100             $attrs['id'] = 'disqus_thread_footer';
101
102             if ($this->div_style) {
103                 $attrs['style'] = $this->div_style;
104             }
105
106             $action->elementStart('div', $attrs);
107             $action->elementStart('noscript');
108
109             $action->raw('Please enable JavaScript to view the ');
110             $noscriptUrl = 'http://disqus.com/?ref_noscript=' . $this->shortname;
111             $action->element('a', array('href' => $noscriptUrl), 'comments powered by Disqus.');
112             $action->elementEnd('noscript');
113
114             $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
115             $action->raw('blog comments powered by ');
116             $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
117             $action->elementEnd('a');
118             $action->elementEnd('div');
119         }
120     }
121
122     function onEndShowScripts($action)
123     {
124         // fugly
125         $script = <<<ENDOFSCRIPT
126 var disqus_shortname = '%s';
127 (function () {
128   var s = document.createElement('script'); s.async = true;
129   s.src = 'http://disqus.com/forums/%s/count.js';
130   (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
131 }());
132 ENDOFSCRIPT;
133         $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
134
135         return true;
136     }
137
138     function onStartShowNoticeItem($noticeListItem)
139     {
140         if (empty($noticeListItem->notice->is_local)) {
141             return true;
142         }
143
144         $noticeListItem->showNotice();
145         $noticeListItem->showNoticeInfo();
146
147         $noticeUrl = $noticeListItem->notice->bestUrl();
148         $noticeUrl .= '#disqus_thread';
149
150         $noticeListItem->out->element(
151             'a', array('href' => $noticeUrl, 'class' => 'disqus_count', 'Comments')
152         );
153
154         $noticeListItem->showNoticeOptions();
155         Event::handle('EndShowNoticeItem', array($noticeListItem));
156
157         return false;
158     }
159
160     function onPluginVersion(&$versions)
161     {
162         $versions[] = array('name' => 'Disqus',
163                             'version' => STATUSNET_VERSION,
164                             'author' => 'Zach Copley',
165                             'homepage' => 'http://status.net/wiki/Plugin:Disqus',
166                             'rawdescription' =>
167                             _m('Use <a href="http://disqus.com/">Disqus</a>'.
168                                ' to add commenting to notice pages.'));
169         return true;
170     }
171 }