author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 15:52:01 +0100 | |
changeset 17 | 34716fd837a4 |
parent 16 | a86126ab1dd4 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* A class for displaying various tree-like structures. |
|
4 |
* |
|
5 |
* Extend the Walker class to use it, see examples below. Child classes |
|
6 |
* do not need to implement all of the abstract methods in the class. The child |
|
7 |
* only needs to implement the methods that are needed. |
|
8 |
* |
|
9 |
* @since 2.1.0 |
|
10 |
* |
|
11 |
* @package WordPress |
|
12 |
* @abstract |
|
13 |
*/ |
|
14 |
class Walker { |
|
15 |
/** |
|
16 |
* What the class handles. |
|
17 |
* |
|
18 |
* @since 2.1.0 |
|
19 |
* @var string |
|
20 |
*/ |
|
5 | 21 |
public $tree_type; |
0 | 22 |
|
23 |
/** |
|
24 |
* DB fields to use. |
|
25 |
* |
|
26 |
* @since 2.1.0 |
|
27 |
* @var array |
|
28 |
*/ |
|
5 | 29 |
public $db_fields; |
0 | 30 |
|
31 |
/** |
|
32 |
* Max number of pages walked by the paged walker |
|
33 |
* |
|
34 |
* @since 2.7.0 |
|
35 |
* @var int |
|
36 |
*/ |
|
5 | 37 |
public $max_pages = 1; |
38 |
||
39 |
/** |
|
40 |
* Whether the current element has children or not. |
|
41 |
* |
|
42 |
* To be used in start_el(). |
|
43 |
* |
|
44 |
* @since 4.0.0 |
|
45 |
* @var bool |
|
46 |
*/ |
|
47 |
public $has_children; |
|
0 | 48 |
|
49 |
/** |
|
50 |
* Starts the list before the elements are added. |
|
51 |
* |
|
52 |
* The $args parameter holds additional values that may be used with the child |
|
53 |
* class methods. This method is called at the start of the output list. |
|
54 |
* |
|
55 |
* @since 2.1.0 |
|
56 |
* @abstract |
|
57 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @param string $output Used to append additional content (passed by reference). |
0 | 59 |
* @param int $depth Depth of the item. |
60 |
* @param array $args An array of additional arguments. |
|
61 |
*/ |
|
5 | 62 |
public function start_lvl( &$output, $depth = 0, $args = array() ) {} |
0 | 63 |
|
64 |
/** |
|
65 |
* Ends the list of after the elements are added. |
|
66 |
* |
|
67 |
* The $args parameter holds additional values that may be used with the child |
|
68 |
* class methods. This method finishes the list at the end of output of the elements. |
|
69 |
* |
|
70 |
* @since 2.1.0 |
|
71 |
* @abstract |
|
72 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* @param string $output Used to append additional content (passed by reference). |
0 | 74 |
* @param int $depth Depth of the item. |
75 |
* @param array $args An array of additional arguments. |
|
76 |
*/ |
|
5 | 77 |
public function end_lvl( &$output, $depth = 0, $args = array() ) {} |
0 | 78 |
|
79 |
/** |
|
80 |
* Start the element output. |
|
81 |
* |
|
82 |
* The $args parameter holds additional values that may be used with the child |
|
83 |
* class methods. Includes the element output also. |
|
84 |
* |
|
85 |
* @since 2.1.0 |
|
86 |
* @abstract |
|
87 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
* @param string $output Used to append additional content (passed by reference). |
0 | 89 |
* @param object $object The data object. |
90 |
* @param int $depth Depth of the item. |
|
91 |
* @param array $args An array of additional arguments. |
|
92 |
* @param int $current_object_id ID of the current item. |
|
93 |
*/ |
|
5 | 94 |
public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {} |
0 | 95 |
|
96 |
/** |
|
97 |
* Ends the element output, if needed. |
|
98 |
* |
|
99 |
* The $args parameter holds additional values that may be used with the child class methods. |
|
100 |
* |
|
101 |
* @since 2.1.0 |
|
102 |
* @abstract |
|
103 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* @param string $output Used to append additional content (passed by reference). |
0 | 105 |
* @param object $object The data object. |
106 |
* @param int $depth Depth of the item. |
|
107 |
* @param array $args An array of additional arguments. |
|
108 |
*/ |
|
5 | 109 |
public function end_el( &$output, $object, $depth = 0, $args = array() ) {} |
0 | 110 |
|
111 |
/** |
|
112 |
* Traverse elements to create list from elements. |
|
113 |
* |
|
114 |
* Display one element if the element doesn't have any children otherwise, |
|
115 |
* display the element and its children. Will only traverse up to the max |
|
116 |
* depth and no ignore elements under that depth. It is possible to set the |
|
117 |
* max depth to include all depths, see walk() method. |
|
118 |
* |
|
119 |
* This method should not be called directly, use the walk() method instead. |
|
120 |
* |
|
121 |
* @since 2.5.0 |
|
122 |
* |
|
123 |
* @param object $element Data object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @param array $children_elements List of elements to continue traversing (passed by reference). |
0 | 125 |
* @param int $max_depth Max depth to traverse. |
126 |
* @param int $depth Depth of current element. |
|
127 |
* @param array $args An array of arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* @param string $output Used to append additional content (passed by reference). |
0 | 129 |
*/ |
5 | 130 |
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
if ( ! $element ) { |
0 | 132 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
} |
0 | 134 |
|
135 |
$id_field = $this->db_fields['id']; |
|
5 | 136 |
$id = $element->$id_field; |
0 | 137 |
|
16 | 138 |
// Display this element. |
5 | 139 |
$this->has_children = ! empty( $children_elements[ $id ] ); |
140 |
if ( isset( $args[0] ) && is_array( $args[0] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
$args[0]['has_children'] = $this->has_children; // Back-compat. |
5 | 142 |
} |
143 |
||
16 | 144 |
$this->start_el( $output, $element, $depth, ...array_values( $args ) ); |
0 | 145 |
|
16 | 146 |
// Descend only when the depth is right and there are childrens for this element. |
147 |
if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) { |
|
0 | 148 |
|
9 | 149 |
foreach ( $children_elements[ $id ] as $child ) { |
0 | 150 |
|
9 | 151 |
if ( ! isset( $newlevel ) ) { |
0 | 152 |
$newlevel = true; |
16 | 153 |
// Start the child delimiter. |
154 |
$this->start_lvl( $output, $depth, ...array_values( $args ) ); |
|
0 | 155 |
} |
156 |
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); |
|
157 |
} |
|
158 |
unset( $children_elements[ $id ] ); |
|
159 |
} |
|
160 |
||
9 | 161 |
if ( isset( $newlevel ) && $newlevel ) { |
16 | 162 |
// End the child delimiter. |
163 |
$this->end_lvl( $output, $depth, ...array_values( $args ) ); |
|
0 | 164 |
} |
165 |
||
16 | 166 |
// End this element. |
167 |
$this->end_el( $output, $element, $depth, ...array_values( $args ) ); |
|
0 | 168 |
} |
169 |
||
170 |
/** |
|
171 |
* Display array of elements hierarchically. |
|
172 |
* |
|
173 |
* Does not assume any existing order of elements. |
|
174 |
* |
|
175 |
* $max_depth = -1 means flatly display every element. |
|
176 |
* $max_depth = 0 means display all levels. |
|
177 |
* $max_depth > 0 specifies the number of display levels. |
|
178 |
* |
|
179 |
* @since 2.1.0 |
|
16 | 180 |
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
181 |
* to the function signature. |
|
0 | 182 |
* |
183 |
* @param array $elements An array of elements. |
|
184 |
* @param int $max_depth The maximum hierarchical depth. |
|
16 | 185 |
* @param mixed ...$args Optional additional arguments. |
0 | 186 |
* @return string The hierarchical item output. |
187 |
*/ |
|
16 | 188 |
public function walk( $elements, $max_depth, ...$args ) { |
0 | 189 |
$output = ''; |
190 |
||
16 | 191 |
// Invalid parameter or nothing to walk. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
if ( $max_depth < -1 || empty( $elements ) ) { |
0 | 193 |
return $output; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
} |
0 | 195 |
|
196 |
$parent_field = $this->db_fields['parent']; |
|
197 |
||
16 | 198 |
// Flat display. |
0 | 199 |
if ( -1 == $max_depth ) { |
200 |
$empty_array = array(); |
|
9 | 201 |
foreach ( $elements as $e ) { |
0 | 202 |
$this->display_element( $e, $empty_array, 1, 0, $args, $output ); |
9 | 203 |
} |
0 | 204 |
return $output; |
205 |
} |
|
206 |
||
207 |
/* |
|
208 |
* Need to display in hierarchical order. |
|
209 |
* Separate elements into two buckets: top level and children elements. |
|
210 |
* Children_elements is two dimensional array, eg. |
|
211 |
* Children_elements[10][] contains all sub-elements whose parent is 10. |
|
212 |
*/ |
|
213 |
$top_level_elements = array(); |
|
214 |
$children_elements = array(); |
|
9 | 215 |
foreach ( $elements as $e ) { |
216 |
if ( empty( $e->$parent_field ) ) { |
|
0 | 217 |
$top_level_elements[] = $e; |
9 | 218 |
} else { |
0 | 219 |
$children_elements[ $e->$parent_field ][] = $e; |
9 | 220 |
} |
0 | 221 |
} |
222 |
||
223 |
/* |
|
224 |
* When none of the elements is top level. |
|
225 |
* Assume the first one must be root of the sub elements. |
|
226 |
*/ |
|
9 | 227 |
if ( empty( $top_level_elements ) ) { |
0 | 228 |
|
229 |
$first = array_slice( $elements, 0, 1 ); |
|
9 | 230 |
$root = $first[0]; |
0 | 231 |
|
232 |
$top_level_elements = array(); |
|
233 |
$children_elements = array(); |
|
9 | 234 |
foreach ( $elements as $e ) { |
235 |
if ( $root->$parent_field == $e->$parent_field ) { |
|
0 | 236 |
$top_level_elements[] = $e; |
9 | 237 |
} else { |
0 | 238 |
$children_elements[ $e->$parent_field ][] = $e; |
9 | 239 |
} |
0 | 240 |
} |
241 |
} |
|
242 |
||
9 | 243 |
foreach ( $top_level_elements as $e ) { |
0 | 244 |
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); |
9 | 245 |
} |
0 | 246 |
|
247 |
/* |
|
248 |
* If we are displaying all levels, and remaining children_elements is not empty, |
|
249 |
* then we got orphans, which should be displayed regardless. |
|
250 |
*/ |
|
16 | 251 |
if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) { |
0 | 252 |
$empty_array = array(); |
9 | 253 |
foreach ( $children_elements as $orphans ) { |
254 |
foreach ( $orphans as $op ) { |
|
0 | 255 |
$this->display_element( $op, $empty_array, 1, 0, $args, $output ); |
9 | 256 |
} |
257 |
} |
|
258 |
} |
|
0 | 259 |
|
9 | 260 |
return $output; |
0 | 261 |
} |
262 |
||
263 |
/** |
|
9 | 264 |
* paged_walk() - produce a page of nested elements |
265 |
* |
|
266 |
* Given an array of hierarchical elements, the maximum depth, a specific page number, |
|
267 |
* and number of elements per page, this function first determines all top level root elements |
|
268 |
* belonging to that page, then lists them and all of their children in hierarchical order. |
|
269 |
* |
|
0 | 270 |
* $max_depth = 0 means display all levels. |
271 |
* $max_depth > 0 specifies the number of display levels. |
|
272 |
* |
|
9 | 273 |
* @since 2.7.0 |
16 | 274 |
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
275 |
* to the function signature. |
|
0 | 276 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* @param array $elements |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* @param int $max_depth The maximum hierarchical depth. |
16 | 279 |
* @param int $page_num The specific page number, beginning with 1. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @param int $per_page |
16 | 281 |
* @param mixed ...$args Optional additional arguments. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
* @return string XHTML of the specified page of elements |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
*/ |
16 | 284 |
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
if ( empty( $elements ) || $max_depth < -1 ) { |
0 | 286 |
return ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
} |
0 | 288 |
|
289 |
$output = ''; |
|
290 |
||
291 |
$parent_field = $this->db_fields['parent']; |
|
292 |
||
293 |
$count = -1; |
|
9 | 294 |
if ( -1 == $max_depth ) { |
0 | 295 |
$total_top = count( $elements ); |
9 | 296 |
} |
297 |
if ( $page_num < 1 || $per_page < 0 ) { |
|
16 | 298 |
// No paging. |
0 | 299 |
$paging = false; |
9 | 300 |
$start = 0; |
301 |
if ( -1 == $max_depth ) { |
|
0 | 302 |
$end = $total_top; |
9 | 303 |
} |
0 | 304 |
$this->max_pages = 1; |
305 |
} else { |
|
306 |
$paging = true; |
|
9 | 307 |
$start = ( (int) $page_num - 1 ) * (int) $per_page; |
308 |
$end = $start + $per_page; |
|
309 |
if ( -1 == $max_depth ) { |
|
310 |
$this->max_pages = ceil( $total_top / $per_page ); |
|
311 |
} |
|
0 | 312 |
} |
313 |
||
16 | 314 |
// Flat display. |
0 | 315 |
if ( -1 == $max_depth ) { |
9 | 316 |
if ( ! empty( $args[0]['reverse_top_level'] ) ) { |
0 | 317 |
$elements = array_reverse( $elements ); |
318 |
$oldstart = $start; |
|
9 | 319 |
$start = $total_top - $end; |
320 |
$end = $total_top - $oldstart; |
|
0 | 321 |
} |
322 |
||
323 |
$empty_array = array(); |
|
324 |
foreach ( $elements as $e ) { |
|
325 |
$count++; |
|
9 | 326 |
if ( $count < $start ) { |
0 | 327 |
continue; |
9 | 328 |
} |
329 |
if ( $count >= $end ) { |
|
0 | 330 |
break; |
9 | 331 |
} |
0 | 332 |
$this->display_element( $e, $empty_array, 1, 0, $args, $output ); |
333 |
} |
|
334 |
return $output; |
|
335 |
} |
|
336 |
||
337 |
/* |
|
338 |
* Separate elements into two buckets: top level and children elements. |
|
339 |
* Children_elements is two dimensional array, e.g. |
|
340 |
* $children_elements[10][] contains all sub-elements whose parent is 10. |
|
341 |
*/ |
|
342 |
$top_level_elements = array(); |
|
343 |
$children_elements = array(); |
|
9 | 344 |
foreach ( $elements as $e ) { |
345 |
if ( 0 == $e->$parent_field ) { |
|
0 | 346 |
$top_level_elements[] = $e; |
9 | 347 |
} else { |
0 | 348 |
$children_elements[ $e->$parent_field ][] = $e; |
9 | 349 |
} |
0 | 350 |
} |
351 |
||
352 |
$total_top = count( $top_level_elements ); |
|
9 | 353 |
if ( $paging ) { |
354 |
$this->max_pages = ceil( $total_top / $per_page ); |
|
355 |
} else { |
|
0 | 356 |
$end = $total_top; |
9 | 357 |
} |
0 | 358 |
|
9 | 359 |
if ( ! empty( $args[0]['reverse_top_level'] ) ) { |
0 | 360 |
$top_level_elements = array_reverse( $top_level_elements ); |
9 | 361 |
$oldstart = $start; |
362 |
$start = $total_top - $end; |
|
363 |
$end = $total_top - $oldstart; |
|
0 | 364 |
} |
9 | 365 |
if ( ! empty( $args[0]['reverse_children'] ) ) { |
366 |
foreach ( $children_elements as $parent => $children ) { |
|
367 |
$children_elements[ $parent ] = array_reverse( $children ); |
|
368 |
} |
|
0 | 369 |
} |
370 |
||
371 |
foreach ( $top_level_elements as $e ) { |
|
372 |
$count++; |
|
373 |
||
374 |
// For the last page, need to unset earlier children in order to keep track of orphans. |
|
9 | 375 |
if ( $end >= $total_top && $count < $start ) { |
0 | 376 |
$this->unset_children( $e, $children_elements ); |
9 | 377 |
} |
0 | 378 |
|
9 | 379 |
if ( $count < $start ) { |
0 | 380 |
continue; |
9 | 381 |
} |
0 | 382 |
|
9 | 383 |
if ( $count >= $end ) { |
0 | 384 |
break; |
9 | 385 |
} |
0 | 386 |
|
387 |
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); |
|
388 |
} |
|
389 |
||
390 |
if ( $end >= $total_top && count( $children_elements ) > 0 ) { |
|
391 |
$empty_array = array(); |
|
9 | 392 |
foreach ( $children_elements as $orphans ) { |
393 |
foreach ( $orphans as $op ) { |
|
0 | 394 |
$this->display_element( $op, $empty_array, 1, 0, $args, $output ); |
9 | 395 |
} |
396 |
} |
|
0 | 397 |
} |
398 |
||
399 |
return $output; |
|
400 |
} |
|
401 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* Calculates the total number of root elements. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* @param array $elements Elements to list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @return int Number of root elements. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
*/ |
9 | 410 |
public function get_number_of_root_elements( $elements ) { |
411 |
$num = 0; |
|
0 | 412 |
$parent_field = $this->db_fields['parent']; |
413 |
||
9 | 414 |
foreach ( $elements as $e ) { |
415 |
if ( 0 == $e->$parent_field ) { |
|
0 | 416 |
$num++; |
9 | 417 |
} |
0 | 418 |
} |
419 |
return $num; |
|
420 |
} |
|
421 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
* Unset all the children for a given top level element. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* @param object $e |
16 | 428 |
* @param array $children_elements |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
*/ |
9 | 430 |
public function unset_children( $e, &$children_elements ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
if ( ! $e || ! $children_elements ) { |
0 | 432 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
} |
0 | 434 |
|
435 |
$id_field = $this->db_fields['id']; |
|
9 | 436 |
$id = $e->$id_field; |
0 | 437 |
|
9 | 438 |
if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) { |
439 |
foreach ( (array) $children_elements[ $id ] as $child ) { |
|
0 | 440 |
$this->unset_children( $child, $children_elements ); |
9 | 441 |
} |
442 |
} |
|
0 | 443 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
unset( $children_elements[ $id ] ); |
0 | 445 |
} |
446 |
||
16 | 447 |
} |