/
discussit.plugin.php
244 lines (194 loc) · 6.3 KB
/
discussit.plugin.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
<?php
class Discussit extends Plugin
{
static $regex_name= '/@([A-Za-z0-9]+)/';
static $regex_url= '/@\<a href="(.+)#comment-([0-9]+)"\>([A-Za-z0-9]+)\<\/a\>/';
/**
* Add update beacon support
**/
public function action_update_check()
{
Update::add( $this->info->name, '95d9b76a-ca6c-f5d4-9551-e8c0b020e097', $this->info->version );
}
/**
* Find referenced comments
*/
private static function get_parents($comment, $content, $comments) {
$parents= array();
$params= array(
'post_id' => $comment->post->id,
'limit' => 1,
'orderby' => 'date DESC',
'where' => array('date < ' . $comment->date->sql)
);
// Check for name
preg_match_all(self::$regex_name, $content, $matches);
$matches= $matches[1];
foreach($matches as $name) {
$matched_comments= Comments::get(array_merge($params, array('criteria_fields' => array('name'), 'criteria' => $name)));
if(count($matched_comments) > 0) {
$parents[]= $matched_comments[0];
}
}
// Check for url
$matches= array();
preg_match_all(self::$regex_url, $content, $matches);
$ids= $matches[2];
foreach($ids as $id) {
$matched_comments= Comments::get(array_merge($params, array('id' => $id)));
if(count($matched_comments) > 0) {
$parents[]= $matched_comments[0];
}
}
return $parents;
}
/**
* Remove all relationships
*/
private function remove_relationships($comment, $parents) {
foreach($parents as $parent) {
if($parent->info->children) {
$children= unserialize($parent->info->children);
unset($children[$comment->id]);
$parent->info->children= serialize($children);
$parent->update();
}
}
}
/**
* Create relationships as needed
*/
private function create_relationships($comment, $parents, $update = true) {
$parents_save= array();
foreach($parents as $parent) {
if($parent->info->children) {
$children= unserialize($parent->info->children);
$children[$comment->id]= $comment->id;
} else {
$children= array($comment->id => $comment->id);
}
$parent->info->children= serialize($children);
$parent->update();
$parents_save[$parent->id]= $parent->id;
}
$comment->info->parents= serialize($parents_save);
if($update) {
$comment->update();
}
}
/**
* Test old comments for relationships
* This will take a loooooong time
*/
public function action_plugin_activation( $plugin_file )
{
$comments= Comments::get(array('nolimit' => true));
foreach($comments as $comment) {
$parents= self::get_parents($comment, $comment->content, $comment->post->comments);
$this->create_relationships($comment, $parents);
}
}
/**
* Remove all comment relationships
*/
public function action_plugin_deactivation( $plugin_file )
{
DB::delete(DB::table('commentinfo'), array('name' => 'parents'));
DB::delete(DB::table('commentinfo'), array('name' => 'children'));
}
/**
* Catch new comments
**/
public function action_comment_insert_after($comment)
{
$parents= self::get_parents($comment, $comment->content, $comment->post->comments);
$this->create_relationships($comment, $parents);
}
/**
* Clear and update relationships
*/
public function action_comment_update_content($comment, $old, $new) {
if(is_object($new)) {
$new= $new->value;
}
// Severe old ties
$this->remove_relationships($comment, $comment->parents);
// Create new ties
$parents= self::get_parents($comment, $new, $comment->post->comments);
$this->create_relationships($comment, $parents, false);
}
/**
* Magically enable use of parents
*/
public function filter_comment_parents($value, $comment) {
$parent_ids= unserialize($comment->info->parents);
if(!is_array($parent_ids)) {
return array();
}
$parents= Comments::get(array('id' => $parent_ids, 'nolimit' => true, 'orderby' => 'date ASC'));
return $parents;
}
/**
* Magically enable use of children
*/
public function filter_comment_children($value, $comment) {
$child_ids= unserialize($comment->info->children);
if(!is_array($child_ids)) {
return array();
}
$children= Comments::get(array('id' => $child_ids, 'nolimit' => true, 'orderby' => 'date ASC'));
return $children;
}
/**
* Linkify parents and children
*/
public function filter_comment_content_out($content, $comment) {
preg_match_all(self::$regex_name, $content, $matches);
$matches= $matches[1];
foreach($matches as $name) {
$params= array(
'post_id' => $comment->post->id,
'limit' => 1,
'orderby' => 'date DESC',
'where' => array('date < ' . $comment->date->sql),
'criteria_fields' => array('name'),
'criteria' => $name
);
$matched_comments= Comments::get($params);
if(count($matched_comments) > 0) {
$parent= $matched_comments[0];
$content= str_replace('@' . $name, '@<a href="' . $comment->post->permalink . '#comment-' . $parent->id . '" title="This comment is in response to ' . $parent->name . '">' . $name . '</a>', $content);
}
}
return $content;
}
/**
* Show stuff in the comment form
*/
public function action_form_comment_edit($form, $comment) {
$form->comment_controls->append('fieldset', 'discussion_tab', _t('Discussion'));
$discussion = $form->discussion_tab->append('wrapper', 'discussion');
$discussion->class = 'container';
if(count($comment->parents) > 0) {
$parents= array();
foreach($comment->parents as $parent) {
$parents[]= '<a href="' . URL::get('admin', 'page=comment&id=' . $parent->id) . '">' . $parent->name . '</a>';
}
$str= 'This comment is descended from comments by ' . Format::and_list($parents) . '.';
} else {
$str= 'This comment has no parents.';
}
$discussion->append('static', 'parents', '<div class="container"><p class="pct25">'._t('Parents').'</p><p>' . $str . '</p></div><hr />');
if(count($comment->children) > 0) {
$children= array();
foreach($comment->children as $child) {
$children[]= '<a href="' . URL::get('admin', 'page=comment&id=' . $child->id) . '">' . $child->name . '</a>';
}
$str= 'There are children of this comment by ' . Format::and_list($children) . '.';
} else {
$str= 'This comment has no children.';
}
$discussion->append('static', 'children', '<div class="container"><p class="pct25">'._t('Children').'</p><p>' . $str . '</p></div><hr />');
}
}
?>