]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Disqus/DisqusPlugin.php
Merge branch 'master' into 0.9.x
[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  *      '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 class DisqusPlugin extends Plugin
72 {
73     public $shortname; // Required 'shortname' for actually triggering Disqus.
74     public $div_style; // Optional CSS chunk for the main <div>
75
76     function onEndShowContentBlock($action)
77     {
78         if (get_class($action) == 'ShownoticeAction') {
79
80             $attrs = array();
81             $attrs['id'] = 'disqus_thread';
82
83             if ($this->div_style) {
84                 $attrs['style'] = $this->div_style;
85             }
86
87             $action->element('div', $attrs, null);
88
89             $script = <<<ENDOFSCRIPT
90 var disqus_identifier = %d;
91   (function() {
92    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
93    dsq.src = 'http://%s.disqus.com/embed.js';
94    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
95   })();
96 ENDOFSCRIPT;
97
98             $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
99
100             $attrs = array();
101
102             $attrs['id'] = 'disqus_thread_footer';
103
104             if ($this->div_style) {
105                 $attrs['style'] = $this->div_style;
106             }
107
108             $action->elementStart('div', $attrs);
109             $action->elementStart('noscript');
110
111                         // @todo FIXME: No i18n yet, because of bad implementation. Should be one string.
112             $action->raw('Please enable JavaScript to view the ');
113             $noscriptUrl = 'http://disqus.com/?ref_noscript=' . $this->shortname;
114             $action->element('a', array('href' => $noscriptUrl), 'comments powered by Disqus.');
115             $action->elementEnd('noscript');
116
117             $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
118             $action->raw('blog comments powered by ');
119             $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
120             $action->elementEnd('a');
121             $action->elementEnd('div');
122         }
123     }
124
125     function onEndShowScripts($action)
126     {
127         // fugly
128         $script = <<<ENDOFSCRIPT
129 var disqus_shortname = '%s';
130 (function () {
131   var s = document.createElement('script'); s.async = true;
132   s.src = 'http://disqus.com/forums/%s/count.js';
133   (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
134 }());
135 ENDOFSCRIPT;
136         $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
137
138         return true;
139     }
140
141     function onStartShowNoticeItem($noticeListItem)
142     {
143         if (empty($noticeListItem->notice->is_local)) {
144             return true;
145         }
146
147         $noticeListItem->showNotice();
148         $noticeListItem->showNoticeInfo();
149
150         $noticeUrl = $noticeListItem->notice->bestUrl();
151         $noticeUrl .= '#disqus_thread';
152
153         $noticeListItem->out->element(
154             'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments'
155         );
156
157         $noticeListItem->showNoticeOptions();
158         Event::handle('EndShowNoticeItem', array($noticeListItem));
159
160         return false;
161     }
162
163     function onPluginVersion(&$versions)
164     {
165         $versions[] = array('name' => 'Disqus',
166                             'version' => STATUSNET_VERSION,
167                             'author' => 'Zach Copley',
168                             'homepage' => 'http://status.net/wiki/Plugin:Disqus',
169                             'rawdescription' =>
170                             _m('Use <a href="http://disqus.com/">Disqus</a>'.
171                                ' to add commenting to notice pages.'));
172         return true;
173     }
174 }