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