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


Generated: Sun Apr 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1