[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7