Changeset 3704
- Timestamp:
- 04/13/2006 04:40:48 AM (19 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 4 edited
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 ?> -
trunk/wp-includes/functions.php
r3701 r3704 713 713 return $_page; 714 714 } 715 }716 717 function walk_tree($tree_type, $elements, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {718 $args = array_slice(func_get_args(), 7);719 $parents = array();720 $depth = 1;721 $previous_element = '';722 $output = '';723 724 $last_element->post_parent = 0;725 $last_element->post_id = 0;726 $elements[] = $last_element;727 728 if ( 'page' == $tree_type ) {729 $parent_field = 'post_parent';730 $id_field = 'ID';731 } else {732 $parent_field = 'category_parent';733 $id_field = 'cat_ID';734 }735 736 $flat = false;737 if ( $to_depth == -1 )738 $flat = true;739 740 foreach ( $elements as $element ) {741 // If flat, start and end the element and skip the level checks.742 if ( $flat) {743 // Start the element.744 if ( !empty($start_element_callback) && ($element->$id_field != 0) ) {745 $cb_args = array_merge( array($output, $element, $depth), $args);746 $output = call_user_func_array($start_element_callback, $cb_args);747 }748 749 // End the element.750 if ( !empty($end_element_callback) && ($element->$id_field != 0) ) {751 $cb_args = array_merge( array($output, $element, $depth), $args);752 $output = call_user_func_array($end_element_callback, $cb_args);753 }754 755 continue;756 }757 758 // Walk the tree.759 if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) {760 // Previous element is my parent. Descend a level.761 $depth++; //always do this so when we start the element further down, we know where we are762 array_unshift($parents, $previous_element);763 if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth764 if ( !empty($start_level_callback) ) {765 $cb_args = array_merge( array($output, $depth), $args);766 $output = call_user_func_array($start_level_callback, $cb_args);767 }768 }769 } else if ( $element->$parent_field == $previous_element->$parent_field ) {770 // On the same level as previous element, so close the previous771 if ( !$to_depth || ($depth <= $to_depth) ) {772 if ( !empty($end_element_callback) ) {773 $cb_args = array_merge( array($output, $previous_element, $depth), $args);774 $output = call_user_func_array($end_element_callback, $cb_args);775 }776 }777 } else if ( $depth > 1 ) {778 // Ascend one or more levels.779 if ( !$to_depth || ($depth <= $to_depth) ) {780 if ( !empty($end_element_callback) ) {781 $cb_args = array_merge( array($output, $previous_element, $depth), $args);782 $output = call_user_func_array($end_element_callback, $cb_args);783 }784 }785 786 while ( $parent = array_shift($parents) ) {787 $depth--;788 if ( !$to_depth || ($depth < $to_depth) ) {789 if ( !empty($end_level_callback) ) {790 $cb_args = array_merge( array($output, $depth), $args);791 $output = call_user_func_array($end_level_callback, $cb_args);792 }793 if ( !empty($end_element_callback) ) {794 $cb_args = array_merge( array($output, $parent, $depth), $args);795 $output = call_user_func_array($end_element_callback, $cb_args);796 }797 }798 if ( $element->$parent_field == $parents[0]->$id_field ) {799 break;800 }801 }802 } else if ( !empty($previous_element) ) {803 // Close off previous element.804 if ( !$to_depth || ($depth <= $to_depth) ) {805 if ( !empty($end_element_callback) ) {806 $cb_args = array_merge( array($output, $previous_element, $depth), $args);807 $output = call_user_func_array($end_element_callback, $cb_args);808 }809 }810 }811 812 // Start the element.813 if ( !$to_depth || ($depth <= $to_depth) ) {814 if ( !empty($start_element_callback) && ($element->$id_field != 0) ) {815 $cb_args = array_merge( array($output, $element, $depth), $args);816 $output = call_user_func_array($start_element_callback, $cb_args);817 }818 }819 820 $previous_element = $element;821 }822 823 return $output;824 }825 826 function walk_page_tree($pages, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {827 $args = array('page', $pages, $to_depth, $start_element_callback, $end_element_callback, $start_level_callback, $end_level_callback);828 $extra_args = array_slice(func_get_args(), 6);829 830 return call_user_func_array('walk_tree', array_merge($args, $extra_args));831 }832 833 function walk_category_tree($pages, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {834 $args = array('category', $pages, $to_depth, $start_element_callback, $end_element_callback, $start_level_callback, $end_level_callback);835 $extra_args = array_slice(func_get_args(), 6);836 return call_user_func_array('walk_tree', array_merge($args, $extra_args));837 715 } 838 716 -
trunk/wp-includes/template-functions-category.php
r3691 r3704 1 1 <?php 2 3 function walk_category_tree() { 4 $walker = new Walker_Category; 5 $args = func_get_args(); 6 return call_user_func_array(array(&$walker, 'walk'), $args); 7 } 8 9 function walk_category_dropdown_tree() { 10 $walker = new Walker_CategoryDropdown; 11 $args = func_get_args(); 12 return call_user_func_array(array(&$walker, 'walk'), $args); 13 } 2 14 3 15 function get_the_category($id = false) { … … 186 198 $depth = -1; // Flat. 187 199 188 $output .= walk_category_ tree($categories, $depth, '_category_dropdown_element', '', '', '', $selected, $r);200 $output .= walk_category_dropdown_tree($categories, $depth, $r); 189 201 $output .= "</select>\n"; 190 202 } … … 194 206 if ( $echo ) 195 207 echo $output; 196 197 return $output;198 }199 200 function _category_dropdown_element($output, $category, $depth, $selected, $args) {201 $pad = str_repeat(' ', $depth * 3);202 203 $cat_name = apply_filters('list_cats', $category->cat_name, $category);204 $output .= "\t<option value=\"".$category->cat_ID."\"";205 if ( $category->cat_ID == $selected )206 $output .= ' selected="selected"';207 $output .= '>';208 $output .= $cat_name;209 if ( $args['show_count'] )210 $output .= ' ('. $category->category_count .')';211 if ( $args['show_last_update'] ) {212 $format = 'Y-m-d';213 $output .= ' ' . gmdate($format, $category->last_update_timestamp);214 }215 $output .= "</option>\n";216 208 217 209 return $output; … … 246 238 } else { 247 239 global $wp_query; 248 $ current_category= $wp_query->get_queried_object_id();240 $r['current_category'] = $wp_query->get_queried_object_id(); 249 241 if ( $hierarchical ) 250 242 $depth = 0; // Walk the full depth. … … 252 244 $depth = -1; // Flat. 253 245 254 $output .= walk_category_tree($categories, $depth, '_category_list_element_start', '_category_list_element_end', '_category_list_level_start', '_category_list_level_end', $current_category,$r);246 $output .= walk_category_tree($categories, $depth, $r); 255 247 } 256 248 … … 259 251 260 252 echo apply_filters('list_cats', $output); 261 }262 263 function _category_list_level_start($output, $depth, $cat, $args) {264 if ( 'list' != $args['style'] )265 return $output;266 267 $indent = str_repeat("\t", $depth);268 $output .= "$indent<ul class='children'>\n";269 return $output;270 }271 272 function _category_list_level_end($output, $depth, $cat, $args) {273 if ( 'list' != $args['style'] )274 return $output;275 276 $indent = str_repeat("\t", $depth);277 $output .= "$indent</ul>\n";278 return $output;279 }280 281 function _category_list_element_start($output, $category, $depth, $current_category, $args) {282 extract($args);283 284 $link = '<a href="' . get_category_link($category->cat_ID) . '" ';285 if ( $use_desc_for_title == 0 || empty($category->category_description) )286 $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';287 else288 $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';289 $link .= '>';290 $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';291 292 if ( (! empty($feed_image)) || (! empty($feed)) ) {293 $link .= ' ';294 295 if ( empty($feed_image) )296 $link .= '(';297 298 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';299 300 if ( !empty($feed) ) {301 $title = ' title="' . $feed . '"';302 $alt = ' alt="' . $feed . '"';303 $name = $feed;304 $link .= $title;305 }306 307 $link .= '>';308 309 if ( !empty($feed_image) )310 $link .= "<img src='$feed_image' $alt$title" . ' />';311 else312 $link .= $name;313 $link .= '</a>';314 if (empty($feed_image))315 $link .= ')';316 }317 318 if ( $show_count )319 $link .= ' ('.intval($category->category_count).')';320 321 if ( $show_date ) {322 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);323 }324 325 if ( 'list' == $args['style'] ) {326 $output .= "\t<li";327 if ( ($category->cat_ID == $current_category) && is_category() )328 $output .= ' class="current-cat"';329 $output .= ">$link\n";330 } else {331 $output .= "\t$link<br />\n";332 }333 334 return $output;335 }336 337 function _category_list_element_end($output, $category, $depth, $cat, $args) {338 if ( 'list' != $args['style'] )339 return $output;340 341 $output .= "</li>\n";342 return $output;343 253 } 344 254 -
trunk/wp-includes/template-functions-post.php
r3701 r3704 279 279 */ 280 280 281 function walk_page_tree() { 282 $walker = new Walker_Page; 283 $args = func_get_args(); 284 return call_user_func_array(array(&$walker, 'walk'), $args); 285 } 286 287 function walk_page_dropdown_tree() { 288 $walker = new Walker_PageDropdown; 289 $args = func_get_args(); 290 return call_user_func_array(array(&$walker, 'walk'), $args); 291 } 281 292 282 293 function &get_page_children($page_id, $pages) { … … 382 393 if ( ! empty($pages) ) { 383 394 $output = "<select name='$name'>\n"; 384 $output .= walk_page_ tree($pages, $depth, '_page_dropdown_element', '', '', '', $selected);395 $output .= walk_page_dropdown_tree($pages, $depth, $r); 385 396 $output .= "</select>\n"; 386 397 } … … 390 401 if ( $echo ) 391 402 echo $output; 392 393 return $output;394 }395 396 function _page_dropdown_element($output, $page, $depth, $selected) {397 $pad = str_repeat(' ', $depth * 3);398 399 $output .= "\t<option value=\"$page->ID\"";400 if ( $page->ID == $selected )401 $output .= ' selected="selected"';402 $output .= '>';403 $title = wp_specialchars($page->post_title);404 $output .= "$pad$title";405 $output .= "</option>\n";406 403 407 404 return $output; … … 429 426 global $wp_query; 430 427 $current_page = $wp_query->get_queried_object_id(); 431 $output .= walk_page_tree($pages, $ r['depth'], '_page_list_element_start', '_page_list_element_end', '_page_list_level_start', '_page_list_level_end', $current_page, $r['show_date'], $r['date_format']);428 $output .= walk_page_tree($pages, $depth, $current_page, $r['show_date'], $r['date_format']); 432 429 433 430 if ( $r['title_li'] ) … … 441 438 else 442 439 return $output; 443 }444 445 function _page_list_level_start($output, $depth) {446 $indent = str_repeat("\t", $depth);447 $output .= "$indent<ul>\n";448 return $output;449 }450 451 function _page_list_level_end($output, $depth) {452 $indent = str_repeat("\t", $depth);453 $output .= "$indent</ul>\n";454 return $output;455 }456 457 function _page_list_element_start($output, $page, $depth, $current_page, $show_date, $date_format) {458 if ( $depth )459 $indent = str_repeat("\t", $depth);460 461 $css_class = 'page_item';462 if ( $page->ID == $current_page )463 $css_class .= ' current_page_item';464 465 $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . wp_specialchars($page->post_title) . '">' . $page->post_title . '</a>';466 467 if ( !empty($show_date) ) {468 if ( 'modified' == $show_date )469 $time = $page->post_modified;470 else471 $time = $page->post_date;472 473 $output .= " " . mysql2date($date_format, $time);474 }475 476 return $output;477 }478 479 function _page_list_element_end($output, $page, $depth) {480 $output .= "</li>\n";481 482 return $output;483 440 } 484 441
Note: See TracChangeset
for help on using the changeset viewer.