Changeset 3704 for trunk/wp-includes/classes.php
- Timestamp:
- 04/13/2006 04:40:48 AM (20 years ago)
- File:
-
- 1 edited
-
trunk/wp-includes/classes.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/classes.php
r3682 r3704 401 401 } 402 402 403 404 // A class for displaying various tree-like structures. Extend the Walker class to use it, see examples at the bottom 405 406 class Walker { 407 var $tree_type; 408 var $db_fields; 409 410 //abstract callbacks 411 function start_lvl($output) { return $output; } 412 function end_lvl($output) { return $output; } 413 function start_el($output) { return $output; } 414 function end_el($output) { return $output; } 415 416 function walk($elements, $to_depth) { 417 $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = ''; 418 419 //padding at the end 420 $last_element->post_parent = 0; 421 $last_element->post_id = 0; 422 $elements[] = $last_element; 423 424 $id_field = $this->db_fields['id']; 425 $parent_field = $this->db_fields['parent']; 426 427 $flat = ($to_depth == -1) ? true : false; 428 429 foreach ( $elements as $element ) { 430 // If flat, start and end the element and skip the level checks. 431 if ( $flat) { 432 // Start the element. 433 if ( $element->$id_field != 0 ) { 434 $cb_args = array_merge( array($output, $element, $depth - 1), $args); 435 $output = call_user_func_array(array(&$this, 'start_el'), $cb_args); 436 } 437 438 // End the element. 439 if ( $element->$id_field != 0 ) { 440 $cb_args = array_merge( array($output, $element, $depth - 1), $args); 441 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 442 } 443 444 continue; 445 } 446 447 // Walk the tree. 448 if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) { 449 // Previous element is my parent. Descend a level. 450 array_unshift($parents, $previous_element); 451 $depth++; //always do this so when we start the element further down, we know where we are 452 if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth 453 $cb_args = array_merge( array($output, $depth - 1), $args); 454 $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args); 455 } 456 } else if ( $element->$parent_field == $previous_element->$parent_field) { 457 // On the same level as previous element. 458 if ( !$to_depth || ($depth <= $to_depth) ) { 459 $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args); 460 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 461 } 462 } else if ( $depth > 1 ) { 463 // Ascend one or more levels. 464 if ( !$to_depth || ($depth <= $to_depth) ) { 465 $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args); 466 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 467 } 468 469 while ( $parent = array_shift($parents) ) { 470 $depth--; 471 if ( !$to_depth || ($depth < $to_depth) ) { 472 $cb_args = array_merge( array($output, $depth - 1), $args); 473 $output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args); 474 $cb_args = array_merge( array($output, $parent, $depth - 1), $args); 475 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 476 } 477 if ( $element->$parent_field == $parents[0]->$id_field ) { 478 break; 479 } 480 } 481 } else if ( !empty($previous_element) ) { 482 // Close off previous element. 483 if ( !$to_depth || ($depth <= $to_depth) ) { 484 $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args); 485 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 486 } 487 } 488 489 // Start the element. 490 if ( !$to_depth || ($depth <= $to_depth) ) { 491 if ( $element->$id_field != 0 ) { 492 $cb_args = array_merge( array($output, $element, $depth - 1), $args); 493 $output = call_user_func_array(array(&$this, 'start_el'), $cb_args); 494 } 495 } 496 497 $previous_element = $element; 498 } 499 500 return $output; 501 } 502 } 503 504 class Walker_Page extends Walker { 505 var $tree_type = 'page'; 506 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this 507 508 function start_lvl($output, $depth) { 509 $indent = str_repeat("\t", $depth); 510 $output .= "$indent<ul>\n"; 511 return $output; 512 } 513 514 function end_lvl($output, $depth) { 515 $indent = str_repeat("\t", $depth); 516 $output .= "$indent</ul>\n"; 517 return $output; 518 } 519 520 function start_el($output, $page, $depth, $current_page, $show_date, $date_format) { 521 if ( $depth ) 522 $indent = str_repeat("\t", $depth); 523 524 $css_class = 'page_item'; 525 if ( $page->ID == $current_page ) 526 $css_class .= ' current_page_item'; 527 528 $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . wp_specialchars($page->post_title) . '">' . $page->post_title . '</a>'; 529 530 if ( !empty($show_date) ) { 531 if ( 'modified' == $show_date ) 532 $time = $page->post_modified; 533 else 534 $time = $page->post_date; 535 536 $output .= " " . mysql2date($date_format, $time); 537 } 538 539 return $output; 540 } 541 542 function end_el($output, $page, $depth) { 543 $output .= "</li>\n"; 544 545 return $output; 546 } 547 548 } 549 550 class Walker_PageDropdown extends Walker { 551 var $tree_type = 'page'; 552 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this 553 554 function start_el($output, $page, $depth, $args) { 555 $pad = str_repeat(' ', $depth * 3); 556 557 $output .= "\t<option value=\"$page->ID\""; 558 if ( $page->ID == $args['selected'] ) 559 $output .= ' selected="selected"'; 560 $output .= '>'; 561 $title = wp_specialchars($page->post_title); 562 $output .= "$pad$title"; 563 $output .= "</option>\n"; 564 565 return $output; 566 } 567 } 568 569 class Walker_Category extends Walker { 570 var $tree_type = 'category'; 571 var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this 572 573 function start_lvl($output, $depth, $args) { 574 if ( 'list' != $args['style'] ) 575 return $output; 576 577 $indent = str_repeat("\t", $depth); 578 $output .= "$indent<ul class='children'>\n"; 579 return $output; 580 } 581 582 function end_lvl($output, $depth, $args) { 583 if ( 'list' != $args['style'] ) 584 return $output; 585 586 $indent = str_repeat("\t", $depth); 587 $output .= "$indent</ul>\n"; 588 return $output; 589 } 590 591 function start_el($output, $category, $depth, $args) { 592 extract($args); 593 594 $link = '<a href="' . get_category_link($category->cat_ID) . '" '; 595 if ( $use_desc_for_title == 0 || empty($category->category_description) ) 596 $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"'; 597 else 598 $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"'; 599 $link .= '>'; 600 $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>'; 601 602 if ( (! empty($feed_image)) || (! empty($feed)) ) { 603 $link .= ' '; 604 605 if ( empty($feed_image) ) 606 $link .= '('; 607 608 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"'; 609 610 if ( !empty($feed) ) { 611 $title = ' title="' . $feed . '"'; 612 $alt = ' alt="' . $feed . '"'; 613 $name = $feed; 614 $link .= $title; 615 } 616 617 $link .= '>'; 618 619 if ( !empty($feed_image) ) 620 $link .= "<img src='$feed_image' $alt$title" . ' />'; 621 else 622 $link .= $name; 623 $link .= '</a>'; 624 if (empty($feed_image)) 625 $link .= ')'; 626 } 627 628 if ( $show_count ) 629 $link .= ' ('.intval($category->category_count).')'; 630 631 if ( $show_date ) { 632 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp); 633 } 634 635 if ( 'list' == $args['style'] ) { 636 $output .= "\t<li"; 637 if ( ($category->cat_ID == $current_category) && is_category() ) 638 $output .= ' class="current-cat"'; 639 $output .= ">$link\n"; 640 } else { 641 $output .= "\t$link<br />\n"; 642 } 643 644 return $output; 645 } 646 647 function end_el($output, $page, $depth) { 648 if ( 'list' != $args['style'] ) 649 return $output; 650 651 $output .= "</li>\n"; 652 return $output; 653 } 654 655 } 656 657 class Walker_CategoryDropdown extends Walker { 658 var $tree_type = 'category'; 659 var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this 660 661 function start_el($output, $category, $depth, $args) { 662 $pad = str_repeat(' ', $depth * 3); 663 664 $cat_name = apply_filters('list_cats', $category->cat_name, $category); 665 $output .= "\t<option value=\"".$category->cat_ID."\""; 666 if ( $category->cat_ID == $args['selected'] ) 667 $output .= ' selected="selected"'; 668 $output .= '>'; 669 $output .= $cat_name; 670 if ( $args['show_count'] ) 671 $output .= ' ('. $category->category_count .')'; 672 if ( $args['show_last_update'] ) { 673 $format = 'Y-m-d'; 674 $output .= ' ' . gmdate($format, $category->last_update_timestamp); 675 } 676 $output .= "</option>\n"; 677 678 return $output; 679 } 680 } 681 403 682 ?>
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)