]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Disqus/DisqusPlugin.php
ec5857dd7453945c90064cd01689cb7e48ec45dc
[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  *         'divStyle'  => 'width:675px; padding-top:10px; position:relative; float:left;'
51  *     )
52  * );
53  *
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.:
56  *
57  * addPlugin(
58  *     'Disqus', array(
59  *         'shortname' => 'YOURSHORTNAME',
60  *         'divStyle'  => 'width:675px; padding-top:10px; position:relative; float:left;',
61  *         'nicknames' => array('spock', 'kirk', 'bones')
62  *     )
63  * );
64  *
65  *
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.
72  *
73  * See: http://help.disqus.com/entries/100878-css-customization
74  *
75  *
76  * @category Plugin
77  * @package  StatusNet
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/
81  *
82  * @see      Event
83  */
84 class DisqusPlugin extends Plugin
85 {
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)
89
90     /**
91      * Add a Disqus commenting section to the end of an individual
92      * notice page's content block
93      *
94      * @param Action $action The current action
95      */
96     function onEndShowContentBlock($action)
97     {
98         if (get_class($action) == 'ShownoticeAction') {
99
100             $profile = Profile::staticGet('id', $action->notice->profile_id);
101
102             if ($this->hasCommenting($profile)) {
103
104                 $attrs = array();
105                 $attrs['id'] = 'disqus_thread';
106
107                 if ($this->divStyle) {
108                     $attrs['style'] = $this->divStyle;
109                 }
110
111                 $action->element('div', $attrs, null);
112
113                 $script = <<<ENDOFSCRIPT
114     var disqus_identifier = %d;
115       (function() {
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);
119       })();
120 ENDOFSCRIPT;
121
122                 $action->inlineScript(sprintf($script, $action->notice->id, $this->shortname));
123
124                 $attrs = array();
125
126                 $attrs['id'] = 'disqus_thread_footer';
127
128                 if ($this->divStyle) {
129                     $attrs['style'] = $this->divStyle;
130                 }
131
132                 $action->elementStart('div', $attrs);
133                 $action->elementStart('noscript');
134
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);
138
139                 $action->elementEnd('noscript');
140
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');
146             }
147         }
148     }
149
150     /**
151      * Add Disqus comment count script to the end of the scripts section
152      *
153      * @param Action $action the current action
154      *
155      */
156     function onEndShowScripts($action)
157     {
158         // fugly
159         $script = <<<ENDOFSCRIPT
160 var disqus_shortname = '%s';
161 (function () {
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);
165 }());
166 ENDOFSCRIPT;
167         $action->inlineScript(sprintf($script, $this->shortname, $this->shortname));
168
169     }
170
171     /**
172      * Override the default Notice display to add Disqus comments link
173      * (the link displays the total number of comments for each notice)
174      *
175      * @param NoticeListItem $noticeListItem
176      *
177      * @return boolean override
178      */
179     function onStartShowNoticeItem($noticeListItem)
180     {
181         // Don't enable commenting for remote notices
182         if (empty($noticeListItem->notice->is_local)) {
183             return true;
184         }
185
186         $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
187
188         if ($this->hasCommenting($profile)) {
189
190             // @todo Refactor individual notice display to have it's own event hooks
191
192             $noticeListItem->showNotice();
193             $noticeListItem->showNoticeInfo();
194
195             $noticeUrl = $noticeListItem->notice->bestUrl();
196             $noticeUrl .= '#disqus_thread';
197
198             $noticeListItem->out->element(
199                 'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments'
200             );
201
202             $noticeListItem->showNoticeOptions();
203             Event::handle('EndShowNoticeItem', array($noticeListItem));
204
205             return false;
206         } else {
207             return true;
208         }
209     }
210
211     /**
212      * Helper to check whether commenting should be enabled
213      * for a given notice
214      *
215      * Assumes commenting should be enabled, unless the
216      * nicknames array is populated
217      *
218      * @param Profile $profile the profile to check
219      *
220      * @return boolean true if yes
221      */
222     private function hasCommenting($profile)
223     {
224         if (!empty($this->nicknames)) {
225             foreach ($this->nicknames as $nickname) {
226                 if ($profile->nickname == $nickname) {
227                     return true;
228                 }
229             }
230             return false;
231         }
232
233         return true;
234     }
235
236     /**
237      * Plugin details
238      *
239      * @param &$versions Array of current plugins
240      *
241      * @return boolean true
242      */
243     function onPluginVersion(&$versions)
244     {
245         $versions[] = array('name' => 'Disqus',
246                             'version' => STATUSNET_VERSION,
247                             'author' => 'Zach Copley',
248                             'homepage' => 'http://status.net/wiki/Plugin:Disqus',
249                             'rawdescription' =>
250                             _m('Use <a href="http://disqus.com/">Disqus</a>'.
251                                ' to add commenting to notice pages.'));
252         return true;
253     }
254 }