/
plugin.lastcomments.php
183 lines (154 loc) · 4.78 KB
/
plugin.lastcomments.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
<?php
/*
* Plugin Name: LastComments
* Type: Block
* Version: 1.0
* Plugin URI: https://www.flatpress.org
* Author: FlatPress
* Author URI: https://www.flatpress.org
* Description: Adds a widget that shows the latest comments. Part of the standard distribution.
*/
define('LASTCOMMENTS_CACHE_FILE', CACHE_DIR . 'lastcomments.tmp');
define('LASTCOMMENTS_MAX', 8);
add_action('comment_post', 'plugin_lastcomments_cache', 0, 2);
function plugin_lastcomments_widget() {
if (false === ($f = io_load_file(LASTCOMMENTS_CACHE_FILE))) {
// no comments in cache
$list = array();
} else {
// if file exists and its correctly read, we get the stored list
// (it is stored in encoded form)
$list = unserialize($f);
}
$content = '<ul class="last-comments">';
// cimangi Aggiunta traduzione stringhe
// load plugin strings
// they're located under plugin.PLUGINNAME/lang/LANGID/
$lang = lang_load('plugin:lastcomments');
$update = false;
if ($count = count($list)) {
while ($arr = array_pop($list)) {
$q = new FPDB_Query(array(
'id' => $arr ['entry']
), null);
// first element of the array is dropped, as it is the ID, which
// we already know
@list (, $entry) = $q->getEntry();
if (!$entry) {
$count--;
$update = true;
continue;
}
$content .= "<li>
<blockquote class=\"comment-quote\" cite=\"comments.php?entry={$arr['entry']}#{$arr['id']}\">
{$arr['content']}
<p><a href=\"" . get_comments_link($arr ['entry']) . "#{$arr['id']}\">{$arr['name']} - {$entry['subject']}</a></p>
</blockquote></li>\n";
}
$subject = $lang ['plugin'] ['lastcomments'] ['last'] . ' ' . $count . ' ' . $lang ['plugin'] ['lastcomments'] ['comments'];
}
if (!$count) {
if ($update)
fs_delete(LASTCOMMENTS_CACHE_FILE);
$content .= '<li>' . $lang ['plugin'] ['lastcomments'] ['no_comments'] . '</li>';
$subject = $lang ['plugin'] ['lastcomments'] ['no_new_comments'];
}
$content .= '</ul>';
$entry ['subject'] = $subject;
$entry ['content'] = $content;
return $entry;
}
/**
* function plugin_lastcomments_cache
*
* comment cache is a reverse queue; we put
* element on the top, and we delete elements
* from bottom; this is because the output
* string is created reading queuing from top to bottom.
* All this headache stuff just to say that
* in the end the widget will show up elements ordered
* from newer to older :P
*
* @param $entryid string
* entry id i.e. entryNNNNNN-NNNNNN
* @param $comment array
* where $comment[0] is $commentid i.e. commentNNNNNN-NNNNNN
* and $comment[1] is the actual content array
*/
function plugin_lastcomments_cache($entryid, $comment) {
// max num of chars per comment
$CHOP_AT = 30;
list ($id, $content) = $comment;
comment_clean($content);
if (false === ($f = io_load_file(LASTCOMMENTS_CACHE_FILE))) {
// no comments in cache
$list = array();
} else {
// if file exists and its correctly read, we get the stored list
// (it is stored in encoded form)
$list = unserialize($f);
if (count($list) + 1 > LASTCOMMENTS_MAX) {
// comments are more than allowed maximum:
// we delete the last in queue.
array_shift($list);
}
}
if (strlen($content ['content']) > $CHOP_AT) {
$string = substr($content ['content'], 0, $CHOP_AT) . '...';
} else {
$string = $content ['content'];
}
array_push($list, array(
'name' => $content ['name'],
'content' => $string,
'id' => $id,
'entry' => $entryid
));
return io_write_file(LASTCOMMENTS_CACHE_FILE, serialize($list));
}
register_widget('lastcomments', 'LastComments', 'plugin_lastcomments_widget');
function plugin_lastcomments_rss() {
global $smarty;
if (false === ($f = io_load_file(LASTCOMMENTS_CACHE_FILE))) {
// no comments in cache
$list = array();
} else {
// if file exists and its correctly read, we get the stored list
// (it is stored in encoded form)
$list = unserialize($f);
}
$newlist = array();
foreach ($list as $c) {
$newlist [] = comment_parse($list ['entryid'], $list ['id']);
}
$smarty->assign('lastcomments_list', $newlist);
}
/*
*
* function plugin_lastcomments_def_rss_link() {
* return BLOG_BASEURL . "?feed=lastcomments-rss2";
* }
*
* function plugin_lastcomments_rss_link() {
* return apply_filters('plugin_lastcomments_rss_link', '');
* }
*
* add_action('wp_head', 'plugin_lastcomments_rsshead');
* function plugin_lastcomments_rsshead() {
* echo "\n<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Get RSS 2.0 Feed\" href=\"".
* plugin_lastcomments_rss_link()
* ."\" />\n";
* }
*
* add_action('init', 'plugin_lastcomments_rssinit');
* function plugin_lastcomments_rssinit() {
* global $smarty;
*
* if (isset($_GET['feed']) && $_GET['feed']=='lastcomments-rss2') {
* $smarty->display('plugin:lastcomments/plugin.lastcomments-feed');
* exit();
* }
* }
*
*/
?>