1
|
|
|
<?php |
2
|
|
|
namespace Classy; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class for handling menu functionality |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class Menu { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Menu ID |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
public $ID; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Items |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $items; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Main constructor. Tries to find menu id based on provided arg (or not) and inits menu |
24
|
|
|
* |
25
|
|
|
* @param string $arg it can be menu id, slug or full name |
26
|
|
|
*/ |
27
|
|
|
public function __construct( $arg = null ) { |
28
|
|
|
|
29
|
|
|
if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) { |
30
|
|
|
|
31
|
|
|
$menu_id = $this->check_menu_id( $arg ); |
32
|
|
|
|
33
|
|
|
} elseif ( is_string( $arg ) ) { |
34
|
|
|
|
35
|
|
|
$menu_id = $this->get_menu_id_by_name( $arg ); |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ( ! isset( $menu_id ) ) { |
40
|
|
|
|
41
|
|
|
$menu_id = $this->get_first_menu_id(); |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ( $menu_id ) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->ID = $menu_id; |
48
|
|
|
|
49
|
|
|
$this->init(); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Inits menu |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function init() { |
61
|
|
|
|
62
|
|
|
$_return = array(); |
63
|
|
|
|
64
|
|
|
$items = wp_get_nav_menu_items( $this->ID ); |
65
|
|
|
|
66
|
|
|
foreach ( $items as $item ) { |
67
|
|
|
|
68
|
|
|
$_return[ $item->ID ] = new Menu_Item( $item ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Apply nesting |
72
|
|
|
|
73
|
|
|
foreach ( $_return as $item_id => $item ) { |
74
|
|
|
|
75
|
|
|
if ( isset( $item->menu_item_parent ) && $item->menu_item_parent && isset( $_return[ $item->menu_item_parent ] ) ) { |
76
|
|
|
|
77
|
|
|
$_return[ $item->menu_item_parent ]->add_child( $item ); |
78
|
|
|
|
79
|
|
|
unset( $_return[ $item_id ] ); |
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
|
|
|
return false; |
|
|
|
|
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Checks if the provided menu id exists |
112
|
|
|
* |
113
|
|
|
* @param int $menu_id |
114
|
|
|
* @return int|boolean |
115
|
|
|
*/ |
116
|
|
|
protected function check_menu_id( $menu_id ) { |
117
|
|
|
|
118
|
|
|
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
119
|
|
|
|
120
|
|
|
if ( is_array( $menus ) && count( $menus ) ) { |
121
|
|
|
|
122
|
|
|
foreach ( $menus as $menu ) { |
123
|
|
|
|
124
|
|
|
if ( absint( $menu->term_id ) === absint( $menu_id ) ) { |
125
|
|
|
|
126
|
|
|
return $menu_id; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns menu id by menu name/slug |
138
|
|
|
* |
139
|
|
|
* @param string $slug |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
protected function get_menu_id_by_name( $slug = null ) { |
143
|
|
|
|
144
|
|
|
if ( $slug && is_string( $slug ) ) { |
|
|
|
|
145
|
|
|
|
146
|
|
|
$menu_id = get_term_by( 'slug', $slug, 'nav_menu' ); |
147
|
|
|
|
148
|
|
|
if ( $menu_id ) { return $menu_id; } |
149
|
|
|
|
150
|
|
|
$menu_id = get_term_by( 'name', $slug, 'nav_menu' ); |
151
|
|
|
|
152
|
|
|
if ( $menu_id ) { return $menu_id; } |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
|
|
|
|
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns menu items |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function get_items() { |
165
|
|
|
|
166
|
|
|
return $this->items; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: