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