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