[ 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 toolbar menu node.
 247       *
 248       * Groups can be used to organize toolbar items into distinct sections of a toolbar menu.
 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 = ( 'root' === $node->parent ) ? 'ab-top-menu' : 'ab-submenu';
 335  
 336              if ( 'group' === $node->type ) {
 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 ( 'item' === $parent->type && 'item' === $node->type ) {
 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 ( 'group' === $parent->type && 'group' === $node->type ) {
 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 ( false === $index ) {
 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       * @param object $root
 430       */
 431  	final protected function _render( $root ) {
 432          // Add browser classes.
 433          // We have to do this here since admin bar shows on the front end.
 434          $class = 'nojq nojs';
 435          if ( wp_is_mobile() ) {
 436              $class .= ' mobile';
 437          }
 438  
 439          ?>
 440          <div id="wpadminbar" class="<?php echo $class; ?>">
 441              <?php if ( ! is_admin() && ! did_action( 'wp_body_open' ) ) { ?>
 442                  <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
 443              <?php } ?>
 444              <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>">
 445                  <?php
 446                  foreach ( $root->children as $group ) {
 447                      $this->_render_group( $group );
 448                  }
 449                  ?>
 450              </div>
 451              <?php if ( is_user_logged_in() ) : ?>
 452              <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e( 'Log Out' ); ?></a>
 453              <?php endif; ?>
 454          </div>
 455  
 456          <?php
 457      }
 458  
 459      /**
 460       * @param object $node
 461       */
 462  	final protected function _render_container( $node ) {
 463          if ( 'container' !== $node->type || empty( $node->children ) ) {
 464              return;
 465          }
 466  
 467          echo '<div id="' . esc_attr( 'wp-admin-bar-' . $node->id ) . '" class="ab-group-container">';
 468          foreach ( $node->children as $group ) {
 469              $this->_render_group( $group );
 470          }
 471          echo '</div>';
 472      }
 473  
 474      /**
 475       * @param object $node
 476       */
 477  	final protected function _render_group( $node ) {
 478          if ( 'container' === $node->type ) {
 479              $this->_render_container( $node );
 480              return;
 481          }
 482          if ( 'group' !== $node->type || empty( $node->children ) ) {
 483              return;
 484          }
 485  
 486          if ( ! empty( $node->meta['class'] ) ) {
 487              $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
 488          } else {
 489              $class = '';
 490          }
 491  
 492          echo "<ul id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$class>";
 493          foreach ( $node->children as $item ) {
 494              $this->_render_item( $item );
 495          }
 496          echo '</ul>';
 497      }
 498  
 499      /**
 500       * @param object $node
 501       */
 502  	final protected function _render_item( $node ) {
 503          if ( 'item' !== $node->type ) {
 504              return;
 505          }
 506  
 507          $is_parent             = ! empty( $node->children );
 508          $has_link              = ! empty( $node->href );
 509          $is_root_top_item      = 'root-default' === $node->parent;
 510          $is_top_secondary_item = 'top-secondary' === $node->parent;
 511  
 512          // Allow only numeric values, then casted to integers, and allow a tabindex value of `0` for a11y.
 513          $tabindex        = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : '';
 514          $aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : '';
 515  
 516          $menuclass = '';
 517          $arrow     = '';
 518  
 519          if ( $is_parent ) {
 520              $menuclass        = 'menupop ';
 521              $aria_attributes .= ' aria-haspopup="true"';
 522          }
 523  
 524          if ( ! empty( $node->meta['class'] ) ) {
 525              $menuclass .= $node->meta['class'];
 526          }
 527  
 528          // Print the arrow icon for the menu children with children.
 529          if ( ! $is_root_top_item && ! $is_top_secondary_item && $is_parent ) {
 530              $arrow = '<span class="wp-admin-bar-arrow" aria-hidden="true"></span>';
 531          }
 532  
 533          if ( $menuclass ) {
 534              $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
 535          }
 536  
 537          echo "<li id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$menuclass>";
 538  
 539          if ( $has_link ) {
 540              $attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' );
 541              echo "<a class='ab-item'$aria_attributes href='" . esc_url( $node->href ) . "'";
 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                  continue;
 550              }
 551  
 552              if ( 'onclick' === $attribute ) {
 553                  echo " $attribute='" . esc_js( $node->meta[ $attribute ] ) . "'";
 554              } else {
 555                  echo " $attribute='" . esc_attr( $node->meta[ $attribute ] ) . "'";
 556              }
 557          }
 558  
 559          echo ">{$arrow}{$node->title}";
 560  
 561          if ( $has_link ) {
 562              echo '</a>';
 563          } else {
 564              echo '</div>';
 565          }
 566  
 567          if ( $is_parent ) {
 568              echo '<div class="ab-sub-wrapper">';
 569              foreach ( $node->children as $group ) {
 570                  $this->_render_group( $group );
 571              }
 572              echo '</div>';
 573          }
 574  
 575          if ( ! empty( $node->meta['html'] ) ) {
 576              echo $node->meta['html'];
 577          }
 578  
 579          echo '</li>';
 580      }
 581  
 582      /**
 583       * Renders toolbar items recursively.
 584       *
 585       * @since 3.1.0
 586       * @deprecated 3.3.0 Use WP_Admin_Bar::_render_item() or WP_Admin_bar::render() instead.
 587       * @see WP_Admin_Bar::_render_item()
 588       * @see WP_Admin_Bar::render()
 589       *
 590       * @param string $id    Unused.
 591       * @param object $node
 592       */
 593  	public function recursive_render( $id, $node ) {
 594          _deprecated_function( __METHOD__, '3.3.0', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
 595          $this->_render_item( $node );
 596      }
 597  
 598      /**
 599       */
 600  	public function add_menus() {
 601          // User-related, aligned right.
 602          add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
 603          add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
 604          add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
 605          add_action( 'admin_bar_menu', 'wp_admin_bar_recovery_mode_menu', 8 );
 606  
 607          // Site-related.
 608          add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
 609          add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
 610          add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
 611          add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
 612          add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
 613          add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
 614  
 615          // Content-related.
 616          if ( ! is_network_admin() && ! is_user_admin() ) {
 617              add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
 618              add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
 619          }
 620          add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
 621  
 622          add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
 623  
 624          /**
 625           * Fires after menus are added to the menu bar.
 626           *
 627           * @since 3.1.0
 628           */
 629          do_action( 'add_admin_bar_menus' );
 630      }
 631  }


Generated: Thu Nov 26 01:00:03 2020 Cross-referenced by PHPXref 0.7.1