[ Index ]

PHP Cross Reference of WordPress

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  class WP_Admin_Bar {
  16      private $nodes = array();
  17      private $bound = false;
  18      public $user;
  19  
  20      /**
  21       * @since 3.3.0
  22       *
  23       * @param string $name
  24       * @return string|array|void
  25       */
  26  	public function __get( $name ) {
  27          switch ( $name ) {
  28              case 'proto':
  29                  return is_ssl() ? 'https://' : 'http://';
  30  
  31              case 'menu':
  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.' );
  33                  return array(); // Sorry, folks.
  34          }
  35      }
  36  
  37      /**
  38       * Initializes the admin bar.
  39       *
  40       * @since 3.1.0
  41       */
  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() ) {
  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 ) );
  51                  $this->user->account_domain = $this->user->domain;
  52              } else {
  53                  $this->user->active_blog    = $this->user->blogs[ get_current_blog_id() ];
  54                  $this->user->domain         = trailingslashit( home_url() );
  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               */
  68              $admin_bar_args  = get_theme_support( 'admin-bar' );
  69              $header_callback = $admin_bar_args[0]['callback'];
  70          }
  71  
  72          if ( empty( $header_callback ) ) {
  73              $header_callback = '_admin_bar_bump_cb';
  74          }
  75  
  76          add_action( 'wp_head', $header_callback );
  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  
  89      /**
  90       * Adds a node (menu item) to the admin bar menu.
  91       *
  92       * @since 3.3.0
  93       *
  94       * @param array $node The attributes that define the node.
  95       */
  96  	public function add_menu( $node ) {
  97          $this->add_node( $node );
  98      }
  99  
 100      /**
 101       * Removes a node from the admin bar.
 102       *
 103       * @since 3.1.0
 104       *
 105       * @param string $id The menu slug to remove.
 106       */
 107  	public function remove_menu( $id ) {
 108          $this->remove_node( $id );
 109      }
 110  
 111      /**
 112       * Adds a node to the menu.
 113       *
 114       * @since 3.1.0
 115       * @since 4.5.0 Added the ability to pass 'lang' and 'dir' meta data.
 116       *
 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.
 125       *     @type array  $meta   Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
 126       *                          'onclick', 'target', 'title', 'tabindex'. Default empty.
 127       * }
 128       */
 129  	public function add_node( $args ) {
 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 ) );
 133          }
 134  
 135          if ( is_object( $args ) ) {
 136              $args = get_object_vars( $args );
 137          }
 138  
 139          // Ensure we have a valid title.
 140          if ( empty( $args['id'] ) ) {
 141              if ( empty( $args['title'] ) ) {
 142                  return;
 143              }
 144  
 145              _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3.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.
 160          $maybe_defaults = $this->get_node( $args['id'] );
 161          if ( $maybe_defaults ) {
 162              $defaults = get_object_vars( $maybe_defaults );
 163          }
 164  
 165          // Do the same for 'meta' items.
 166          if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) {
 167              $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
 168          }
 169  
 170          $args = wp_parse_args( $args, $defaults );
 171  
 172          $back_compat_parents = array(
 173              'my-account-with-avatar' => array( 'my-account', '3.3' ),
 174              'my-blogs'               => array( 'my-sites', '3.3' ),
 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  
 186      /**
 187       * @since 3.3.0
 188       *
 189       * @param array $args
 190       */
 191  	final protected function _set_node( $args ) {
 192          $this->nodes[ $args['id'] ] = (object) $args;
 193      }
 194  
 195      /**
 196       * Gets a node.
 197       *
 198       * @since 3.3.0
 199       *
 200       * @param string $id
 201       * @return object|void Node.
 202       */
 203  	final public function get_node( $id ) {
 204          $node = $this->_get_node( $id );
 205          if ( $node ) {
 206              return clone $node;
 207          }
 208      }
 209  
 210      /**
 211       * @since 3.3.0
 212       *
 213       * @param string $id
 214       * @return object|void
 215       */
 216  	final protected function _get_node( $id ) {
 217          if ( $this->bound ) {
 218              return;
 219          }
 220  
 221          if ( empty( $id ) ) {
 222              $id = 'root';
 223          }
 224  
 225          if ( isset( $this->nodes[ $id ] ) ) {
 226              return $this->nodes[ $id ];
 227          }
 228      }
 229  
 230      /**
 231       * @since 3.3.0
 232       *
 233       * @return array|void
 234       */
 235  	final public function get_nodes() {
 236          $nodes = $this->_get_nodes();
 237          if ( ! $nodes ) {
 238              return;
 239          }
 240  
 241          foreach ( $nodes as &$node ) {
 242              $node = clone $node;
 243          }
 244          return $nodes;
 245      }
 246  
 247      /**
 248       * @since 3.3.0
 249       *
 250       * @return array|void
 251       */
 252  	final protected function _get_nodes() {
 253          if ( $this->bound ) {
 254              return;
 255          }
 256  
 257          return $this->nodes;
 258      }
 259  
 260      /**
 261       * Adds a group to a toolbar menu node.
 262       *
 263       * Groups can be used to organize toolbar items into distinct sections of a toolbar menu.
 264       *
 265       * @since 3.3.0
 266       *
 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       * }
 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       *
 285       * @since 3.1.0
 286       *
 287       * @param string $id The ID of the item.
 288       */
 289  	public function remove_node( $id ) {
 290          $this->_unset_node( $id );
 291      }
 292  
 293      /**
 294       * @since 3.3.0
 295       *
 296       * @param string $id
 297       */
 298  	final protected function _unset_node( $id ) {
 299          unset( $this->nodes[ $id ] );
 300      }
 301  
 302      /**
 303       * @since 3.1.0
 304       */
 305  	public function render() {
 306          $root = $this->_bind();
 307          if ( $root ) {
 308              $this->_render( $root );
 309          }
 310      }
 311  
 312      /**
 313       * @since 3.3.0
 314       *
 315       * @return object|void
 316       */
 317  	final protected function _bind() {
 318          if ( $this->bound ) {
 319              return;
 320          }
 321  
 322          // Add the root node.
 323          // Clear it first, just in case. Don't mess with The Root.
 324          $this->remove_node( 'root' );
 325          $this->add_node(
 326              array(
 327                  'id'    => 'root',
 328                  'group' => false,
 329              )
 330          );
 331  
 332          // Normalize nodes: define internal 'children' and 'type' properties.
 333          foreach ( $this->_get_nodes() as $node ) {
 334              $node->children = array();
 335              $node->type     = ( $node->group ) ? 'group' : 'item';
 336              unset( $node->group );
 337  
 338              // The Root wants your orphans. No lonely items allowed.
 339              if ( ! $node->parent ) {
 340                  $node->parent = 'root';
 341              }
 342          }
 343  
 344          foreach ( $this->_get_nodes() as $node ) {
 345              if ( 'root' === $node->id ) {
 346                  continue;
 347              }
 348  
 349              // Fetch the parent node. If it isn't registered, ignore the node.
 350              $parent = $this->_get_node( $node->parent );
 351              if ( ! $parent ) {
 352                  continue;
 353              }
 354  
 355              // Generate the group class (we distinguish between top level and other level groups).
 356              $group_class = ( 'root' === $node->parent ) ? 'ab-top-menu' : 'ab-submenu';
 357  
 358              if ( 'group' === $node->type ) {
 359                  if ( empty( $node->meta['class'] ) ) {
 360                      $node->meta['class'] = $group_class;
 361                  } else {
 362                      $node->meta['class'] .= ' ' . $group_class;
 363                  }
 364              }
 365  
 366              // Items in items aren't allowed. Wrap nested items in 'default' groups.
 367              if ( 'item' === $parent->type && 'item' === $node->type ) {
 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.
 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                  // Groups in groups aren't allowed. Add a special 'container' node.
 395                  // The container will invisibly wrap both groups.
 396              } elseif ( 'group' === $parent->type && 'group' === $node->type ) {
 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.
 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                      );
 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 );
 425                          if ( false === $index ) {
 426                              $grandparent->children[] = $container;
 427                          } else {
 428                              array_splice( $grandparent->children, $index, 1, array( $container ) );
 429                          }
 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  
 445          $root        = $this->_get_node( 'root' );
 446          $this->bound = true;
 447          return $root;
 448      }
 449  
 450      /**
 451       * @since 3.3.0
 452       *
 453       * @param object $root
 454       */
 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';
 459          if ( wp_is_mobile() ) {
 460              $class .= ' mobile';
 461          }
 462  
 463          ?>
 464          <div id="wpadminbar" class="<?php echo $class; ?>">
 465              <?php if ( ! is_admin() && ! did_action( 'wp_body_open' ) ) { ?>
 466                  <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
 467              <?php } ?>
 468              <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>">
 469                  <?php
 470                  foreach ( $root->children as $group ) {
 471                      $this->_render_group( $group );
 472                  }
 473                  ?>
 474              </div>
 475              <?php if ( is_user_logged_in() ) : ?>
 476              <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e( 'Log Out' ); ?></a>
 477              <?php endif; ?>
 478          </div>
 479  
 480          <?php
 481      }
 482  
 483      /**
 484       * @since 3.3.0
 485       *
 486       * @param object $node
 487       */
 488  	final protected function _render_container( $node ) {
 489          if ( 'container' !== $node->type || empty( $node->children ) ) {
 490              return;
 491          }
 492  
 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>';
 498      }
 499  
 500      /**
 501       * @since 3.3.0
 502       *
 503       * @param object $node
 504       */
 505  	final protected function _render_group( $node ) {
 506          if ( 'container' === $node->type ) {
 507              $this->_render_container( $node );
 508              return;
 509          }
 510          if ( 'group' !== $node->type || empty( $node->children ) ) {
 511              return;
 512          }
 513  
 514          if ( ! empty( $node->meta['class'] ) ) {
 515              $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
 516          } else {
 517              $class = '';
 518          }
 519  
 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>';
 525      }
 526  
 527      /**
 528       * @since 3.3.0
 529       *
 530       * @param object $node
 531       */
 532  	final protected function _render_item( $node ) {
 533          if ( 'item' !== $node->type ) {
 534              return;
 535          }
 536  
 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;
 541  
 542          // Allow only numeric values, then casted to integers, and allow a tabindex value of `0` for a11y.
 543          $tabindex        = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : '';
 544          $aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : '';
 545  
 546          $menuclass = '';
 547          $arrow     = '';
 548  
 549          if ( $is_parent ) {
 550              $menuclass        = 'menupop ';
 551              $aria_attributes .= ' aria-haspopup="true"';
 552          }
 553  
 554          if ( ! empty( $node->meta['class'] ) ) {
 555              $menuclass .= $node->meta['class'];
 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          }
 566  
 567          echo "<li id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$menuclass>";
 568  
 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          }
 576  
 577          foreach ( $attributes as $attribute ) {
 578              if ( empty( $node->meta[ $attribute ] ) ) {
 579                  continue;
 580              }
 581  
 582              if ( 'onclick' === $attribute ) {
 583                  echo " $attribute='" . esc_js( $node->meta[ $attribute ] ) . "'";
 584              } else {
 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          }
 596  
 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          }
 604  
 605          if ( ! empty( $node->meta['html'] ) ) {
 606              echo $node->meta['html'];
 607          }
 608  
 609          echo '</li>';
 610      }
 611  
 612      /**
 613       * Renders toolbar items recursively.
 614       *
 615       * @since 3.1.0
 616       * @deprecated 3.3.0 Use WP_Admin_Bar::_render_item() or WP_Admin_bar::render() instead.
 617       * @see WP_Admin_Bar::_render_item()
 618       * @see WP_Admin_Bar::render()
 619       *
 620       * @param string $id    Unused.
 621       * @param object $node
 622       */
 623  	public function recursive_render( $id, $node ) {
 624          _deprecated_function( __METHOD__, '3.3.0', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
 625          $this->_render_item( $node );
 626      }
 627  
 628      /**
 629       * Adds menus to the admin bar.
 630       *
 631       * @since 3.1.0
 632       */
 633  	public function add_menus() {
 634          // User-related, aligned right.
 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 );
 638          add_action( 'admin_bar_menu', 'wp_admin_bar_recovery_mode_menu', 8 );
 639  
 640          // Site-related.
 641          add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 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 );
 645          add_action( 'admin_bar_menu', 'wp_admin_bar_edit_site_menu', 40 );
 646          add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
 647          add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
 648  
 649          // Content-related.
 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  }


Generated: Sat Apr 27 01:00:02 2024 Cross-referenced by PHPXref 0.7.1