14 class Walker { |
14 class Walker { |
15 /** |
15 /** |
16 * What the class handles. |
16 * What the class handles. |
17 * |
17 * |
18 * @since 2.1.0 |
18 * @since 2.1.0 |
|
19 * @access public |
19 * @var string |
20 * @var string |
20 * @access public |
21 */ |
21 */ |
22 public $tree_type; |
22 var $tree_type; |
|
23 |
23 |
24 /** |
24 /** |
25 * DB fields to use. |
25 * DB fields to use. |
26 * |
26 * |
27 * @since 2.1.0 |
27 * @since 2.1.0 |
28 * @var array |
28 * @var array |
29 * @access protected |
29 */ |
30 */ |
30 public $db_fields; |
31 var $db_fields; |
|
32 |
31 |
33 /** |
32 /** |
34 * Max number of pages walked by the paged walker |
33 * Max number of pages walked by the paged walker |
35 * |
34 * |
36 * @since 2.7.0 |
35 * @since 2.7.0 |
37 * @var int |
36 * @var int |
38 * @access protected |
37 */ |
39 */ |
38 public $max_pages = 1; |
40 var $max_pages = 1; |
39 |
|
40 /** |
|
41 * Whether the current element has children or not. |
|
42 * |
|
43 * To be used in start_el(). |
|
44 * |
|
45 * @since 4.0.0 |
|
46 * @var bool |
|
47 */ |
|
48 public $has_children; |
41 |
49 |
42 /** |
50 /** |
43 * Starts the list before the elements are added. |
51 * Starts the list before the elements are added. |
44 * |
52 * |
45 * The $args parameter holds additional values that may be used with the child |
53 * The $args parameter holds additional values that may be used with the child |
50 * |
58 * |
51 * @param string $output Passed by reference. Used to append additional content. |
59 * @param string $output Passed by reference. Used to append additional content. |
52 * @param int $depth Depth of the item. |
60 * @param int $depth Depth of the item. |
53 * @param array $args An array of additional arguments. |
61 * @param array $args An array of additional arguments. |
54 */ |
62 */ |
55 function start_lvl( &$output, $depth = 0, $args = array() ) {} |
63 public function start_lvl( &$output, $depth = 0, $args = array() ) {} |
56 |
64 |
57 /** |
65 /** |
58 * Ends the list of after the elements are added. |
66 * Ends the list of after the elements are added. |
59 * |
67 * |
60 * The $args parameter holds additional values that may be used with the child |
68 * The $args parameter holds additional values that may be used with the child |
65 * |
73 * |
66 * @param string $output Passed by reference. Used to append additional content. |
74 * @param string $output Passed by reference. Used to append additional content. |
67 * @param int $depth Depth of the item. |
75 * @param int $depth Depth of the item. |
68 * @param array $args An array of additional arguments. |
76 * @param array $args An array of additional arguments. |
69 */ |
77 */ |
70 function end_lvl( &$output, $depth = 0, $args = array() ) {} |
78 public function end_lvl( &$output, $depth = 0, $args = array() ) {} |
71 |
79 |
72 /** |
80 /** |
73 * Start the element output. |
81 * Start the element output. |
74 * |
82 * |
75 * The $args parameter holds additional values that may be used with the child |
83 * The $args parameter holds additional values that may be used with the child |
82 * @param object $object The data object. |
90 * @param object $object The data object. |
83 * @param int $depth Depth of the item. |
91 * @param int $depth Depth of the item. |
84 * @param array $args An array of additional arguments. |
92 * @param array $args An array of additional arguments. |
85 * @param int $current_object_id ID of the current item. |
93 * @param int $current_object_id ID of the current item. |
86 */ |
94 */ |
87 function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {} |
95 public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {} |
88 |
96 |
89 /** |
97 /** |
90 * Ends the element output, if needed. |
98 * Ends the element output, if needed. |
91 * |
99 * |
92 * The $args parameter holds additional values that may be used with the child class methods. |
100 * The $args parameter holds additional values that may be used with the child class methods. |
97 * @param string $output Passed by reference. Used to append additional content. |
105 * @param string $output Passed by reference. Used to append additional content. |
98 * @param object $object The data object. |
106 * @param object $object The data object. |
99 * @param int $depth Depth of the item. |
107 * @param int $depth Depth of the item. |
100 * @param array $args An array of additional arguments. |
108 * @param array $args An array of additional arguments. |
101 */ |
109 */ |
102 function end_el( &$output, $object, $depth = 0, $args = array() ) {} |
110 public function end_el( &$output, $object, $depth = 0, $args = array() ) {} |
103 |
111 |
104 /** |
112 /** |
105 * Traverse elements to create list from elements. |
113 * Traverse elements to create list from elements. |
106 * |
114 * |
107 * Display one element if the element doesn't have any children otherwise, |
115 * Display one element if the element doesn't have any children otherwise, |
119 * @param int $depth Depth of current element. |
127 * @param int $depth Depth of current element. |
120 * @param array $args An array of arguments. |
128 * @param array $args An array of arguments. |
121 * @param string $output Passed by reference. Used to append additional content. |
129 * @param string $output Passed by reference. Used to append additional content. |
122 * @return null Null on failure with no changes to parameters. |
130 * @return null Null on failure with no changes to parameters. |
123 */ |
131 */ |
124 function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
132 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
125 |
133 |
126 if ( !$element ) |
134 if ( !$element ) |
127 return; |
135 return; |
128 |
136 |
129 $id_field = $this->db_fields['id']; |
137 $id_field = $this->db_fields['id']; |
|
138 $id = $element->$id_field; |
130 |
139 |
131 //display this element |
140 //display this element |
132 if ( isset( $args[0] ) && is_array( $args[0] ) ) |
141 $this->has_children = ! empty( $children_elements[ $id ] ); |
133 $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] ); |
142 if ( isset( $args[0] ) && is_array( $args[0] ) ) { |
|
143 $args[0]['has_children'] = $this->has_children; // Backwards compatibility. |
|
144 } |
|
145 |
134 $cb_args = array_merge( array(&$output, $element, $depth), $args); |
146 $cb_args = array_merge( array(&$output, $element, $depth), $args); |
135 call_user_func_array(array($this, 'start_el'), $cb_args); |
147 call_user_func_array(array($this, 'start_el'), $cb_args); |
136 |
|
137 $id = $element->$id_field; |
|
138 |
148 |
139 // descend only when the depth is right and there are childrens for this element |
149 // descend only when the depth is right and there are childrens for this element |
140 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) { |
150 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) { |
141 |
151 |
142 foreach( $children_elements[ $id ] as $child ){ |
152 foreach( $children_elements[ $id ] as $child ){ |
176 * |
186 * |
177 * @param array $elements An array of elements. |
187 * @param array $elements An array of elements. |
178 * @param int $max_depth The maximum hierarchical depth. |
188 * @param int $max_depth The maximum hierarchical depth. |
179 * @return string The hierarchical item output. |
189 * @return string The hierarchical item output. |
180 */ |
190 */ |
181 function walk( $elements, $max_depth) { |
191 public function walk( $elements, $max_depth) { |
182 |
192 |
183 $args = array_slice(func_get_args(), 2); |
193 $args = array_slice(func_get_args(), 2); |
184 $output = ''; |
194 $output = ''; |
185 |
195 |
186 if ($max_depth < -1) //invalid parameter |
196 if ($max_depth < -1) //invalid parameter |
187 return $output; |
197 return $output; |
188 |
198 |
189 if (empty($elements)) //nothing to walk |
199 if (empty($elements)) //nothing to walk |
190 return $output; |
200 return $output; |
191 |
201 |
192 $id_field = $this->db_fields['id']; |
|
193 $parent_field = $this->db_fields['parent']; |
202 $parent_field = $this->db_fields['parent']; |
194 |
203 |
195 // flat display |
204 // flat display |
196 if ( -1 == $max_depth ) { |
205 if ( -1 == $max_depth ) { |
197 $empty_array = array(); |
206 $empty_array = array(); |
265 * |
274 * |
266 * @param int $max_depth The maximum hierarchical depth. |
275 * @param int $max_depth The maximum hierarchical depth. |
267 * @param int $page_num The specific page number, beginning with 1. |
276 * @param int $page_num The specific page number, beginning with 1. |
268 * @return string XHTML of the specified page of elements |
277 * @return string XHTML of the specified page of elements |
269 */ |
278 */ |
270 function paged_walk( $elements, $max_depth, $page_num, $per_page ) { |
279 public function paged_walk( $elements, $max_depth, $page_num, $per_page ) { |
271 |
280 |
272 /* sanity check */ |
281 /* sanity check */ |
273 if ( empty($elements) || $max_depth < -1 ) |
282 if ( empty($elements) || $max_depth < -1 ) |
274 return ''; |
283 return ''; |
275 |
284 |
276 $args = array_slice( func_get_args(), 4 ); |
285 $args = array_slice( func_get_args(), 4 ); |
277 $output = ''; |
286 $output = ''; |
278 |
287 |
279 $id_field = $this->db_fields['id']; |
|
280 $parent_field = $this->db_fields['parent']; |
288 $parent_field = $this->db_fields['parent']; |
281 |
289 |
282 $count = -1; |
290 $count = -1; |
283 if ( -1 == $max_depth ) |
291 if ( -1 == $max_depth ) |
284 $total_top = count( $elements ); |
292 $total_top = count( $elements ); |