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