[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-admin-bar.php (source)

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


Generated : Thu Jun 25 08:20:12 2026 Cross-referenced by PHPXref