1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class for handling menu functionality. |
4
|
|
|
* |
5
|
|
|
* @package Classy |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Classy; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Menu. |
12
|
|
|
*/ |
13
|
|
|
class Menu { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Menu ID. |
17
|
|
|
* |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
public $ID; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Menu title. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $title; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Items. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $items; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Main constructor. Tries to find menu id based on provided arg (or not) and inits menu. |
38
|
|
|
* |
39
|
|
|
* @param string $arg It can be menu id, slug or full name. |
40
|
|
|
*/ |
41
|
|
|
public function __construct( $arg = null ) { |
42
|
|
|
$locations = get_nav_menu_locations(); |
43
|
|
|
|
44
|
|
|
if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) { |
45
|
|
|
$menu_id = $this->check_menu_id( $arg ); |
46
|
|
|
} elseif ( is_array( $locations ) && count( $locations ) ) { |
47
|
|
|
$menu_id = $this->get_locations_menu_id( $locations, $arg ); |
48
|
|
|
} elseif ( is_string( $arg ) ) { |
49
|
|
|
$menu_id = $this->get_menu_id_by_name( $arg ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ( ! isset( $menu_id ) ) { |
53
|
|
|
$menu_id = $this->get_first_menu_id(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( $menu_id ) { |
57
|
|
|
$this->ID = $menu_id; |
|
|
|
|
58
|
|
|
$this->title = $this->get_menu_name_by_id($menu_id); |
|
|
|
|
59
|
|
|
$this->init(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Init menu. |
65
|
|
|
*/ |
66
|
|
|
protected function init() { |
67
|
|
|
$_return = array(); |
68
|
|
|
$items = wp_get_nav_menu_items( $this->ID ); |
69
|
|
|
|
70
|
|
|
foreach ( $items as $item ) { |
71
|
|
|
$_return[ $item->ID ] = new Menu_Item( $item ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Apply nesting. |
75
|
|
|
foreach ( $_return as $item_id => $item ) { |
76
|
|
|
if ( |
77
|
|
|
isset( $item->menu_item_parent ) && |
78
|
|
|
$item->menu_item_parent && |
79
|
|
|
isset( $_return[ $item->menu_item_parent ] ) |
80
|
|
|
) { |
81
|
|
|
$_return[ $item->menu_item_parent ]->add_child( $item ); |
82
|
|
|
unset( $_return[ $item_id ] ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->items = $_return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns first menu id or false if there are no menus. |
91
|
|
|
* |
92
|
|
|
* @return int|bool |
93
|
|
|
*/ |
94
|
|
|
protected function get_first_menu_id() { |
95
|
|
|
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
96
|
|
|
|
97
|
|
|
if ( is_array( $menus ) && count( $menus ) ) { |
98
|
|
|
if ( isset( $menus[0]->term_id ) ) { |
99
|
|
|
return $menus[0]->term_id; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Performs menu id lookup inside navigation Menus. |
108
|
|
|
* |
109
|
|
|
* @param array $locations locations that are returned by get_nav_menu_locations(). |
110
|
|
|
* @param int|string $arg navigation id. |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
protected function get_locations_menu_id( $locations, $arg ) { |
114
|
|
|
if ( is_numeric( $arg ) ) { |
115
|
|
|
$arg = array_search( $arg, $locations ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ( isset( $locations[ $arg ] ) ) { |
119
|
|
|
$menu_id = $locations[ $arg ]; |
120
|
|
|
|
121
|
|
|
return $menu_id; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Checks if the provided menu id exists. |
127
|
|
|
* |
128
|
|
|
* @param int $menu_id Menu ID. |
129
|
|
|
* |
130
|
|
|
* @return int|boolean |
131
|
|
|
*/ |
132
|
|
|
protected function check_menu_id( $menu_id ) { |
133
|
|
|
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
134
|
|
|
|
135
|
|
|
if ( is_array( $menus ) && count( $menus ) ) { |
136
|
|
|
foreach ( $menus as $menu ) { |
137
|
|
|
if ( absint( $menu->term_id ) === absint( $menu_id ) ) { |
138
|
|
|
return $menu_id; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns menu id by menu name/slug. |
148
|
|
|
* |
149
|
|
|
* @param string $slug Menu's name. |
150
|
|
|
* |
151
|
|
|
* @return int|bool |
152
|
|
|
*/ |
153
|
|
|
protected function get_menu_id_by_name( $slug = null ) { |
154
|
|
|
if ( $slug && is_string( $slug ) ) { |
|
|
|
|
155
|
|
|
if ( $menu_id = get_term_by( 'slug', $slug, 'nav_menu' ) ) { |
156
|
|
|
return $menu_id; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ( $menu_id = get_term_by( 'name', $slug, 'nav_menu' ) ) { |
160
|
|
|
return $menu_id; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns menu name by menu id. |
169
|
|
|
* |
170
|
|
|
* @param string $name Menu's name. |
|
|
|
|
171
|
|
|
* |
172
|
|
|
* @return int|bool |
173
|
|
|
*/ |
174
|
|
|
protected function get_menu_name_by_id( $id = null ) { |
175
|
|
|
if ( $id && is_int( $id ) ) { |
176
|
|
|
$menu_obj = wp_get_nav_menu_object($id); |
177
|
|
|
|
178
|
|
|
if ( $menu_obj ) { |
179
|
|
|
return $menu_obj->name; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns menu items. |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function get_items() { |
193
|
|
|
return $this->items; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.