0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* The WordPress Toolbar |
|
4 |
* |
|
5 |
* @since 3.1.0 |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Toolbar |
|
9 |
*/ |
|
10 |
class WP_Admin_Bar { |
|
11 |
private $nodes = array(); |
|
12 |
private $bound = false; |
|
13 |
public $user; |
|
14 |
|
|
15 |
public function __get( $name ) { |
|
16 |
switch ( $name ) { |
|
17 |
case 'proto' : |
|
18 |
return is_ssl() ? 'https://' : 'http://'; |
5
|
19 |
|
0
|
20 |
case 'menu' : |
|
21 |
_deprecated_argument( 'WP_Admin_Bar', '3.3', 'Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node(), not the <code>menu</code> property.' ); |
|
22 |
return array(); // Sorry, folks. |
|
23 |
} |
|
24 |
} |
|
25 |
|
|
26 |
public function initialize() { |
|
27 |
$this->user = new stdClass; |
|
28 |
|
|
29 |
if ( is_user_logged_in() ) { |
|
30 |
/* Populate settings we need for the menu based on the current user. */ |
|
31 |
$this->user->blogs = get_blogs_of_user( get_current_user_id() ); |
|
32 |
if ( is_multisite() ) { |
|
33 |
$this->user->active_blog = get_active_blog_for_user( get_current_user_id() ); |
|
34 |
$this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) ); |
|
35 |
$this->user->account_domain = $this->user->domain; |
|
36 |
} else { |
|
37 |
$this->user->active_blog = $this->user->blogs[get_current_blog_id()]; |
|
38 |
$this->user->domain = trailingslashit( home_url() ); |
|
39 |
$this->user->account_domain = $this->user->domain; |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
43 |
add_action( 'wp_head', 'wp_admin_bar_header' ); |
|
44 |
|
|
45 |
add_action( 'admin_head', 'wp_admin_bar_header' ); |
|
46 |
|
|
47 |
if ( current_theme_supports( 'admin-bar' ) ) { |
|
48 |
/** |
|
49 |
* To remove the default padding styles from WordPress for the Toolbar, use the following code: |
|
50 |
* add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) ); |
|
51 |
*/ |
|
52 |
$admin_bar_args = get_theme_support( 'admin-bar' ); |
|
53 |
$header_callback = $admin_bar_args[0]['callback']; |
|
54 |
} |
|
55 |
|
|
56 |
if ( empty($header_callback) ) |
|
57 |
$header_callback = '_admin_bar_bump_cb'; |
|
58 |
|
|
59 |
add_action('wp_head', $header_callback); |
|
60 |
|
|
61 |
wp_enqueue_script( 'admin-bar' ); |
|
62 |
wp_enqueue_style( 'admin-bar' ); |
|
63 |
|
|
64 |
/** |
|
65 |
* Fires after WP_Admin_Bar is initialized. |
|
66 |
* |
|
67 |
* @since 3.1.0 |
|
68 |
*/ |
|
69 |
do_action( 'admin_bar_init' ); |
|
70 |
} |
|
71 |
|
|
72 |
public function add_menu( $node ) { |
|
73 |
$this->add_node( $node ); |
|
74 |
} |
|
75 |
|
|
76 |
public function remove_menu( $id ) { |
|
77 |
$this->remove_node( $id ); |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Add a node to the menu. |
|
82 |
* |
5
|
83 |
* @param array $args { |
|
84 |
* Arguments for adding a node. |
|
85 |
* |
|
86 |
* @type string $id ID of the item. |
|
87 |
* @type string $title Title of the node. |
|
88 |
* @type string $parent Optional. ID of the parent node. |
|
89 |
* @type string $href Optional. Link for the item. |
|
90 |
* @type bool $group Optional. Whether or not the node is a group. Default false. |
|
91 |
* @type array $meta Meta data including the following keys: 'html', 'class', 'rel', |
|
92 |
* 'onclick', 'target', 'title', 'tabindex'. Default empty. |
|
93 |
* } |
0
|
94 |
*/ |
|
95 |
public function add_node( $args ) { |
|
96 |
// Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) |
|
97 |
if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) |
|
98 |
$args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); |
|
99 |
|
|
100 |
if ( is_object( $args ) ) |
|
101 |
$args = get_object_vars( $args ); |
|
102 |
|
|
103 |
// Ensure we have a valid title. |
|
104 |
if ( empty( $args['id'] ) ) { |
|
105 |
if ( empty( $args['title'] ) ) |
|
106 |
return; |
|
107 |
|
|
108 |
_doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' ); |
|
109 |
// Deprecated: Generate an ID from the title. |
|
110 |
$args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) ); |
|
111 |
} |
|
112 |
|
|
113 |
$defaults = array( |
|
114 |
'id' => false, |
|
115 |
'title' => false, |
|
116 |
'parent' => false, |
|
117 |
'href' => false, |
|
118 |
'group' => false, |
|
119 |
'meta' => array(), |
|
120 |
); |
|
121 |
|
|
122 |
// If the node already exists, keep any data that isn't provided. |
|
123 |
if ( $maybe_defaults = $this->get_node( $args['id'] ) ) |
|
124 |
$defaults = get_object_vars( $maybe_defaults ); |
|
125 |
|
|
126 |
// Do the same for 'meta' items. |
|
127 |
if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) |
|
128 |
$args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] ); |
|
129 |
|
|
130 |
$args = wp_parse_args( $args, $defaults ); |
|
131 |
|
|
132 |
$back_compat_parents = array( |
|
133 |
'my-account-with-avatar' => array( 'my-account', '3.3' ), |
|
134 |
'my-blogs' => array( 'my-sites', '3.3' ), |
|
135 |
); |
|
136 |
|
|
137 |
if ( isset( $back_compat_parents[ $args['parent'] ] ) ) { |
|
138 |
list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ]; |
|
139 |
_deprecated_argument( __METHOD__, $version, sprintf( 'Use <code>%s</code> as the parent for the <code>%s</code> admin bar node instead of <code>%s</code>.', $new_parent, $args['id'], $args['parent'] ) ); |
|
140 |
$args['parent'] = $new_parent; |
|
141 |
} |
|
142 |
|
|
143 |
$this->_set_node( $args ); |
|
144 |
} |
|
145 |
|
|
146 |
final protected function _set_node( $args ) { |
|
147 |
$this->nodes[ $args['id'] ] = (object) $args; |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* Gets a node. |
|
152 |
* |
|
153 |
* @return object Node. |
|
154 |
*/ |
|
155 |
final public function get_node( $id ) { |
|
156 |
if ( $node = $this->_get_node( $id ) ) |
|
157 |
return clone $node; |
|
158 |
} |
|
159 |
|
|
160 |
final protected function _get_node( $id ) { |
|
161 |
if ( $this->bound ) |
|
162 |
return; |
|
163 |
|
|
164 |
if ( empty( $id ) ) |
|
165 |
$id = 'root'; |
|
166 |
|
|
167 |
if ( isset( $this->nodes[ $id ] ) ) |
|
168 |
return $this->nodes[ $id ]; |
|
169 |
} |
|
170 |
|
|
171 |
final public function get_nodes() { |
|
172 |
if ( ! $nodes = $this->_get_nodes() ) |
|
173 |
return; |
|
174 |
|
|
175 |
foreach ( $nodes as &$node ) { |
|
176 |
$node = clone $node; |
|
177 |
} |
|
178 |
return $nodes; |
|
179 |
} |
|
180 |
|
|
181 |
final protected function _get_nodes() { |
|
182 |
if ( $this->bound ) |
|
183 |
return; |
|
184 |
|
|
185 |
return $this->nodes; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* Add a group to a menu node. |
|
190 |
* |
|
191 |
* @since 3.3.0 |
|
192 |
* |
5
|
193 |
* @param array $args { |
|
194 |
* Array of arguments for adding a group. |
|
195 |
* |
|
196 |
* @type string $id ID of the item. |
|
197 |
* @type string $parent Optional. ID of the parent node. Default 'root'. |
|
198 |
* @type array $meta Meta data for the group including the following keys: |
|
199 |
* 'class', 'onclick', 'target', and 'title'. |
|
200 |
* } |
0
|
201 |
*/ |
|
202 |
final public function add_group( $args ) { |
|
203 |
$args['group'] = true; |
|
204 |
|
|
205 |
$this->add_node( $args ); |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* Remove a node. |
|
210 |
* |
|
211 |
* @param string The ID of the item. |
|
212 |
*/ |
|
213 |
public function remove_node( $id ) { |
|
214 |
$this->_unset_node( $id ); |
|
215 |
} |
|
216 |
|
|
217 |
final protected function _unset_node( $id ) { |
|
218 |
unset( $this->nodes[ $id ] ); |
|
219 |
} |
|
220 |
|
|
221 |
public function render() { |
|
222 |
$root = $this->_bind(); |
|
223 |
if ( $root ) |
|
224 |
$this->_render( $root ); |
|
225 |
} |
|
226 |
|
|
227 |
final protected function _bind() { |
|
228 |
if ( $this->bound ) |
|
229 |
return; |
|
230 |
|
|
231 |
// Add the root node. |
|
232 |
// Clear it first, just in case. Don't mess with The Root. |
|
233 |
$this->remove_node( 'root' ); |
|
234 |
$this->add_node( array( |
|
235 |
'id' => 'root', |
|
236 |
'group' => false, |
|
237 |
) ); |
|
238 |
|
|
239 |
// Normalize nodes: define internal 'children' and 'type' properties. |
|
240 |
foreach ( $this->_get_nodes() as $node ) { |
|
241 |
$node->children = array(); |
|
242 |
$node->type = ( $node->group ) ? 'group' : 'item'; |
|
243 |
unset( $node->group ); |
|
244 |
|
|
245 |
// The Root wants your orphans. No lonely items allowed. |
|
246 |
if ( ! $node->parent ) |
|
247 |
$node->parent = 'root'; |
|
248 |
} |
|
249 |
|
|
250 |
foreach ( $this->_get_nodes() as $node ) { |
|
251 |
if ( 'root' == $node->id ) |
|
252 |
continue; |
|
253 |
|
|
254 |
// Fetch the parent node. If it isn't registered, ignore the node. |
|
255 |
if ( ! $parent = $this->_get_node( $node->parent ) ) { |
|
256 |
continue; |
|
257 |
} |
|
258 |
|
|
259 |
// Generate the group class (we distinguish between top level and other level groups). |
|
260 |
$group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu'; |
|
261 |
|
|
262 |
if ( $node->type == 'group' ) { |
|
263 |
if ( empty( $node->meta['class'] ) ) |
|
264 |
$node->meta['class'] = $group_class; |
|
265 |
else |
|
266 |
$node->meta['class'] .= ' ' . $group_class; |
|
267 |
} |
|
268 |
|
|
269 |
// Items in items aren't allowed. Wrap nested items in 'default' groups. |
|
270 |
if ( $parent->type == 'item' && $node->type == 'item' ) { |
|
271 |
$default_id = $parent->id . '-default'; |
|
272 |
$default = $this->_get_node( $default_id ); |
|
273 |
|
|
274 |
// The default group is added here to allow groups that are |
|
275 |
// added before standard menu items to render first. |
|
276 |
if ( ! $default ) { |
|
277 |
// Use _set_node because add_node can be overloaded. |
|
278 |
// Make sure to specify default settings for all properties. |
|
279 |
$this->_set_node( array( |
|
280 |
'id' => $default_id, |
|
281 |
'parent' => $parent->id, |
|
282 |
'type' => 'group', |
|
283 |
'children' => array(), |
|
284 |
'meta' => array( |
|
285 |
'class' => $group_class, |
|
286 |
), |
|
287 |
'title' => false, |
|
288 |
'href' => false, |
|
289 |
) ); |
|
290 |
$default = $this->_get_node( $default_id ); |
|
291 |
$parent->children[] = $default; |
|
292 |
} |
|
293 |
$parent = $default; |
|
294 |
|
|
295 |
// Groups in groups aren't allowed. Add a special 'container' node. |
|
296 |
// The container will invisibly wrap both groups. |
|
297 |
} elseif ( $parent->type == 'group' && $node->type == 'group' ) { |
|
298 |
$container_id = $parent->id . '-container'; |
|
299 |
$container = $this->_get_node( $container_id ); |
|
300 |
|
|
301 |
// We need to create a container for this group, life is sad. |
|
302 |
if ( ! $container ) { |
|
303 |
// Use _set_node because add_node can be overloaded. |
|
304 |
// Make sure to specify default settings for all properties. |
|
305 |
$this->_set_node( array( |
|
306 |
'id' => $container_id, |
|
307 |
'type' => 'container', |
|
308 |
'children' => array( $parent ), |
|
309 |
'parent' => false, |
|
310 |
'title' => false, |
|
311 |
'href' => false, |
|
312 |
'meta' => array(), |
|
313 |
) ); |
|
314 |
|
|
315 |
$container = $this->_get_node( $container_id ); |
|
316 |
|
|
317 |
// Link the container node if a grandparent node exists. |
|
318 |
$grandparent = $this->_get_node( $parent->parent ); |
|
319 |
|
|
320 |
if ( $grandparent ) { |
|
321 |
$container->parent = $grandparent->id; |
|
322 |
|
|
323 |
$index = array_search( $parent, $grandparent->children, true ); |
|
324 |
if ( $index === false ) |
|
325 |
$grandparent->children[] = $container; |
|
326 |
else |
|
327 |
array_splice( $grandparent->children, $index, 1, array( $container ) ); |
|
328 |
} |
|
329 |
|
|
330 |
$parent->parent = $container->id; |
|
331 |
} |
|
332 |
|
|
333 |
$parent = $container; |
|
334 |
} |
|
335 |
|
|
336 |
// Update the parent ID (it might have changed). |
|
337 |
$node->parent = $parent->id; |
|
338 |
|
|
339 |
// Add the node to the tree. |
|
340 |
$parent->children[] = $node; |
|
341 |
} |
|
342 |
|
|
343 |
$root = $this->_get_node( 'root' ); |
|
344 |
$this->bound = true; |
|
345 |
return $root; |
|
346 |
} |
|
347 |
|
|
348 |
final protected function _render( $root ) { |
|
349 |
global $is_IE; |
|
350 |
|
|
351 |
// Add browser classes. |
|
352 |
// We have to do this here since admin bar shows on the front end. |
|
353 |
$class = 'nojq nojs'; |
|
354 |
if ( $is_IE ) { |
|
355 |
if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) ) |
|
356 |
$class .= ' ie7'; |
|
357 |
elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) ) |
|
358 |
$class .= ' ie8'; |
|
359 |
elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) ) |
|
360 |
$class .= ' ie9'; |
|
361 |
} elseif ( wp_is_mobile() ) { |
|
362 |
$class .= ' mobile'; |
|
363 |
} |
|
364 |
|
|
365 |
?> |
5
|
366 |
<div id="wpadminbar" class="<?php echo $class; ?>"> |
|
367 |
<?php if ( ! is_admin() ) { ?> |
|
368 |
<a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a> |
|
369 |
<?php } ?> |
|
370 |
<div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>" tabindex="0"> |
0
|
371 |
<?php foreach ( $root->children as $group ) { |
|
372 |
$this->_render_group( $group ); |
|
373 |
} ?> |
|
374 |
</div> |
|
375 |
<?php if ( is_user_logged_in() ) : ?> |
|
376 |
<a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a> |
|
377 |
<?php endif; ?> |
|
378 |
</div> |
|
379 |
|
|
380 |
<?php |
|
381 |
} |
|
382 |
|
|
383 |
final protected function _render_container( $node ) { |
|
384 |
if ( $node->type != 'container' || empty( $node->children ) ) |
|
385 |
return; |
|
386 |
|
|
387 |
?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php |
|
388 |
foreach ( $node->children as $group ) { |
|
389 |
$this->_render_group( $group ); |
|
390 |
} |
|
391 |
?></div><?php |
|
392 |
} |
|
393 |
|
|
394 |
final protected function _render_group( $node ) { |
|
395 |
if ( $node->type == 'container' ) |
|
396 |
return $this->_render_container( $node ); |
|
397 |
|
|
398 |
if ( $node->type != 'group' || empty( $node->children ) ) |
|
399 |
return; |
|
400 |
|
|
401 |
if ( ! empty( $node->meta['class'] ) ) |
|
402 |
$class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"'; |
|
403 |
else |
|
404 |
$class = ''; |
|
405 |
|
|
406 |
?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php |
|
407 |
foreach ( $node->children as $item ) { |
|
408 |
$this->_render_item( $item ); |
|
409 |
} |
|
410 |
?></ul><?php |
|
411 |
} |
|
412 |
|
|
413 |
final protected function _render_item( $node ) { |
|
414 |
if ( $node->type != 'item' ) |
|
415 |
return; |
|
416 |
|
|
417 |
$is_parent = ! empty( $node->children ); |
|
418 |
$has_link = ! empty( $node->href ); |
|
419 |
|
|
420 |
$tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : ''; |
|
421 |
$aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : ''; |
|
422 |
|
|
423 |
$menuclass = ''; |
|
424 |
|
|
425 |
if ( $is_parent ) { |
|
426 |
$menuclass = 'menupop '; |
|
427 |
$aria_attributes .= ' aria-haspopup="true"'; |
|
428 |
} |
|
429 |
|
|
430 |
if ( ! empty( $node->meta['class'] ) ) |
|
431 |
$menuclass .= $node->meta['class']; |
|
432 |
|
|
433 |
if ( $menuclass ) |
|
434 |
$menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"'; |
|
435 |
|
|
436 |
?> |
|
437 |
|
|
438 |
<li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php |
|
439 |
if ( $has_link ): |
|
440 |
?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php |
|
441 |
if ( ! empty( $node->meta['onclick'] ) ) : |
|
442 |
?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php |
|
443 |
endif; |
|
444 |
if ( ! empty( $node->meta['target'] ) ) : |
|
445 |
?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php |
|
446 |
endif; |
|
447 |
if ( ! empty( $node->meta['title'] ) ) : |
|
448 |
?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php |
|
449 |
endif; |
5
|
450 |
if ( ! empty( $node->meta['rel'] ) ) : |
|
451 |
?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php |
|
452 |
endif; |
0
|
453 |
?>><?php |
|
454 |
else: |
|
455 |
?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; |
|
456 |
if ( ! empty( $node->meta['title'] ) ) : |
|
457 |
?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php |
|
458 |
endif; |
|
459 |
?>><?php |
|
460 |
endif; |
|
461 |
|
|
462 |
echo $node->title; |
|
463 |
|
|
464 |
if ( $has_link ) : |
|
465 |
?></a><?php |
|
466 |
else: |
|
467 |
?></div><?php |
|
468 |
endif; |
|
469 |
|
|
470 |
if ( $is_parent ) : |
|
471 |
?><div class="ab-sub-wrapper"><?php |
|
472 |
foreach ( $node->children as $group ) { |
|
473 |
$this->_render_group( $group ); |
|
474 |
} |
|
475 |
?></div><?php |
|
476 |
endif; |
|
477 |
|
|
478 |
if ( ! empty( $node->meta['html'] ) ) |
|
479 |
echo $node->meta['html']; |
|
480 |
|
|
481 |
?> |
|
482 |
</li><?php |
|
483 |
} |
|
484 |
|
|
485 |
public function recursive_render( $id, $node ) { |
|
486 |
_deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' ); |
|
487 |
$this->_render_item( $node ); |
|
488 |
} |
|
489 |
|
|
490 |
public function add_menus() { |
|
491 |
// User related, aligned right. |
|
492 |
add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 ); |
|
493 |
add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 ); |
|
494 |
add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); |
|
495 |
|
|
496 |
// Site related. |
5
|
497 |
add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 ); |
0
|
498 |
add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); |
|
499 |
add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 ); |
|
500 |
add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 ); |
|
501 |
add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 ); |
|
502 |
|
|
503 |
// Content related. |
|
504 |
if ( ! is_network_admin() && ! is_user_admin() ) { |
|
505 |
add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); |
|
506 |
add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); |
|
507 |
} |
|
508 |
add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 ); |
|
509 |
|
|
510 |
add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 ); |
|
511 |
|
|
512 |
/** |
|
513 |
* Fires after menus are added to the menu bar. |
|
514 |
* |
|
515 |
* @since 3.1.0 |
|
516 |
*/ |
|
517 |
do_action( 'add_admin_bar_menus' ); |
|
518 |
} |
|
519 |
} |