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