1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class for handling menu functionality |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
class ClassyMenu { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Menu ID |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
public $ID; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Items |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $items; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Main constructor. Tries to find menu id based on provided arg (or not) and inits menu |
23
|
|
|
* |
24
|
|
|
* @param string $arg it can be menu id, slug or full name |
25
|
|
|
*/ |
26
|
|
|
public function __construct($arg = null) { |
27
|
|
|
|
28
|
|
|
if (is_numeric($arg) && $arg != 0) { |
29
|
|
|
|
30
|
|
|
$menu_id = $this->check_menu_id($arg); |
31
|
|
|
|
32
|
|
|
} elseif (is_string($arg)) { |
33
|
|
|
|
34
|
|
|
$menu_id = $this->get_menu_id_by_name($arg); |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!isset($menu_id)) { |
39
|
|
|
|
40
|
|
|
$menu_id = $this->get_first_menu_id(); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($menu_id) { |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->ID = $menu_id; |
47
|
|
|
|
48
|
|
|
$this->init(); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Inits menu |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function init() { |
60
|
|
|
|
61
|
|
|
$_return = array(); |
62
|
|
|
|
63
|
|
|
$items = wp_get_nav_menu_items($this->ID); |
64
|
|
|
|
65
|
|
|
foreach ($items as $item) { |
66
|
|
|
|
67
|
|
|
$_return[$item->ID] = new ClassyMenuItem($item); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Apply nesting |
71
|
|
|
|
72
|
|
|
foreach ($_return as $item_id => $item) { |
73
|
|
|
|
74
|
|
|
if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($_return[$item->menu_item_parent])) { |
75
|
|
|
|
76
|
|
|
$_return[$item->menu_item_parent]->add_child($item); |
77
|
|
|
|
78
|
|
|
unset($_return[$item_id]); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->items = $_return; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retuns first menu id or false if there are no menus |
90
|
|
|
* |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
protected function get_first_menu_id() { |
94
|
|
|
|
95
|
|
|
$menus = get_terms('nav_menu', array('hide_empty' => true)); |
96
|
|
|
|
97
|
|
|
if (is_array($menus) && count($menus)) { |
98
|
|
|
|
99
|
|
|
if (isset($menus[0]->term_id)) { |
100
|
|
|
|
101
|
|
|
return $menus[0]->term_id; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks if the provided menu id exists |
113
|
|
|
* |
114
|
|
|
* @param int $menu_id |
115
|
|
|
* @return int/boolean |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
protected function check_menu_id($menu_id) { |
118
|
|
|
|
119
|
|
|
$menus = get_terms('nav_menu', array('hide_empty' => true)); |
120
|
|
|
|
121
|
|
|
if (is_array($menus) && count($menus)) { |
122
|
|
|
|
123
|
|
|
foreach ($menus as $menu) { |
124
|
|
|
|
125
|
|
|
if ($menu->term_id == $menu_id) { |
126
|
|
|
|
127
|
|
|
return $menu_id; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns menu id by menu name/slug |
141
|
|
|
* |
142
|
|
|
* @param string $slug |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
|
|
protected function get_menu_id_by_name($slug = null) { |
146
|
|
|
|
147
|
|
|
if ($slug && is_string($slug)) { |
|
|
|
|
148
|
|
|
|
149
|
|
|
$menu_id = get_term_by('slug', $slug, 'nav_menu'); |
150
|
|
|
|
151
|
|
|
if ($menu_id) return $menu_id; |
152
|
|
|
|
153
|
|
|
$menu_id = get_term_by('name', $slug, 'nav_menu'); |
154
|
|
|
|
155
|
|
|
if ($menu_id) return $menu_id; |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
|
|
|
|
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns menu items |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function get_items() { |
169
|
|
|
|
170
|
|
|
return $this->items; |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.