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


Generated: Wed Nov 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1