133 } |
133 } |
134 |
134 |
135 $id_field = $this->db_fields['id']; |
135 $id_field = $this->db_fields['id']; |
136 $id = $element->$id_field; |
136 $id = $element->$id_field; |
137 |
137 |
138 //display this element |
138 // Display this element. |
139 $this->has_children = ! empty( $children_elements[ $id ] ); |
139 $this->has_children = ! empty( $children_elements[ $id ] ); |
140 if ( isset( $args[0] ) && is_array( $args[0] ) ) { |
140 if ( isset( $args[0] ) && is_array( $args[0] ) ) { |
141 $args[0]['has_children'] = $this->has_children; // Back-compat. |
141 $args[0]['has_children'] = $this->has_children; // Back-compat. |
142 } |
142 } |
143 |
143 |
144 $cb_args = array_merge( array( &$output, $element, $depth ), $args ); |
144 $this->start_el( $output, $element, $depth, ...array_values( $args ) ); |
145 call_user_func_array( array( $this, 'start_el' ), $cb_args ); |
145 |
146 |
146 // Descend only when the depth is right and there are childrens for this element. |
147 // 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 ] ) ) { |
148 if ( ( $max_depth == 0 || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) { |
|
149 |
148 |
150 foreach ( $children_elements[ $id ] as $child ) { |
149 foreach ( $children_elements[ $id ] as $child ) { |
151 |
150 |
152 if ( ! isset( $newlevel ) ) { |
151 if ( ! isset( $newlevel ) ) { |
153 $newlevel = true; |
152 $newlevel = true; |
154 //start the child delimiter |
153 // Start the child delimiter. |
155 $cb_args = array_merge( array( &$output, $depth ), $args ); |
154 $this->start_lvl( $output, $depth, ...array_values( $args ) ); |
156 call_user_func_array( array( $this, 'start_lvl' ), $cb_args ); |
|
157 } |
155 } |
158 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); |
156 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); |
159 } |
157 } |
160 unset( $children_elements[ $id ] ); |
158 unset( $children_elements[ $id ] ); |
161 } |
159 } |
162 |
160 |
163 if ( isset( $newlevel ) && $newlevel ) { |
161 if ( isset( $newlevel ) && $newlevel ) { |
164 //end the child delimiter |
162 // End the child delimiter. |
165 $cb_args = array_merge( array( &$output, $depth ), $args ); |
163 $this->end_lvl( $output, $depth, ...array_values( $args ) ); |
166 call_user_func_array( array( $this, 'end_lvl' ), $cb_args ); |
164 } |
167 } |
165 |
168 |
166 // End this element. |
169 //end this element |
167 $this->end_el( $output, $element, $depth, ...array_values( $args ) ); |
170 $cb_args = array_merge( array( &$output, $element, $depth ), $args ); |
|
171 call_user_func_array( array( $this, 'end_el' ), $cb_args ); |
|
172 } |
168 } |
173 |
169 |
174 /** |
170 /** |
175 * Display array of elements hierarchically. |
171 * Display array of elements hierarchically. |
176 * |
172 * |
179 * $max_depth = -1 means flatly display every element. |
175 * $max_depth = -1 means flatly display every element. |
180 * $max_depth = 0 means display all levels. |
176 * $max_depth = 0 means display all levels. |
181 * $max_depth > 0 specifies the number of display levels. |
177 * $max_depth > 0 specifies the number of display levels. |
182 * |
178 * |
183 * @since 2.1.0 |
179 * @since 2.1.0 |
|
180 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
181 * to the function signature. |
184 * |
182 * |
185 * @param array $elements An array of elements. |
183 * @param array $elements An array of elements. |
186 * @param int $max_depth The maximum hierarchical depth. |
184 * @param int $max_depth The maximum hierarchical depth. |
|
185 * @param mixed ...$args Optional additional arguments. |
187 * @return string The hierarchical item output. |
186 * @return string The hierarchical item output. |
188 */ |
187 */ |
189 public function walk( $elements, $max_depth ) { |
188 public function walk( $elements, $max_depth, ...$args ) { |
190 $args = array_slice( func_get_args(), 2 ); |
|
191 $output = ''; |
189 $output = ''; |
192 |
190 |
193 //invalid parameter or nothing to walk |
191 // Invalid parameter or nothing to walk. |
194 if ( $max_depth < -1 || empty( $elements ) ) { |
192 if ( $max_depth < -1 || empty( $elements ) ) { |
195 return $output; |
193 return $output; |
196 } |
194 } |
197 |
195 |
198 $parent_field = $this->db_fields['parent']; |
196 $parent_field = $this->db_fields['parent']; |
199 |
197 |
200 // flat display |
198 // Flat display. |
201 if ( -1 == $max_depth ) { |
199 if ( -1 == $max_depth ) { |
202 $empty_array = array(); |
200 $empty_array = array(); |
203 foreach ( $elements as $e ) { |
201 foreach ( $elements as $e ) { |
204 $this->display_element( $e, $empty_array, 1, 0, $args, $output ); |
202 $this->display_element( $e, $empty_array, 1, 0, $args, $output ); |
205 } |
203 } |
248 |
246 |
249 /* |
247 /* |
250 * If we are displaying all levels, and remaining children_elements is not empty, |
248 * If we are displaying all levels, and remaining children_elements is not empty, |
251 * then we got orphans, which should be displayed regardless. |
249 * then we got orphans, which should be displayed regardless. |
252 */ |
250 */ |
253 if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) { |
251 if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) { |
254 $empty_array = array(); |
252 $empty_array = array(); |
255 foreach ( $children_elements as $orphans ) { |
253 foreach ( $children_elements as $orphans ) { |
256 foreach ( $orphans as $op ) { |
254 foreach ( $orphans as $op ) { |
257 $this->display_element( $op, $empty_array, 1, 0, $args, $output ); |
255 $this->display_element( $op, $empty_array, 1, 0, $args, $output ); |
258 } |
256 } |
271 * |
269 * |
272 * $max_depth = 0 means display all levels. |
270 * $max_depth = 0 means display all levels. |
273 * $max_depth > 0 specifies the number of display levels. |
271 * $max_depth > 0 specifies the number of display levels. |
274 * |
272 * |
275 * @since 2.7.0 |
273 * @since 2.7.0 |
|
274 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
275 * to the function signature. |
276 * |
276 * |
277 * @param array $elements |
277 * @param array $elements |
278 * @param int $max_depth The maximum hierarchical depth. |
278 * @param int $max_depth The maximum hierarchical depth. |
279 * @param int $page_num The specific page number, beginning with 1. |
279 * @param int $page_num The specific page number, beginning with 1. |
280 * @param int $per_page |
280 * @param int $per_page |
|
281 * @param mixed ...$args Optional additional arguments. |
281 * @return string XHTML of the specified page of elements |
282 * @return string XHTML of the specified page of elements |
282 */ |
283 */ |
283 public function paged_walk( $elements, $max_depth, $page_num, $per_page ) { |
284 public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) { |
284 if ( empty( $elements ) || $max_depth < -1 ) { |
285 if ( empty( $elements ) || $max_depth < -1 ) { |
285 return ''; |
286 return ''; |
286 } |
287 } |
287 |
288 |
288 $args = array_slice( func_get_args(), 4 ); |
|
289 $output = ''; |
289 $output = ''; |
290 |
290 |
291 $parent_field = $this->db_fields['parent']; |
291 $parent_field = $this->db_fields['parent']; |
292 |
292 |
293 $count = -1; |
293 $count = -1; |
294 if ( -1 == $max_depth ) { |
294 if ( -1 == $max_depth ) { |
295 $total_top = count( $elements ); |
295 $total_top = count( $elements ); |
296 } |
296 } |
297 if ( $page_num < 1 || $per_page < 0 ) { |
297 if ( $page_num < 1 || $per_page < 0 ) { |
298 // No paging |
298 // No paging. |
299 $paging = false; |
299 $paging = false; |
300 $start = 0; |
300 $start = 0; |
301 if ( -1 == $max_depth ) { |
301 if ( -1 == $max_depth ) { |
302 $end = $total_top; |
302 $end = $total_top; |
303 } |
303 } |