1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class for handling menu item functionality |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
class ClassyMenuItem extends ClassyBasis { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Children |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $children = array(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CSS Classes |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $classes = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* If item has child |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected $has_child = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Nesting level |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
public $level = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Item title |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $title; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Checks if provided arg is instance of WP_Post and inits it |
41
|
|
|
* |
42
|
|
|
* @param WP_Post $item |
43
|
|
|
*/ |
44
|
|
|
public function __construct($item) { |
45
|
|
|
|
46
|
|
|
if (is_a($item, 'WP_Post')) { |
47
|
|
|
|
48
|
|
|
$this->import($item); |
49
|
|
|
$this->filter_classes(); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns item title |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function get_title() { |
61
|
|
|
|
62
|
|
|
return $this->title; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns item slug |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function get_slug() { |
72
|
|
|
|
73
|
|
|
return $this->post_name; |
|
|
|
|
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns item link (url) |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function get_link() { |
83
|
|
|
|
84
|
|
|
return $this->url; |
|
|
|
|
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retuns item children, if there are any |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function get_children() { |
94
|
|
|
|
95
|
|
|
return $this->children; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns menu item classes |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function get_classes() { |
104
|
|
|
|
105
|
|
|
return implode(' ', $this->classes); |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds css class to classes array |
111
|
|
|
* |
112
|
|
|
* @param string $class_name |
113
|
|
|
*/ |
114
|
|
|
public function add_class($class_name) { |
115
|
|
|
|
116
|
|
|
$this->classes[] = $class_name; |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Adds child to current ClassyMenuItem |
122
|
|
|
* |
123
|
|
|
* @param ClassyMenuItem $item |
124
|
|
|
*/ |
125
|
|
|
public function add_child($item) { |
126
|
|
|
|
127
|
|
|
if ( !$this->has_child ) { |
128
|
|
|
$this->add_class( 'menu-item-has-children' ); |
129
|
|
|
$this->has_child = true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ( !isset( $this->children ) ) { |
133
|
|
|
$this->children = array(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->children[] = $item; |
137
|
|
|
$item->level = $this->level + 1; |
138
|
|
|
|
139
|
|
|
if ($item->children) { |
|
|
|
|
140
|
|
|
$this->update_child_levels(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Applies filters for item classes |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
protected function filter_classes() { |
151
|
|
|
|
152
|
|
|
$this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this ); |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Updates children nesting level param |
158
|
|
|
* |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
|
|
protected function update_child_levels() { |
162
|
|
|
|
163
|
|
|
if (is_array($this->children)) { |
164
|
|
|
|
165
|
|
|
foreach( $this->children as $child ) { |
166
|
|
|
$child->level = $this->level + 1; |
167
|
|
|
$child->update_child_levels(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
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.