forked from foliovision/thoughtful-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkers.php
331 lines (265 loc) · 9.29 KB
/
walkers.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
<?php
/**
* Sets special actions for first and last comment
*/
class FV_TC_Walker_Comment_capture extends Walker_Comment {
/**
* Count the comments being displayed
* @int
*/
var $count = 0;
/**
* Store comment output start time
* @float
*/
var $tStart = false;
public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
if( $this->count == 0 ) {
$this->tStart = microtime(true);
$output .= "<!--fv comments cache - START-->\n";
}
$this->count++;
$depth++;
$GLOBALS['comment_depth'] = $depth;
$GLOBALS['comment'] = $comment;
if ( !empty( $args['callback'] ) ) {
ob_start();
call_user_func( $args['callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}
if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
ob_start();
$this->ping( $comment, $depth, $args );
$output .= ob_get_clean();
} elseif ( 'html5' === $args['format'] ) {
ob_start();
$this->html5_comment( $comment, $depth, $args );
$output .= ob_get_clean();
} else {
ob_start();
$this->comment( $comment, $depth, $args );
$output .= ob_get_clean();
}
}
public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
if ( !empty( $args['end-callback'] ) ) {
ob_start();
call_user_func( $args['end-callback'], $comment, $args, $depth );
$output .= ob_get_clean();
} else {
if ( 'div' == $args['style'] )
$output .= "</div><!-- #comment-## -->\n";
else
$output .= "</li><!-- #comment-## -->\n";
}
global $wp_query;
//$output .= "<!--fv comments cache - DEBUG ".$this->count." ".$wp_query->query_vars['comments_per_page']." remainder ".($wp_query->queried_object->comment_count % $wp_query->query_vars['comments_per_page'])."-->\n";
if(
$this->count == $wp_query->queried_object->comment_count ||
$this->count == $wp_query->query_vars['comments_per_page'] ||
( $this->count == $wp_query->queried_object->comment_count % $wp_query->query_vars['comments_per_page'] &&
$wp_query->query_vars['cpage'] == ceil($wp_query->queried_object->comment_count/$wp_query->query_vars['comments_per_page'])
)
) {
global $fv_tc;
$output .= "<!--fv comments cache - END-->\n";
echo "<!--fv comments cache - WP took ".(microtime(true)-$this->tStart)." seconds to list comments!-->\n";
$aCache = $fv_tc->cache_data;
$aCache['html'] = $output;
$aCache['date'] = date( 'U' );
$aCache['comments'] = $fv_tc->cache_comment_count;
if( !is_user_logged_in() && !$fv_tc->cache_comment_author && !isset( $_COOKIE['fv-debug'] ) ) {
$res = file_put_contents( $fv_tc->cache_filename, serialize( $aCache ) );
if( !$res ) {
echo "<!--fv comments error writing into $fv_tc->cache_filename -->\n";
} else {
echo "<!--fv comments cache - stored $fv_tc->cache_filename @ ".$aCache['date']."-->\n";
}
} else {
echo "<!--fv comments do not cache into $fv_tc->cache_filename @ ".$aCache['date']." - user logged in or comment author-->\n";
}
}
}
} // FV_TC_Walker_Comment_capture
/**
* Discards any output from wp_list_comments()
*/
class FV_TC_Walker_Comment_blank extends Walker_Comment {
public function start_lvl( &$output, $depth = 0, $args = array() ) {}
public function end_lvl( &$output, $depth = 0, $args = array() ) {}
public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
}
public function walk( $elements, $max_depth) {
$args = array_slice(func_get_args(), 2);
$output = '';
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* Need to display in hierarchical order.
* Separate elements into two buckets: top level and children elements.
* Children_elements is two dimensional array, eg.
* Children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
/*
* When none of the elements is top level.
* Assume the first one must be root of the sub elements.
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
}
foreach ( $top_level_elements as $e )
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
/*
* If we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless.
*/
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
return $output;
}
public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
/* sanity check */
if ( empty($elements) || $max_depth < -1 )
return '';
$args = array_slice( func_get_args(), 4 );
$output = '';
$parent_field = $this->db_fields['parent'];
$count = -1;
if ( -1 == $max_depth )
$total_top = count( $elements );
if ( $page_num < 1 || $per_page < 0 ) {
// No paging
$paging = false;
$start = 0;
if ( -1 == $max_depth )
$end = $total_top;
$this->max_pages = 1;
} else {
$paging = true;
$start = ( (int)$page_num - 1 ) * (int)$per_page;
$end = $start + $per_page;
if ( -1 == $max_depth )
$this->max_pages = ceil($total_top / $per_page);
}
// flat display
if ( -1 == $max_depth ) {
if ( !empty($args[0]['reverse_top_level']) ) {
$elements = array_reverse( $elements );
$oldstart = $start;
$start = $total_top - $end;
$end = $total_top - $oldstart;
}
$empty_array = array();
foreach ( $elements as $e ) {
$count++;
if ( $count < $start )
continue;
if ( $count >= $end )
break;
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
}
return $output;
}
/*
* Separate elements into two buckets: top level and children elements.
* Children_elements is two dimensional array, e.g.
* $children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
$total_top = count( $top_level_elements );
if ( $paging )
$this->max_pages = ceil($total_top / $per_page);
else
$end = $total_top;
if ( !empty($args[0]['reverse_top_level']) ) {
$top_level_elements = array_reverse( $top_level_elements );
$oldstart = $start;
$start = $total_top - $end;
$end = $total_top - $oldstart;
}
if ( !empty($args[0]['reverse_children']) ) {
foreach ( $children_elements as $parent => $children )
$children_elements[$parent] = array_reverse( $children );
}
foreach ( $top_level_elements as $e ) {
$count++;
// For the last page, need to unset earlier children in order to keep track of orphans.
if ( $end >= $total_top && $count < $start )
$this->unset_children( $e, $children_elements );
if ( $count < $start )
continue;
if ( $count >= $end )
break;
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
}
if ( $end >= $total_top && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
return $output;
}
public function get_number_of_root_elements( $elements ){
$num = 0;
$parent_field = $this->db_fields['parent'];
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$num++;
}
return $num;
}
// Unset all the children for a given top level element.
public function unset_children( $e, &$children_elements ){
if ( !$e || !$children_elements )
return;
$id_field = $this->db_fields['id'];
$id = $e->$id_field;
if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
foreach ( (array) $children_elements[$id] as $child )
$this->unset_children( $child, $children_elements );
if ( isset($children_elements[$id]) )
unset( $children_elements[$id] );
}
} // FV_TC_Walker_Comment_blank