-
Notifications
You must be signed in to change notification settings - Fork 0
/
disqus_rcw.php
406 lines (340 loc) · 14.2 KB
/
disqus_rcw.php
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
<?php
/*
* Plugin Name: Disqus Recent Comments (async)
* Description: Widget to show recent comments from Disqus API.
* Author: hg
* Version: 1.1.0
*/
class Disqus_RCW extends WP_Widget
{
public function __construct()
{
$widget_ops =
parent::__construct(
'disqus-rcw',
'Disqus Recent Comments (async)',
array('classname' => 'dsq-widget', 'description' => 'Recent comments from Disqus')
);
add_action('wp_ajax_nopriv_load_recent_comments', array($this, 'load_recent_comments_callback'));
add_action('wp_ajax_load_recent_comments', array($this, 'load_recent_comments_callback'));
}
public function widget($args, $instance)
{
set_default_values($instance);
if ($instance['css_enabled']) {
wp_enqueue_style('dashicons');
wp_enqueue_style('disqus-rcw', plugins_url('disqus_rcw.css', __FILE__));
}
if ($instance['livestamp_timeout'] > 0) {
wp_register_script('moment.js', '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js', array(), '2.11.2');
wp_register_script('moment.js-it', '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/locale/it.js', array('moment.js'), '2.11.2');
wp_register_script('livestamp', '//cdnjs.cloudflare.com/ajax/libs/livestamp/1.1.2/livestamp.min.js', array('jquery', 'moment.js-it'), '1.1.2');
wp_enqueue_script('livestamp');
}
echo $args['before_widget'];
echo $args['before_title'];
echo esc_html($instance['title']);
echo $args['after_title'];
if ($instance['show_update_button']) {
echo '<button class="disqus-rcw-update">';
echo '<i class="disqus-rcw-update-icon dashicons-before dashicons-update"></i>';
echo __('Update');
echo '</button>';
}
echo '<ul class="disqus-rcw-list dsq-widget-list" data-widget-number="'.$this->number.'">';
if ($instance['ajax_enabled']) {
wp_enqueue_script('disqus-rcw', plugins_url('disqus_rcw.js', __FILE__), array('jquery'));
wp_localize_script('disqus-rcw', 'localizedData', array(
'ajax_url' => admin_url('admin-ajax.php'),
'livestamp_timeout' => $instance['livestamp_timeout'],
));
} else {
$this->output_widget_content($instance);
}
echo '</ul>';
echo $args['after_widget'];
}
public function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['api_key'] = strip_tags($new_instance['api_key']);
$instance['forum_name'] = strip_tags($new_instance['forum_name']);
$instance['comment_limit'] = (int) $new_instance['comment_limit'];
$instance['comment_length'] = (int) $new_instance['comment_length'];
$instance['title'] = strip_tags($new_instance['title']);
$instance['request_comment_limit'] = (int) $new_instance['request_comment_limit'];
$instance['comments_per_thread'] = (int) $new_instance['comments_per_thread'];
$instance['cache_timeout'] = (int) $new_instance['cache_timeout'];
$instance['livestamp_timeout'] = (int) $new_instance['livestamp_timeout'];
$instance['ajax_enabled'] = (bool) $new_instance['ajax_enabled'];
$instance['show_update_button'] = (bool) $new_instance['show_update_button'];
$instance['css_enabled'] = (bool) $new_instance['css_enabled'];
return $instance;
}
public function form($instance)
{
set_default_values($instance);
extract($instance);
?>
<p>
<label for="<?= $this->get_field_id('title') ?>"><?='Title' ?></label>
<input id="<?= $this->get_field_id('title') ?>"
name="<?= $this->get_field_name('title') ?>"
value="<?= esc_attr($title) ?>"
type="text" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('comment_limit') ?>"><?= 'Comments shown in the widget' ?></label>
<input id="<?= $this->get_field_id('comment_limit') ?>"
name="<?= $this->get_field_name('comment_limit') ?>"
value="<?= esc_attr($comment_limit) ?>"
type="number" min="1" max="100" class="widefat"/>
</p>
<p>
<label for="<?= $this->get_field_id('comment_length') ?>"><?= 'Maximum comment length <small>0 to show the entire comment</small>' ?></label>
<input id="<?= $this->get_field_id('comment_length') ?>"
name="<?= $this->get_field_name('comment_length') ?>"
value="<?= esc_attr($comment_length) ?>"
type="number" min="1" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('comments_per_thread') ?>"><?= 'Comments from the same thread <small>0 to disable the restriction</small>' ?></label>
<input id="<?= $this->get_field_id('comments_per_thread') ?>"
name="<?= $this->get_field_name('comments_per_thread') ?>"
value="<?= esc_attr($comments_per_thread) ?>"
type="number" min="0" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('cache_timeout') ?>"><?= 'Cache timeout <small>in seconds, 0 to disable caching</small>' ?></label>
<input id="<?= $this->get_field_id('cache_timeout') ?>"
name="<?= $this->get_field_name('cache_timeout') ?>"
value="<?= esc_attr($cache_timeout) ?>"
type="number" min="0" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('livestamp_timeout') ?>"><?= 'Livestamp timeout <small>in seconds, 0 to disable livestamp</small>' ?></label>
<input id="<?= $this->get_field_id('livestamp_timeout') ?>"
name="<?= $this->get_field_name('livestamp_timeout') ?>"
value="<?= esc_attr($livestamp_timeout) ?>"
type="number" min="0" class="widefat" />
</p>
<p>
<input id="<?= $this->get_field_id('ajax_enabled') ?>"
name="<?= $this->get_field_name('ajax_enabled') ?>"
<?php checked($instance[ 'ajax_enabled' ]) ?>
value="true" type="checkbox" />
<label for="<?= $this->get_field_id('ajax_enabled') ?>"><?= 'Asyncronous loading' ?></label>
</p>
<p>
<input id="<?= $this->get_field_id('show_update_button') ?>"
name="<?= $this->get_field_name('show_update_button') ?>"
<?php checked($instance[ 'show_update_button' ]) ?>
value="true" type="checkbox" />
<label for="<?= $this->get_field_id('show_update_button') ?>"><?= 'Show update button' ?></label>
</p>
<p>
<input id="<?= $this->get_field_id('css_enabled') ?>"
name="<?= $this->get_field_name('css_enabled') ?>"
<?php checked($instance[ 'css_enabled' ]) ?>
value="true" type="checkbox" />
<label for="<?= $this->get_field_id('css_enabled') ?>"><?= 'Use widget CSS' ?></label>
</p>
<p>
<label for="<?= $this->get_field_id('api_key') ?>"><?='Api key:' ?></label>
<input id="<?= $this->get_field_id('api_key') ?>"
name="<?= $this->get_field_name('api_key') ?>"
value="<?= esc_attr($api_key) ?>"
type="text" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('forum_name') ?>"><?='Forum name:' ?></label>
<input id="<?= $this->get_field_id('forum_name') ?>"
name="<?= $this->get_field_name('forum_name') ?>"
value="<?= esc_attr($forum_name) ?>"
type="text" class="widefat" />
</p>
<p>
<label for="<?= $this->get_field_id('request_comment_limit') ?>"><?= 'Comments retrieved from disqus <small>[1, 100]</small>' ?></label>
<input id="<?= $this->get_field_id('request_comment_limit') ?>"
name="<?= $this->get_field_name('request_comment_limit') ?>"
value="<?= esc_attr($request_comment_limit) ?>"
type="number" min="1" max="100" class="widefat" />
</p>
<?php
}
protected function output_widget_content($instance)
{
$disqus_params = array(
'api_key' => $instance['api_key'],
'forum' => $instance['forum_name'],
'limit' => $instance['request_comment_limit'],
'related' => 'thread',
'include' => 'approved',
);
$comments = get_last_comments(
$disqus_params,
$instance['comments_per_thread'],
$instance['comment_limit'],
$instance['comment_length'],
$instance['cache_timeout'],
$this->number
);
foreach ($comments as $comment) {
output_comment($comment);
}
}
public function load_recent_comments_callback()
{
$number = intval($_POST['number']);
$widgets = get_option('widget_'.$this->id_base);
if ($widgets !== false) {
$instance = $widgets[$number];
$this->output_widget_content($instance);
}
wp_die();
}
}
function get_last_comments($disqus_params, $comments_per_thread, $comment_limit, $comment_length, $cache_timeout, $widget_id)
{
if ($cache_timeout > 1) {
$response = get_transient('disqus_rcw_cache_'.$widget_id);
if ($response !== false) {
$response = maybe_unserialize($response);
return $response;
}
}
$last_comments = array();
$thread_counters = array();
$response = query_disqus_api($disqus_params);
if (!is_array($response)) {
echo $response;
return array();
}
foreach ($response as $comment) {
if ($comments_per_thread > 0) {
$thread_id = $comment['thread']['id'];
if (isset($thread_counters[$thread_id])) {
if ($thread_counters[$thread_id] >= $comments_per_thread) {
continue;
} else {
$thread_counters[$thread_id] += 1;
}
} else {
$thread_counters[$thread_id] = 1;
}
}
$timestamp = strtotime($comment['createdAt']);
$newcomment = array(
'author_name' => $comment['author']['name'],
'author_url' => $comment['author']['profileUrl'],
'author_avatar' => $comment['author']['avatar']['large']['cache'],
'thread_name' => $comment['thread']['title'],
'thread_url' => $comment['thread']['link'],
'message' => truncate($comment['raw_message'], $comment_length),
'timestamp' => $timestamp,
'time' => date('H:i', $timestamp),
'datetime' => date('d/m/Y H:i:s', $timestamp),
'comment_url' => $comment['thread']['link'].'#comment-'.$comment['id'],
);
$last_comments[] = $newcomment;
++$comment_counter;
if ($comment_counter === $comment_limit) {
break;
}
}
if ($cache_timeout > 1) {
set_transient('disqus_rcw_cache_'.$widget_id, serialize($last_comments), apply_filters('disqus_rcw_cache_time', $cache_timeout));
}
return $last_comments;
}
function query_disqus_api($disqus_params)
{
$url = add_query_arg($disqus_params, 'https://disqus.com/api/3.0/posts/list.json');
$response = wp_remote_get($url);
if (is_wp_error($response) || !isset($response['body'])) {
return $response;
}
$body_array = json_decode($response['body'], true);
if (!isset($body_array['response'])) {
return 'No response field in JSON';
}
return $body_array['response'];
}
function output_comment($comment)
{
extract($comment);
echo '<li class="dsq-widget-item">';
echo '<a href="'.esc_url($author_url).'">';
echo '<img class="dsq-widget-avatar" src="'.esc_url($author_avatar).'">';
echo '</a>';
echo '<a class="dsq-widget-user" href="'.esc_url($author_url).'">'.esc_html($author_name).'</a> ';
echo '<span class="dsq-widget-comment"><p>'.esc_html($message).'</p></span>';
echo '<p class="dsq-widget-meta">';
echo '<a href="'.esc_url($thread_url).'">'.esc_html($thread_name).'</a> · ';
echo '<a href="'.esc_url($comment_url).'" data-livestamp="'.esc_attr($timestamp).'" title="'.esc_attr($datetime).'">'.esc_html($time).'</a>';
echo '</p>';
echo '</li>';
}
function set_default_values(&$instance)
{
$defaults = array(
'title' => 'Recent comments',
'comment_limit' => 10,
'comment_length' => 300,
'comments_per_thread' => 1,
'cache_timeout' => 30,
'livestamp_timeout' => 30,
'ajax_enabled' => true,
'show_update_button' => true,
'css_enabled' => true,
'api_key' => '',
'forum_name' => '',
'request_comment_limit' => 100,
);
foreach ($defaults as $field => $value) {
if (!isset($instance[$field])) {
$instance[$field] = $value;
}
}
}
function truncate($string, $length = 100, $append = '...')
{
if ($length > 0 && strlen($string) > $length) {
$string = substr($string, 0, $length).$append;
}
return $string;
}
/**
* time_elapsed_string().
*
* Zachary Johnson
* http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
*/
function time_elapsed_string($timestring)
{
if (($timestamp = strtotime($timestring)) === -1) {
return '';
}
$etime = time() - $timestamp;
if ($etime < 1) {
return 'adesso';
}
$a = array(12 * 30 * 24 * 60 * 60 => array('anno', 'anni'),
30 * 24 * 60 * 60 => array('mese', 'mesi'),
24 * 60 * 60 => array('giorno', 'giorni'),
60 * 60 => array('ora', 'ore'),
60 => array('minuto', 'minuti'),
1 => array('secondo', 'secondi'),
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r.' '.($r > 1 ? $str[1] : $str[0]).' fa';
}
}
}
add_action('widgets_init', function () {
register_widget('Disqus_RCW');
});
?>