Opened 20 years ago
Closed 20 years ago
#2593 closed defect (bug) (fixed)
The walk_tree family should be a class
| Reported by: | davidhouse | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Administration | Version: | 2.1 |
| Severity: | normal | Keywords: | bg|has-patch |
| Cc: | Focuses: |
Description
Here's a patch that refactors the new walk_tree() functions into a class. Extend the class to create a walker (examples included of categories and pages). Create an instance and call walk() to return the output, then echo or whatever.
Just look at the code :)
Attachments (2)
Change History (8)
#2
@
20 years ago
That was a crap explanation. Lets go again.
walk_tree() is a nice new generic function that got added to be the core a few weeks ago. It's a kind of generic pretty-printer for tree-like structures. For example, we used to have list_cats() and wp_list_pages(), both of which printed a hierarchial structure out (the category list for the former, the page list for the latter). walk_tree() generalises this by allowing you to pass in some callbacks that will get called when starting a new element, finishing an element, or ascending or descending a level.
Callbacks are ugly when abused like this, so this is how my class does it: suppose you want to print some hierarchial tree. Create a new class which extends Walker and defines the following methods:
start_el()end_el()start_lvl()end_lvl()
The names should be fairly self-explanatory. You'll also need to define $dbfields, an associative array:
$dbfields['parent']should be the DB field which is the parent of any given record. This ispost_parentfor posts andcategory_parentfor cats.$dbfields['id']is the same but for the ID. E.g.IDfor posts andcat_IDfor cats.
Once that's done, create an instance and call $instance->walk() passing in the elements you want to print (in object form), and the maximum depth (or -1 to just print flat, no hierarchy). Any extra parameters will get passed to your callbacks.
There are examples in the code.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
More stuff should probably be converted to use this, for example:
Might be nice to build some paging into it as well.