Changeset 42343 for trunk/src/wp-includes/class-wp-walker.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-walker.php (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-walker.php
r42228 r42343 142 142 } 143 143 144 $cb_args = array_merge( array( &$output, $element, $depth), $args);145 call_user_func_array( array($this, 'start_el'), $cb_args);144 $cb_args = array_merge( array( &$output, $element, $depth ), $args ); 145 call_user_func_array( array( $this, 'start_el' ), $cb_args ); 146 146 147 147 // descend only when the depth is right and there are childrens for this element 148 if ( ( $max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {149 150 foreach ( $children_elements[ $id ] as $child ) {151 152 if ( ! isset($newlevel) ) {148 if ( ( $max_depth == 0 || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) { 149 150 foreach ( $children_elements[ $id ] as $child ) { 151 152 if ( ! isset( $newlevel ) ) { 153 153 $newlevel = true; 154 154 //start the child delimiter 155 $cb_args = array_merge( array( &$output, $depth), $args);156 call_user_func_array( array($this, 'start_lvl'), $cb_args);155 $cb_args = array_merge( array( &$output, $depth ), $args ); 156 call_user_func_array( array( $this, 'start_lvl' ), $cb_args ); 157 157 } 158 158 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); … … 161 161 } 162 162 163 if ( isset( $newlevel) && $newlevel ){163 if ( isset( $newlevel ) && $newlevel ) { 164 164 //end the child delimiter 165 $cb_args = array_merge( array( &$output, $depth), $args);166 call_user_func_array( array($this, 'end_lvl'), $cb_args);165 $cb_args = array_merge( array( &$output, $depth ), $args ); 166 call_user_func_array( array( $this, 'end_lvl' ), $cb_args ); 167 167 } 168 168 169 169 //end this element 170 $cb_args = array_merge( array( &$output, $element, $depth), $args);171 call_user_func_array( array($this, 'end_el'), $cb_args);170 $cb_args = array_merge( array( &$output, $element, $depth ), $args ); 171 call_user_func_array( array( $this, 'end_el' ), $cb_args ); 172 172 } 173 173 … … 188 188 */ 189 189 public function walk( $elements, $max_depth ) { 190 $args = array_slice(func_get_args(), 2);190 $args = array_slice( func_get_args(), 2 ); 191 191 $output = ''; 192 192 … … 201 201 if ( -1 == $max_depth ) { 202 202 $empty_array = array(); 203 foreach ( $elements as $e ) 203 foreach ( $elements as $e ) { 204 204 $this->display_element( $e, $empty_array, 1, 0, $args, $output ); 205 } 205 206 return $output; 206 207 } … … 214 215 $top_level_elements = array(); 215 216 $children_elements = array(); 216 foreach ( $elements as $e ) {217 if ( empty( $e->$parent_field ) ) 217 foreach ( $elements as $e ) { 218 if ( empty( $e->$parent_field ) ) { 218 219 $top_level_elements[] = $e; 219 else220 } else { 220 221 $children_elements[ $e->$parent_field ][] = $e; 222 } 221 223 } 222 224 … … 225 227 * Assume the first one must be root of the sub elements. 226 228 */ 227 if ( empty( $top_level_elements) ) {229 if ( empty( $top_level_elements ) ) { 228 230 229 231 $first = array_slice( $elements, 0, 1 ); 230 $root = $first[0];232 $root = $first[0]; 231 233 232 234 $top_level_elements = array(); 233 235 $children_elements = array(); 234 foreach ( $elements as $e ) {235 if ( $root->$parent_field == $e->$parent_field ) 236 foreach ( $elements as $e ) { 237 if ( $root->$parent_field == $e->$parent_field ) { 236 238 $top_level_elements[] = $e; 237 else239 } else { 238 240 $children_elements[ $e->$parent_field ][] = $e; 239 } 240 } 241 242 foreach ( $top_level_elements as $e ) 241 } 242 } 243 } 244 245 foreach ( $top_level_elements as $e ) { 243 246 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); 247 } 244 248 245 249 /* … … 249 253 if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) { 250 254 $empty_array = array(); 251 foreach ( $children_elements as $orphans ) 252 foreach ( $orphans as $op ) 255 foreach ( $children_elements as $orphans ) { 256 foreach ( $orphans as $op ) { 253 257 $this->display_element( $op, $empty_array, 1, 0, $args, $output ); 258 } 259 } 254 260 } 255 261 … … 258 264 259 265 /** 260 * paged_walk() - produce a page of nested elements261 *262 * Given an array of hierarchical elements, the maximum depth, a specific page number,263 * and number of elements per page, this function first determines all top level root elements264 * belonging to that page, then lists them and all of their children in hierarchical order.265 *266 * paged_walk() - produce a page of nested elements 267 * 268 * Given an array of hierarchical elements, the maximum depth, a specific page number, 269 * and number of elements per page, this function first determines all top level root elements 270 * belonging to that page, then lists them and all of their children in hierarchical order. 271 * 266 272 * $max_depth = 0 means display all levels. 267 273 * $max_depth > 0 specifies the number of display levels. 268 274 * 269 * @since 2.7.0275 * @since 2.7.0 270 276 * 271 277 * @param array $elements … … 280 286 } 281 287 282 $args = array_slice( func_get_args(), 4 );288 $args = array_slice( func_get_args(), 4 ); 283 289 $output = ''; 284 290 … … 286 292 287 293 $count = -1; 288 if ( -1 == $max_depth ) 294 if ( -1 == $max_depth ) { 289 295 $total_top = count( $elements ); 290 if ( $page_num < 1 || $per_page < 0 ) { 296 } 297 if ( $page_num < 1 || $per_page < 0 ) { 291 298 // No paging 292 299 $paging = false; 293 $start = 0;294 if ( -1 == $max_depth ) 300 $start = 0; 301 if ( -1 == $max_depth ) { 295 302 $end = $total_top; 303 } 296 304 $this->max_pages = 1; 297 305 } else { 298 306 $paging = true; 299 $start = ( (int)$page_num - 1 ) * (int)$per_page; 300 $end = $start + $per_page; 301 if ( -1 == $max_depth ) 302 $this->max_pages = ceil($total_top / $per_page); 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 } 303 312 } 304 313 305 314 // flat display 306 315 if ( -1 == $max_depth ) { 307 if ( ! empty($args[0]['reverse_top_level']) ) {316 if ( ! empty( $args[0]['reverse_top_level'] ) ) { 308 317 $elements = array_reverse( $elements ); 309 318 $oldstart = $start; 310 $start = $total_top - $end;311 $end = $total_top - $oldstart;319 $start = $total_top - $end; 320 $end = $total_top - $oldstart; 312 321 } 313 322 … … 315 324 foreach ( $elements as $e ) { 316 325 $count++; 317 if ( $count < $start ) 326 if ( $count < $start ) { 318 327 continue; 319 if ( $count >= $end ) 328 } 329 if ( $count >= $end ) { 320 330 break; 331 } 321 332 $this->display_element( $e, $empty_array, 1, 0, $args, $output ); 322 333 } … … 331 342 $top_level_elements = array(); 332 343 $children_elements = array(); 333 foreach ( $elements as $e ) {334 if ( 0 == $e->$parent_field ) 344 foreach ( $elements as $e ) { 345 if ( 0 == $e->$parent_field ) { 335 346 $top_level_elements[] = $e; 336 else347 } else { 337 348 $children_elements[ $e->$parent_field ][] = $e; 349 } 338 350 } 339 351 340 352 $total_top = count( $top_level_elements ); 341 if ( $paging ) 342 $this->max_pages = ceil( $total_top / $per_page);343 else353 if ( $paging ) { 354 $this->max_pages = ceil( $total_top / $per_page ); 355 } else { 344 356 $end = $total_top; 345 346 if ( !empty($args[0]['reverse_top_level']) ) { 357 } 358 359 if ( ! empty( $args[0]['reverse_top_level'] ) ) { 347 360 $top_level_elements = array_reverse( $top_level_elements ); 348 $oldstart = $start; 349 $start = $total_top - $end; 350 $end = $total_top - $oldstart; 351 } 352 if ( !empty($args[0]['reverse_children']) ) { 353 foreach ( $children_elements as $parent => $children ) 354 $children_elements[$parent] = array_reverse( $children ); 361 $oldstart = $start; 362 $start = $total_top - $end; 363 $end = $total_top - $oldstart; 364 } 365 if ( ! empty( $args[0]['reverse_children'] ) ) { 366 foreach ( $children_elements as $parent => $children ) { 367 $children_elements[ $parent ] = array_reverse( $children ); 368 } 355 369 } 356 370 … … 359 373 360 374 // For the last page, need to unset earlier children in order to keep track of orphans. 361 if ( $end >= $total_top && $count < $start ) 375 if ( $end >= $total_top && $count < $start ) { 362 376 $this->unset_children( $e, $children_elements ); 363 364 if ( $count < $start ) 377 } 378 379 if ( $count < $start ) { 365 380 continue; 366 367 if ( $count >= $end ) 381 } 382 383 if ( $count >= $end ) { 368 384 break; 385 } 369 386 370 387 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); … … 373 390 if ( $end >= $total_top && count( $children_elements ) > 0 ) { 374 391 $empty_array = array(); 375 foreach ( $children_elements as $orphans ) 376 foreach ( $orphans as $op ) 392 foreach ( $children_elements as $orphans ) { 393 foreach ( $orphans as $op ) { 377 394 $this->display_element( $op, $empty_array, 1, 0, $args, $output ); 395 } 396 } 378 397 } 379 398 … … 389 408 * @return int Number of root elements. 390 409 */ 391 public function get_number_of_root_elements( $elements ) {392 $num = 0;410 public function get_number_of_root_elements( $elements ) { 411 $num = 0; 393 412 $parent_field = $this->db_fields['parent']; 394 413 395 foreach ( $elements as $e ) {396 if ( 0 == $e->$parent_field ) 414 foreach ( $elements as $e ) { 415 if ( 0 == $e->$parent_field ) { 397 416 $num++; 417 } 398 418 } 399 419 return $num; … … 408 428 * @param array $children_elements 409 429 */ 410 public function unset_children( $e, &$children_elements ) {430 public function unset_children( $e, &$children_elements ) { 411 431 if ( ! $e || ! $children_elements ) { 412 432 return; … … 414 434 415 435 $id_field = $this->db_fields['id']; 416 $id = $e->$id_field;417 418 if ( ! empty($children_elements[$id]) && is_array($children_elements[$id]) )419 foreach ( (array) $children_elements[ $id] as $child )436 $id = $e->$id_field; 437 438 if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) { 439 foreach ( (array) $children_elements[ $id ] as $child ) { 420 440 $this->unset_children( $child, $children_elements ); 441 } 442 } 421 443 422 444 unset( $children_elements[ $id ] );
Note: See TracChangeset
for help on using the changeset viewer.