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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7