class-wpml-admin-menu-item.php
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Admin_Menu_Item {
private $capability;
private $function;
private $menu_slug;
private $menu_title;
private $order;
private $page_title;
private $parent_slug;
/**
* WPML_Menu_Item constructor.
*
* @param array $args
*
* @throws \InvalidArgumentException
*/
public function __construct( array $args = null ) {
if ( $args ) {
$required_fields = array(
'capability',
'menu_slug',
'menu_title',
'page_title',
);
foreach ( $required_fields as $required_field ) {
if ( ! array_key_exists( $required_field, $args ) ) {
throw new InvalidArgumentException( $required_field . ' is missing.' );
}
}
$fields = array(
'function',
'order',
'parent_slug',
);
$fields = array_merge( $required_fields, $fields );
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $args ) ) {
$this->{$field} = $args[ $field ];
}
}
}
}
/**
* Required by `usort` to remove duplicates, as casts array elements to string
*
* @return string
*/
public function __toString() {
return $this->serialize();
}
public function build( $root_slug ) {
$parent = $root_slug;
if ( $this->get_parent_slug() ) {
$parent = $this->get_parent_slug();
}
add_submenu_page(
$parent,
$this->get_page_title(),
$this->get_menu_title(),
$this->get_capability(),
$this->get_menu_slug(),
$this->get_function()
);
}
/**
* @return mixed
*/
public function get_parent_slug() {
return $this->parent_slug;
}
/**
* @param mixed $parent_slug
*/
public function set_parent_slug( $parent_slug ) {
$this->parent_slug = $parent_slug;
}
/**
* @return mixed
*/
public function get_page_title() {
return $this->page_title;
}
/**
* @param mixed $page_title
*/
public function set_page_title( $page_title ) {
$this->page_title = $page_title;
}
/**
* @return mixed
*/
public function get_menu_title() {
return $this->menu_title;
}
/**
* @param mixed $menu_title
*/
public function set_menu_title( $menu_title ) {
$this->menu_title = $menu_title;
}
/**
* @return mixed
*/
public function get_capability() {
return $this->capability;
}
/**
* @param mixed $capability
*/
public function set_capability( $capability ) {
$this->capability = $capability;
}
/**
* @return mixed
*/
public function get_menu_slug() {
return $this->menu_slug;
}
/**
* @param mixed $menu_slug
*/
public function set_menu_slug( $menu_slug ) {
$this->menu_slug = $menu_slug;
}
/**
* @return mixed
*/
public function get_function() {
return $this->function;
}
/**
* @param mixed $function
*/
public function set_function( $function ) {
$this->function = $function;
}
/**
* @return mixed
*/
public function get_order() {
return $this->order;
}
/**
* @param mixed $order
*/
public function set_order( $order ) {
$this->order = $order;
}
/**
* @return string
*/
private function serialize() {
$function = $this->get_function();
if ( is_callable( $function ) ) {
/**
* "Hash" is for the hash table. That's not an actual hash of the callable, but it should
* be good enough for the scope of this function
*/
$function = spl_object_hash( (object) $function );
}
return wp_json_encode(
array(
'capability' => $this->get_capability(),
'function' => $function,
'menu_slug' => $this->get_menu_slug(),
'menu_title' => $this->get_menu_title(),
'order' => $this->get_order(),
'page_title' => $this->get_page_title(),
'parent_slug' => $this->get_parent_slug(),
),
0,
1
);
}
}