web/wp-includes/class-wp-admin-bar.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 <?php
     1 <?php
       
     2 /**
       
     3  * The WordPress Toolbar
       
     4  *
       
     5  * @since 3.1.0
       
     6  *
       
     7  * @package WordPress
       
     8  * @subpackage Toolbar
       
     9  */
     2 class WP_Admin_Bar {
    10 class WP_Admin_Bar {
     3 	private $nodes = array();
    11 	private $nodes = array();
     4 	private $bound = false;
    12 	private $bound = false;
     5 	public $user;
    13 	public $user;
     6 
    14 
   102 		// If the node already exists, keep any data that isn't provided.
   110 		// If the node already exists, keep any data that isn't provided.
   103 		if ( $maybe_defaults = $this->get_node( $args['id'] ) )
   111 		if ( $maybe_defaults = $this->get_node( $args['id'] ) )
   104 			$defaults = get_object_vars( $maybe_defaults );
   112 			$defaults = get_object_vars( $maybe_defaults );
   105 
   113 
   106 		// Do the same for 'meta' items.
   114 		// Do the same for 'meta' items.
   107 		if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
   115 		if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
   108 			$args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
   116 			$args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
   109 
   117 
   110 		$args = wp_parse_args( $args, $defaults );
   118 		$args = wp_parse_args( $args, $defaults );
   111 
   119 
   112 		$back_compat_parents = array(
   120 		$back_compat_parents = array(
   147 		if ( isset( $this->nodes[ $id ] ) )
   155 		if ( isset( $this->nodes[ $id ] ) )
   148 			return $this->nodes[ $id ];
   156 			return $this->nodes[ $id ];
   149 	}
   157 	}
   150 
   158 
   151 	final public function get_nodes() {
   159 	final public function get_nodes() {
   152 	   if ( ! $nodes = $this->_get_nodes() )
   160 		if ( ! $nodes = $this->_get_nodes() )
   153 	      return;
   161 			return;
   154 
   162 
   155 	   foreach ( $nodes as &$node ) {
   163 		foreach ( $nodes as &$node ) {
   156 	       $node = clone $node;
   164 			$node = clone $node;
   157 	   }
   165 		}
   158 	   return $nodes;
   166 		return $nodes;
   159 	}
   167 	}
   160 
   168 
   161 	final protected function _get_nodes() {
   169 	final protected function _get_nodes() {
   162 		if ( $this->bound )
   170 		if ( $this->bound )
   163 			return;
   171 			return;
   182 	}
   190 	}
   183 
   191 
   184 	/**
   192 	/**
   185 	 * Remove a node.
   193 	 * Remove a node.
   186 	 *
   194 	 *
   187 	 * @return object The removed node.
   195 	 * @param string The ID of the item.
   188 	 */
   196 	 */
   189 	public function remove_node( $id ) {
   197 	public function remove_node( $id ) {
   190 		$this->_unset_node( $id );
   198 		$this->_unset_node( $id );
   191 	}
   199 	}
   192 
   200 
   235 			// Generate the group class (we distinguish between top level and other level groups).
   243 			// Generate the group class (we distinguish between top level and other level groups).
   236 			$group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
   244 			$group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
   237 
   245 
   238 			if ( $node->type == 'group' ) {
   246 			if ( $node->type == 'group' ) {
   239 				if ( empty( $node->meta['class'] ) )
   247 				if ( empty( $node->meta['class'] ) )
   240 					$node->meta['class'] = '';
   248 					$node->meta['class'] = $group_class;
   241 				$node->meta['class'] .= ' ' . $group_class;
   249 				else
       
   250 					$node->meta['class'] .= ' ' . $group_class;
   242 			}
   251 			}
   243 
   252 
   244 			// Items in items aren't allowed. Wrap nested items in 'default' groups.
   253 			// Items in items aren't allowed. Wrap nested items in 'default' groups.
   245 			if ( $parent->type == 'item' && $node->type == 'item' ) {
   254 			if ( $parent->type == 'item' && $node->type == 'item' ) {
   246 				$default_id = $parent->id . '-default';
   255 				$default_id = $parent->id . '-default';
   337 			$class .= ' mobile';
   346 			$class .= ' mobile';
   338 		}
   347 		}
   339 
   348 
   340 		?>
   349 		?>
   341 		<div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
   350 		<div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
   342 			<div class="quicklinks">
   351 			<a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
       
   352 			<div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
   343 				<?php foreach ( $root->children as $group ) {
   353 				<?php foreach ( $root->children as $group ) {
   344 					$this->_render_group( $group );
   354 					$this->_render_group( $group );
   345 				} ?>
   355 				} ?>
   346 			</div>
   356 			</div>
       
   357 			<a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
   347 		</div>
   358 		</div>
   348 
   359 
   349 		<?php
   360 		<?php
   350 	}
   361 	}
   351 
   362 
   365 			return $this->_render_container( $node );
   376 			return $this->_render_container( $node );
   366 
   377 
   367 		if ( $node->type != 'group' || empty( $node->children ) )
   378 		if ( $node->type != 'group' || empty( $node->children ) )
   368 			return;
   379 			return;
   369 
   380 
   370 		$class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
   381 		if ( ! empty( $node->meta['class'] ) )
   371 
   382 			$class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
   372 		?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
   383 		else
       
   384 			$class = '';
       
   385 
       
   386 		?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
   373 			foreach ( $node->children as $item ) {
   387 			foreach ( $node->children as $item ) {
   374 				$this->_render_item( $item );
   388 				$this->_render_item( $item );
   375 			}
   389 			}
   376 		?></ul><?php
   390 		?></ul><?php
   377 	}
   391 	}
   381 			return;
   395 			return;
   382 
   396 
   383 		$is_parent = ! empty( $node->children );
   397 		$is_parent = ! empty( $node->children );
   384 		$has_link  = ! empty( $node->href );
   398 		$has_link  = ! empty( $node->href );
   385 
   399 
   386 		$tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
   400 		$tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
       
   401 		$aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
   387 
   402 
   388 		$menuclass = '';
   403 		$menuclass = '';
   389 		$aria_attributes = 'tabindex="' . $tabindex . '"';
       
   390 
   404 
   391 		if ( $is_parent ) {
   405 		if ( $is_parent ) {
   392 			$menuclass = 'menupop';
   406 			$menuclass = 'menupop ';
   393 			$aria_attributes .= ' aria-haspopup="true"';
   407 			$aria_attributes .= ' aria-haspopup="true"';
   394 		}
   408 		}
   395 
   409 
   396 		if ( ! empty( $node->meta['class'] ) )
   410 		if ( ! empty( $node->meta['class'] ) )
   397 			$menuclass .= ' ' . $node->meta['class'];
   411 			$menuclass .= $node->meta['class'];
       
   412 
       
   413 		if ( $menuclass )
       
   414 			$menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
   398 
   415 
   399 		?>
   416 		?>
   400 
   417 
   401 		<li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
   418 		<li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
   402 			if ( $has_link ):
   419 			if ( $has_link ):
   403 				?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
   420 				?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
   404 					if ( ! empty( $node->meta['onclick'] ) ) :
   421 					if ( ! empty( $node->meta['onclick'] ) ) :
   405 						?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
   422 						?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
   406 					endif;
   423 					endif;