-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject_Nav_Walker.php
More file actions
291 lines (256 loc) · 9.26 KB
/
Copy pathObject_Nav_Walker.php
File metadata and controls
291 lines (256 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Tribe\Libs\Nav;
use Walker;
abstract class Object_Nav_Walker extends Walker {
/**
* What the class handles.
*
* @var string[]
*
* @see Walker::$tree_type
*
* @phpstan-ignore-next-line
*/
public $tree_type = [ 'post_type', 'taxonomy', 'custom' ];
/**
* Database fields to use.
*
* @var string[]
*
* @see Walker::$db_fields
*/
public $db_fields = [ 'parent' => 'menu_item_parent', 'id' => 'db_id' ];
/**
* Traverse elements to create list from elements.
*
* Display one element if the element doesn't have any children otherwise,
* display the element and its children. Will only traverse up to the max
* depth and no ignore elements under that depth. It is possible to set the
* max depth to include all depths, see walk() method.
*
* This method should not be called directly, use the walk() method instead.
*
* @param object $element Data object.
* @param array $children_elements List of elements to continue traversing.
* @param int $max_depth Max depth to traverse.
* @param int $depth Depth of current element.
* @param array $args An array of arguments.
* @param string|array $output Passed by reference. Used to append additional content.
*
* @return void Null on failure with no changes to parameters.
* @since 2.5.0
*
*/
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( ! $element ) {
return;
}
if ( ! is_array( $output ) ) {
$output = [];
}
$id_field = $this->db_fields['id'];
//display this element
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
}
$cb_args = array_merge( [&$output, $element, $depth], $args);
call_user_func_array([$this, 'start_el'], $cb_args);
$id = $element->$id_field;
$classes = empty( $element->classes ) ? [] : (array) $element->classes;
$classes[] = 'menu-item-' . $element->ID;
$classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $element, $args[0], $depth );
$output['menu_id'] = $id;
if ( !isset( $element->post_type ) || $element->post_type !== 'nav_menu_item' ) {
if ( $element->type === 'post_type' ) {
$output['menu_id'] = $element->post_type . '-' . $output['menu_id'];
} elseif ( $element->type === 'taxonomy' ) {
$output['menu_id'] = $element->taxonomy . '-' . $output['menu_id'];
} else {
$output['menu_id'] = $element->type . '-' . $output['menu_id'];
}
}
$output['classes'] = implode( ' ', array_merge( $output['classes'] ?? [], $classes ) );
// descend only when the depth is right and there are children for this element
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
$output['has_children'] = true;
foreach ( $children_elements[ $id ] as $child ) {
$child_array = [];
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $child_array );
$output['menu_items'][] = $child_array;
}
unset( $children_elements[ $id ] );
} else {
$output['has_children'] = false;
}
}
/**
* Start the element output.
*
* The $args parameter holds additional values that may be used with the child
* class methods. Includes the element output also.
*
* @param string|array $output Passed by reference. Used to append additional content.
* @param object $data_object The data object.
* @param int $depth Depth of the item.
* @param \stdClass|null|array $args An object of additional arguments.
* @param int $current_object_id ID of the current item.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
$item = $data_object;
if ( !is_array($output) ) {
$output = [];
}
$id_field = $this->db_fields['id'];
remove_filter( 'the_title', 'wptexturize' );
$output['label'] = apply_filters( 'the_title', $item->title, $item->ID );
if ( !empty( $args->numeric_ids ) ) {
$output['id'] = apply_filters( 'nav_menu_item_id', $item->ID, $item, $args, $depth );
} else {
$output['id'] = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
}
$output['excerpt'] = $item->description;
$output['summary_link_label'] = apply_filters( 'summary_link_label', $item->summary_link_label, $item->ID );
if ( isset( $item->post_type ) && $item->post_type == 'nav_menu_item' ) { // only get classes if we're looking at a nav menu item
$classes = get_post_meta( $item->$id_field, '_menu_item_classes', true );
}
if ( !empty($classes) && is_array($classes) ) {
$output['classes'] = $classes;
}
foreach ( [ 'date', 'time', 'datetime', 'timestamp'] as $time_key ) {
if ( !empty($item->$time_key) ) {
$output[$time_key] = $item->$time_key;
}
}
$atts = [];
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['url'] = ! empty( $item->url ) ? $item->url : '';
/**
* Filter the HTML attributes applied to a menu item's <a>.
*
* @param array $atts {
* The HTML attributes applied to the menu item's <a>, empty strings are ignored.
*
* @type string $title Title attribute.
* @type string $target Target attribute.
* @type string $rel The rel attribute.
* @type string $href The href attribute.
* }
*
* @param object $item The current menu item.
* @param \stdClass $args An array of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*
* @see wp_nav_menu()
*
* @since 3.6.0
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
foreach ( $atts as $key => $value ) {
$output[$key] = $value;
}
$output = array_filter( $output );
$output = apply_filters( 'object_nav_walker_start_el', $output, $item, $depth, $args );
}
/**
* Display array of elements hierarchically.
*
* Does not assume any existing order of elements.
*
* $max_depth = -1 means flatly display every element.
* $max_depth = 0 means display all levels.
* $max_depth > 0 specifies the number of display levels.
*
* @param array $elements An array of elements.
* @param int $max_depth The maximum hierarchical depth.
* @param mixed ...$args Optional additional arguments.
*
* @return array|array[]|string
*/
public function walk( $elements, $max_depth, ...$args ) {
$menu_items = [];
if ($max_depth < -1) { //invalid parameter
return $this->format_output($menu_items);
}
if (empty($elements)) { //nothing to walk
return $this->format_output($menu_items);
}
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = [];
foreach ( $elements as $e ) {
$item = [];
$this->display_element( $e, $empty_array, 1, 0, $args, $item );
$menu_items[] = $item;
}
return $this->format_output($menu_items);
}
/*
* Need to display in hierarchical order.
* Separate elements into two buckets: top level and children elements.
* Children_elements is two dimensional array, eg.
* Children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = [];
$children_elements = [];
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field ) {
$top_level_elements[] = $e;
} else {
$children_elements[ $e->$parent_field ][] = $e;
}
}
/*
* When none of the elements is top level.
* Assume the first one must be root of the sub elements.
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = [];
$children_elements = [];
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field ) {
$top_level_elements[] = $e;
} else {
$children_elements[ $e->$parent_field ][] = $e;
}
}
}
foreach ( $top_level_elements as $e ) {
$item = [];
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $item );
$menu_items[] = $item;
}
/*
* If we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless.
*/
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = [];
foreach ( $children_elements as $orphans ) {
foreach ( $orphans as $op ) {
$item = [];
$this->display_element( $op, $empty_array, 1, 0, $args, $item );
$menu_items[] = $item;
}
}
}
return $this->format_output($menu_items);
}
/**
* This is the reason the class is abstract. walk() expects this
* method to return a string, but it gives an array. Subclasses
* must override this to format to a string before returning.
*
* @param array $menu_items
* @return array
*/
protected function format_output( $menu_items ) {
return [
'menu_items' => $menu_items,
];
}
}