forked from atdrupal/disqus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisqus.module
483 lines (437 loc) · 16.6 KB
/
disqus.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/**
* @file
* The Disqus Drupal module.
*/
/**
* Implements hook_help().
*/
function disqus_help($path, $arg) {
switch ($path) {
case 'admin/help#disqus':
$output = '<p>'. t('Uses the <a href="@disqus">Disqus</a> comment system to enhance comments.', array('@disqus' => 'http://disqus.com')) .'</p>';
$output.= '<h3>'. t('Installation') .'</h3>';
$output.= '<ol><li>'. t('Register your site information at <a href="http://disqus.com">Disqus</a>') .'</li>';
$output.= '<li>'. t('In the <a href="@configuration">Disqus configuration</a>, set the domain to what you registered with Disqus, and what node types you would like to have comments', array('@configuration' => url('admin/config/services/disqus'))) .'</li>';
$output.= '<li>'. t('Visit the <a href="@permissions">permissions</a>, and set which users you would like to have the ability to view Disqus threads (recommended for role)', array('@permissions' => url('admin/people/permissions', array('fragment' => 'module-disqus')))) .'</li></ol>';
return $output;
case 'admin/config/services/disqus':
return '<p>'. t('The following provides the general configuration options for the <a href="@disqus">Disqus</a> comment web service.', array('@disqus' => 'http://disqus.com')) .'</p>';
}
}
/**
* Implements hook_permission().
*/
function disqus_permission() {
return array(
'administer disqus' => array(
'title' => t('Administer Disqus'),
'description' => t('Perform administrative actions with Disqus.'),
),
'view disqus comments' => array(
'title' => t('View Disqus comments'),
'description' => t('Allows access to view Disqus comments.')
),
'display disqus comments on profile' => array(
'title' => t('Disqus comments in profile'),
'description' => t('When enabled, will display Disqus comments on the profiles of users belonging to this role.'),
),
'toggle disqus comments' => array(
'title' => t('Toggle Disqus comments'),
'description' => t('When enabled, will allow users to toggle comments on and off on nodes.'),
),
);
}
/**
* Implements hook_menu().
*/
function disqus_menu() {
$items['admin/config/services/disqus'] = array(
'route_name' => 'disqus.admin',
'title' => 'Disqus',
'description' => 'Provides configuration options for the Disqus comment system.',
);
$items['disqus/closewindow'] = array(
'route_name' => 'disqus.close_window',
'title' => 'Please wait',
'description' => 'Once the user logs in through the Disqus login workflow, they are redirected here to automatically close the popup window.',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_element_info().
*/
function disqus_element_info() {
$types['disqus'] = array(
'#disqus' => array(),
'#theme_wrappers' => array('disqus_noscript', 'container'),
'#attributes' => array('id' => 'disqus_thread'),
'#post_render' => array('disqus_element_post_render'),
);
return $types;
}
/**
* Post render function of the Disqus element to inject the Disqus JavaScript.
*/
function disqus_element_post_render($children, &$element) {
// Construct the settings to be passed in for Disqus.
$disqus = array(
'domain' => $element['#disqus']['domain'],
'url' => $element['#disqus']['url'],
'title' => $element['#disqus']['title'],
'identifier' => $element['#disqus']['identifier'],
);
if (isset($element['#disqus']['developer']) && $element['#disqus']['developer']) {
$disqus['developer'] = 1;
}
// If the user is logged in, we can inject the username and email for Disqus.
$account = \Drupal::currentUser();
if (variable_get('disqus_inherit_login', TRUE) && !$account->isAnonymous()) {
$disqus['name'] = $account->getUsername();
$disqus['email'] = $account->getEmail();
}
// Provide alternate language support if desired.
if (variable_get('disqus_localization', FALSE)) {
global $language;
$disqus['language'] = $language->language;
}
// Check if we are to provide Single Sign-On access.
if (variable_get('disqus_sso', FALSE)) {
$data = array();
// Inject the user data if it's available.
if (!$account->isAnonymous()) {
$data['id'] = $account->id();
$data['username'] = $account->getUsername();
$data['email'] = $account->getEmail();
$data['url'] = url('user/' . $account->id(), array('absolute' => TRUE));
// Load the user's avatar.
$user_picture_default = variable_get('user_picture_default', '');
if (isset($account->picture) && !empty($account->picture) && is_numeric($account->picture) && $file = file_load($account->picture)) {
$data['avatar'] = !empty($file->uri) ? $file->uri : NULL;
}
elseif (!empty($user_picture_default)) {
$data['avatar'] = variable_get('user_picture_default', '');
}
if (isset($data['avatar'])) {
$data['avatar'] = file_create_url($data['avatar']);
}
}
// Give Disqus information about the site.
$disqus['sso'] = array(
'name' => variable_get('site_name', t('Drupal')),
// The login window must be closed once the user logs in.
'url' => url('user/login', array('query' => array('destination' => 'disqus/closewindow'))),
// The logout link must redirect back to the original page.
'logout' => url('user/logout', array('query' => array('destination' => $_GET['q']))),
'width' => 800,
'height' => 600,
);
$managed_logo = variable_get('disqus_logo', FALSE);
$use_site_logo = variable_get('disqus_use_site_logo', TRUE);
if (!$use_site_logo && $managed_logo !== FALSE) {
$disqus['sso']['button'] = file_create_url(file_load($managed_logo)->uri);
}
elseif ($logo = theme_get_setting('logo')) {
$disqus['sso']['button'] = $logo;
}
else {
$disqus['sso']['button'] = url('misc/druplicon.png', array('absolute' => TRUE));
}
if ($favicon = theme_get_setting('favicon')) {
$disqus['sso']['icon'] = $favicon;
}
// Encode the data to be sent off to Disqus.
$message = base64_encode(json_encode($data));
$timestamp = time();
$hmac = hash_hmac('sha1', "$message $timestamp", variable_get('disqus_secretkey', ''));
// Stick the authentication requirements and data in the settings.
$disqus['remote_auth_s3'] = "$message $hmac $timestamp";
$disqus['api_key'] = variable_get('disqus_publickey', '');
}
/**
* Pass callbacks on if needed. Callbacks array is two dimensional array
* with callback type as key on first level and array of JS callbacks on the
* second level.
*
* Example:
* @code
* $element['#disqus']['callbacks'] = array(
* 'onNewComment' => array(
* 'myCallbackThatFiresOnCommentPost',
* 'Drupal.mymodule.anotherCallbInsideDrupalObj',
* ),
* );
* @endcode
*/
if (!empty($element['#disqus']['callbacks'])) {
$disqus['callbacks'] = $element['#disqus']['callbacks'];
}
return $children;
}
/**
* Implements hook_node_load().
*/
function disqus_node_load($nodes, $types) {
$disqus_config = \Drupal::config('disqus.settings');
// Make sure we only load Disqus on nodes of the desired types.
$disqustypes = $disqus_config->get('visibility.disqus_nodetypes');
// Check which Disqus domain to use.
$domain = $disqus_config->get('disqus_domain');
if (!empty($domain)) {
// Load Disqus into the nodes.
foreach ($nodes as &$node) {
if (!empty($disqustypes[$node->getType()])) {
// Save the data to the node object.
$node->disqus = array('domain' => $domain);
// Apply the Disqus status to the node.
$status = db_query("SELECT status FROM {disqus} WHERE nid = :nid", array(':nid' => $node->id()))->fetchObject();
$node->disqus['status'] = isset($status->status) ? (bool)$status->status : TRUE;
// Build the absolute URL without the alias for the disqus_url flag.
$node->disqus['url'] = url('node/' . $node->id(), array(
'absolute' => TRUE,
));
// Build the title.
$node->disqus['title'] = check_plain(strip_tags($node->getTitle()));
// Provide the identifier.
$node->disqus['identifier'] = 'node/' . $node->id();
// The developer flag must always be set when the node is unpublished.
if (!$node->isPublished()) {
$node->disqus['developer'] = 1;
}
elseif ($developer = $disqus_config->get('behavior.disqus_developer')) {
$node->disqus['developer'] = (int) $developer;
}
}
}
}
}
/**
* Implements hook_node_view().
*/
function disqus_node_view($node, $view_mode) {
if (isset($node->disqus) && \Drupal::currentUser()->hasPermission('view disqus comments') && $node->disqus['status'] == 1) {
switch ($view_mode) {
case 'full':
// Inject Disqus into the node object.
switch (variable_get('disqus_location', 'content_area')) {
case 'content_area':
// Inject into the node content.
$node->content['disqus'] = array(
'#type' => 'disqus',
'#disqus' => $node->disqus,
'#weight' => variable_get('disqus_weight', 50),
);
break;
}
break;
case 'teaser':
// Display the Disqus link.
$links['disqus_comments_num'] = array(
'title' => t('Comments'),
'href' => 'node/' . $node->id(),
'fragment' => 'disqus_thread',
'attributes' => array(
// Identify the node for Disqus with the unique identifier:
// http://docs.disqus.com/developers/universal/#comment-count
'data-disqus-identifier' => 'node/' . $node->id(),
),
);
$node->content['links']['disqus'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array(
'class' => array('links', 'inline'),
),
);
// Attach disqus.js to load the Disqus comment count JavaScript.
$node->content['links']['#attached']['js'][] = drupal_get_path('module', 'disqus') . '/disqus.js';
$node->content['links']['#attached']['js'][] = array(
'data' => array('disqusComments' => $node->disqus['domain']),
'type' => 'setting',
);
break;
}
}
else {
return array();
}
}
/**
* Implements hook_node_delete().
*/
function disqus_node_delete($node) {
db_delete('disqus')->condition('nid', $node->id())->execute();
}
/**
* Implements hook_node_insert().
*/
function disqus_node_insert($node) {
// Write the value only if it's disabled (default is enabled).
if (isset($node->disqus_status) && $node->disqus_status == FALSE) {
$data = array(
'nid' => $node->id(),
'status' => $node->disqus_status,
);
drupal_write_record('disqus', $data);
}
}
/**
* Implements hook_node_update().
*/
function disqus_node_update($node) {
if (isset($node->disqus_status) && isset($node->disqus['status']) && $node->disqus_status != $node->disqus['status']) {
if ($node->disqus_status) {
disqus_node_delete($node);
}
else {
disqus_node_insert($node);
}
}
}
/**
* Implements hook_user_load().
*/
function disqus_user_load($users) {
// Check which Disqus domain to use.
$domain = variable_get('disqus_domain', '');
if (!empty($domain)) {
foreach ($users as &$account) {
// Only show on the profile if desired. Don't show on the administrator's profile.
if (\Drupal::currentUser()->hasPermission('display disqus comments on profile', $account) && $account->uid != 1) {
// Save the data to the user object.
$account->disqus = array('domain' => $domain);
// Build the absolute URL without the alias for the disqus_url flag.
$account->disqus['url'] = url('user/' . $account->uid, array('absolute' => TRUE));
// Build the title.
$account->disqus['title'] = check_plain(strip_tags($account->name));
// Provide the identifier.
$account->disqus['identifier'] = 'user/' . $account->uid;
// Inject the script.
if ($developer = variable_get('disqus_developer', FALSE)) {
$account->disqus['developer'] = (int) $developer;
}
}
}
}
}
/**
* Implements hook_user_view().
*/
function disqus_user_view($account, $view_mode, $langcode) {
if (isset($account->disqus) && $view_mode == 'full') {
// Inject Disqus into the user object.
switch (variable_get('disqus_location', 'content_area')) {
case 'content_area':
$account->content['disqus'] = array(
'#type' => 'disqus',
'#disqus' => $account->disqus,
'#weight' => variable_get('disqus_weight', 50),
'#access' => \Drupal::currentUser()->hasPermission('view disqus comments'),
);
break;
}
}
}
/**
* Implementation of hook_form_alter().
*/
function disqus_form_alter(&$form, $form_state, $form_id) {
// Allow toggling the comments on or off per node from the node edit form.
if (!empty($form['#node_edit_form'])) {
$node = $form['#node'];
// Only display the toggle Disqus comments setting if comments are available
// for the given node type.
$types = variable_get('disqus_nodetypes', array());
if (isset($types[$node->getType()]) && !empty($types[$node->getType()])) {
// Add a comment settings fieldset for users with "toggle disqus comments" permission
// when Drupal core Comments module is disabled.
if (!isset($form['comment_settings'])) {
$form['comment_settings'] = array(
'#type' => 'fieldset',
'#access' => \Drupal::currentUser()->hasPermission('toggle disqus comments'),
'#title' => t('Comment settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#weight' => 30,
);
}
else {
if (isset($form['comment_settings']['comment'])) {
// Ensure only core Comment administrators see Comment module settings
$form['comment_settings']['comment']['#access'] = $form['comment_settings']['#access'];
// But reveal parent comment settings fieldset if user has toggle permission
$form['comment_settings']['#access'] = \Drupal::currentUser()->hasPermission('toggle disqus comments');
}
}
// Add the Disqus settings into the comment settings fieldset for users with toggle permission.
$form['comment_settings']['disqus_status'] = array(
'#type' => 'checkbox',
'#title' => t('Disqus comments'),
'#description' => t('Users can post comments using <a href="@disqus">Disqus</a>.', array('@disqus' => 'http://disqus.com')),
'#default_value' => isset($node->disqus['status']) ? $node->disqus['status'] : TRUE,
'#access' => \Drupal::currentUser()->hasPermission('toggle disqus comments'),
);
}
}
}
/**
* Implements hook_views_data_alter().
*/
function disqus_views_data_alter(&$data) {
// Number of Disqus comments made on the given node.
$data['node']['disqus_comment_count']['field'] = array(
'title' => t('Disqus Comment Count'),
'group' => t('Content'),
'help' => t('The number of Disqus comments made on the post. Note that this will not work in the preview.'),
'id' => 'disqus_comment_count',
);
}
/**
* Implements hook_theme().
*/
function disqus_theme() {
return array(
'disqus_noscript' => array(
'variables' => array('disqus' => NULL),
),
);
}
/**
* Prepares the noscript tag which is used when JavaScript is not available.
*
* @param $variables
* An array containing a "disqus" array, containing the following items:
* - "domain": The domain associated with this Disqus account.
* - "title": The title of the thread.
* - "developer": Whether or not testing is enabled.
* - "url": The disqus_url variable (http://disqus.com/docs/help/#faq-16).
*/
function theme_disqus_noscript($variables = array()) {
$disqus = $variables['disqus'];
$return = array(
'#markup' => '<noscript><p>' . l(t('View the discussion thread.'), 'http://' . $disqus['domain'] . '.disqus.com/?url=' . urlencode($disqus['url'])) . '</p></noscript>',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'disqus') . '/disqus.js',
array('type' => 'setting', 'data' => array('disqus' => $disqus))
)
)
);
return drupal_render($return);
}
/**
* Creates an instance of the Disqus PHP API.
*
* @param $user_api_key
* The User API Key.
* @param $forum_api_key
* The Forum API key.
*
* @return
* The instance of the Disqus API.
*/
function disqus($user_api_key = NULL, $forum_api_key = NULL) {
return new \Drupal\disqus\Disqus($user_api_key, $forum_api_key);
}