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