wp/wp-includes/class-wp-admin-bar.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    80 		 */
    80 		 */
    81 		do_action( 'admin_bar_init' );
    81 		do_action( 'admin_bar_init' );
    82 	}
    82 	}
    83 
    83 
    84 	/**
    84 	/**
    85 	 * @param array $node
    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.
    86 	 */
    90 	 */
    87 	public function add_menu( $node ) {
    91 	public function add_menu( $node ) {
    88 		$this->add_node( $node );
    92 		$this->add_node( $node );
    89 	}
    93 	}
    90 
    94 
    91 	/**
    95 	/**
    92 	 * @param string $id
    96 	 * Remove a node from the admin bar.
       
    97 	 *
       
    98 	 * @since 3.1.0
       
    99 	 *
       
   100 	 * @param string $id The menu slug to remove.
    93 	 */
   101 	 */
    94 	public function remove_menu( $id ) {
   102 	public function remove_menu( $id ) {
    95 		$this->remove_node( $id );
   103 		$this->remove_node( $id );
    96 	}
   104 	}
    97 
   105 
   112 	 *     @type array  $meta   Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
   120 	 *     @type array  $meta   Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
   113 	 *                          'onclick', 'target', 'title', 'tabindex'. Default empty.
   121 	 *                          'onclick', 'target', 'title', 'tabindex'. Default empty.
   114 	 * }
   122 	 * }
   115 	 */
   123 	 */
   116 	public function add_node( $args ) {
   124 	public function add_node( $args ) {
   117 		// Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
   125 		// Shim for old method signature: add_node( $parent_id, $menu_obj, $args ).
   118 		if ( func_num_args() >= 3 && is_string( func_get_arg( 0 ) ) ) {
   126 		if ( func_num_args() >= 3 && is_string( $args ) ) {
   119 			$args = array_merge( array( 'parent' => func_get_arg( 0 ) ), func_get_arg( 2 ) );
   127 			$args = array_merge( array( 'parent' => $args ), func_get_arg( 2 ) );
   120 		}
   128 		}
   121 
   129 
   122 		if ( is_object( $args ) ) {
   130 		if ( is_object( $args ) ) {
   123 			$args = get_object_vars( $args );
   131 			$args = get_object_vars( $args );
   124 		}
   132 		}
   142 			'group'  => false,
   150 			'group'  => false,
   143 			'meta'   => array(),
   151 			'meta'   => array(),
   144 		);
   152 		);
   145 
   153 
   146 		// If the node already exists, keep any data that isn't provided.
   154 		// If the node already exists, keep any data that isn't provided.
   147 		if ( $maybe_defaults = $this->get_node( $args['id'] ) ) {
   155 		$maybe_defaults = $this->get_node( $args['id'] );
       
   156 		if ( $maybe_defaults ) {
   148 			$defaults = get_object_vars( $maybe_defaults );
   157 			$defaults = get_object_vars( $maybe_defaults );
   149 		}
   158 		}
   150 
   159 
   151 		// Do the same for 'meta' items.
   160 		// Do the same for 'meta' items.
   152 		if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) {
   161 		if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) {
   178 
   187 
   179 	/**
   188 	/**
   180 	 * Gets a node.
   189 	 * Gets a node.
   181 	 *
   190 	 *
   182 	 * @param string $id
   191 	 * @param string $id
   183 	 * @return object Node.
   192 	 * @return object|void Node.
   184 	 */
   193 	 */
   185 	final public function get_node( $id ) {
   194 	final public function get_node( $id ) {
   186 		if ( $node = $this->_get_node( $id ) ) {
   195 		$node = $this->_get_node( $id );
       
   196 		if ( $node ) {
   187 			return clone $node;
   197 			return clone $node;
   188 		}
   198 		}
   189 	}
   199 	}
   190 
   200 
   191 	/**
   201 	/**
   208 
   218 
   209 	/**
   219 	/**
   210 	 * @return array|void
   220 	 * @return array|void
   211 	 */
   221 	 */
   212 	final public function get_nodes() {
   222 	final public function get_nodes() {
   213 		if ( ! $nodes = $this->_get_nodes() ) {
   223 		$nodes = $this->_get_nodes();
       
   224 		if ( ! $nodes ) {
   214 			return;
   225 			return;
   215 		}
   226 		}
   216 
   227 
   217 		foreach ( $nodes as &$node ) {
   228 		foreach ( $nodes as &$node ) {
   218 			$node = clone $node;
   229 			$node = clone $node;
   305 				$node->parent = 'root';
   316 				$node->parent = 'root';
   306 			}
   317 			}
   307 		}
   318 		}
   308 
   319 
   309 		foreach ( $this->_get_nodes() as $node ) {
   320 		foreach ( $this->_get_nodes() as $node ) {
   310 			if ( 'root' == $node->id ) {
   321 			if ( 'root' === $node->id ) {
   311 				continue;
   322 				continue;
   312 			}
   323 			}
   313 
   324 
   314 			// Fetch the parent node. If it isn't registered, ignore the node.
   325 			// Fetch the parent node. If it isn't registered, ignore the node.
   315 			if ( ! $parent = $this->_get_node( $node->parent ) ) {
   326 			$parent = $this->_get_node( $node->parent );
       
   327 			if ( ! $parent ) {
   316 				continue;
   328 				continue;
   317 			}
   329 			}
   318 
   330 
   319 			// Generate the group class (we distinguish between top level and other level groups).
   331 			// Generate the group class (we distinguish between top level and other level groups).
   320 			$group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
   332 			$group_class = ( 'root' === $node->parent ) ? 'ab-top-menu' : 'ab-submenu';
   321 
   333 
   322 			if ( $node->type == 'group' ) {
   334 			if ( 'group' === $node->type ) {
   323 				if ( empty( $node->meta['class'] ) ) {
   335 				if ( empty( $node->meta['class'] ) ) {
   324 					$node->meta['class'] = $group_class;
   336 					$node->meta['class'] = $group_class;
   325 				} else {
   337 				} else {
   326 					$node->meta['class'] .= ' ' . $group_class;
   338 					$node->meta['class'] .= ' ' . $group_class;
   327 				}
   339 				}
   328 			}
   340 			}
   329 
   341 
   330 			// Items in items aren't allowed. Wrap nested items in 'default' groups.
   342 			// Items in items aren't allowed. Wrap nested items in 'default' groups.
   331 			if ( $parent->type == 'item' && $node->type == 'item' ) {
   343 			if ( 'item' === $parent->type && 'item' === $node->type ) {
   332 				$default_id = $parent->id . '-default';
   344 				$default_id = $parent->id . '-default';
   333 				$default    = $this->_get_node( $default_id );
   345 				$default    = $this->_get_node( $default_id );
   334 
   346 
   335 				// The default group is added here to allow groups that are
   347 				// The default group is added here to allow groups that are
   336 				// added before standard menu items to render first.
   348 				// added before standard menu items to render first.
   355 				}
   367 				}
   356 				$parent = $default;
   368 				$parent = $default;
   357 
   369 
   358 				// Groups in groups aren't allowed. Add a special 'container' node.
   370 				// Groups in groups aren't allowed. Add a special 'container' node.
   359 				// The container will invisibly wrap both groups.
   371 				// The container will invisibly wrap both groups.
   360 			} elseif ( $parent->type == 'group' && $node->type == 'group' ) {
   372 			} elseif ( 'group' === $parent->type && 'group' === $node->type ) {
   361 				$container_id = $parent->id . '-container';
   373 				$container_id = $parent->id . '-container';
   362 				$container    = $this->_get_node( $container_id );
   374 				$container    = $this->_get_node( $container_id );
   363 
   375 
   364 				// We need to create a container for this group, life is sad.
   376 				// We need to create a container for this group, life is sad.
   365 				if ( ! $container ) {
   377 				if ( ! $container ) {
   384 
   396 
   385 					if ( $grandparent ) {
   397 					if ( $grandparent ) {
   386 						$container->parent = $grandparent->id;
   398 						$container->parent = $grandparent->id;
   387 
   399 
   388 						$index = array_search( $parent, $grandparent->children, true );
   400 						$index = array_search( $parent, $grandparent->children, true );
   389 						if ( $index === false ) {
   401 						if ( false === $index ) {
   390 							$grandparent->children[] = $container;
   402 							$grandparent->children[] = $container;
   391 						} else {
   403 						} else {
   392 							array_splice( $grandparent->children, $index, 1, array( $container ) );
   404 							array_splice( $grandparent->children, $index, 1, array( $container ) );
   393 						}
   405 						}
   394 					}
   406 					}
   410 		$this->bound = true;
   422 		$this->bound = true;
   411 		return $root;
   423 		return $root;
   412 	}
   424 	}
   413 
   425 
   414 	/**
   426 	/**
   415 	 * @global bool $is_IE
       
   416 	 * @param object $root
   427 	 * @param object $root
   417 	 */
   428 	 */
   418 	final protected function _render( $root ) {
   429 	final protected function _render( $root ) {
   419 		global $is_IE;
       
   420 
       
   421 		// Add browser classes.
   430 		// Add browser classes.
   422 		// We have to do this here since admin bar shows on the front end.
   431 		// We have to do this here since admin bar shows on the front end.
   423 		$class = 'nojq nojs';
   432 		$class = 'nojq nojs';
   424 		if ( $is_IE ) {
   433 		if ( wp_is_mobile() ) {
   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 			$class .= ' mobile';
   434 		}
   435 		}
   435 
   436 
   436 		?>
   437 		?>
   437 		<div id="wpadminbar" class="<?php echo $class; ?>">
   438 		<div id="wpadminbar" class="<?php echo $class; ?>">
   455 
   456 
   456 	/**
   457 	/**
   457 	 * @param object $node
   458 	 * @param object $node
   458 	 */
   459 	 */
   459 	final protected function _render_container( $node ) {
   460 	final protected function _render_container( $node ) {
   460 		if ( $node->type != 'container' || empty( $node->children ) ) {
   461 		if ( 'container' !== $node->type || empty( $node->children ) ) {
   461 			return;
   462 			return;
   462 		}
   463 		}
   463 
   464 
   464 		echo '<div id="' . esc_attr( 'wp-admin-bar-' . $node->id ) . '" class="ab-group-container">';
   465 		echo '<div id="' . esc_attr( 'wp-admin-bar-' . $node->id ) . '" class="ab-group-container">';
   465 		foreach ( $node->children as $group ) {
   466 		foreach ( $node->children as $group ) {
   470 
   471 
   471 	/**
   472 	/**
   472 	 * @param object $node
   473 	 * @param object $node
   473 	 */
   474 	 */
   474 	final protected function _render_group( $node ) {
   475 	final protected function _render_group( $node ) {
   475 		if ( $node->type == 'container' ) {
   476 		if ( 'container' === $node->type ) {
   476 			$this->_render_container( $node );
   477 			$this->_render_container( $node );
   477 			return;
   478 			return;
   478 		}
   479 		}
   479 		if ( $node->type != 'group' || empty( $node->children ) ) {
   480 		if ( 'group' !== $node->type || empty( $node->children ) ) {
   480 			return;
   481 			return;
   481 		}
   482 		}
   482 
   483 
   483 		if ( ! empty( $node->meta['class'] ) ) {
   484 		if ( ! empty( $node->meta['class'] ) ) {
   484 			$class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
   485 			$class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
   495 
   496 
   496 	/**
   497 	/**
   497 	 * @param object $node
   498 	 * @param object $node
   498 	 */
   499 	 */
   499 	final protected function _render_item( $node ) {
   500 	final protected function _render_item( $node ) {
   500 		if ( $node->type != 'item' ) {
   501 		if ( 'item' !== $node->type ) {
   501 			return;
   502 			return;
   502 		}
   503 		}
   503 
   504 
   504 		$is_parent             = ! empty( $node->children );
   505 		$is_parent             = ! empty( $node->children );
   505 		$has_link              = ! empty( $node->href );
   506 		$has_link              = ! empty( $node->href );
   534 		echo "<li id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$menuclass>";
   535 		echo "<li id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$menuclass>";
   535 
   536 
   536 		if ( $has_link ) {
   537 		if ( $has_link ) {
   537 			$attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' );
   538 			$attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' );
   538 			echo "<a class='ab-item'$aria_attributes href='" . esc_url( $node->href ) . "'";
   539 			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 {
   540 		} else {
   543 			$attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' );
   541 			$attributes = array( 'onclick', 'target', 'title', 'rel', 'lang', 'dir' );
   544 			echo '<div class="ab-item ab-empty-item"' . $aria_attributes;
   542 			echo '<div class="ab-item ab-empty-item"' . $aria_attributes;
   545 		}
   543 		}
   546 
   544 
   547 		foreach ( $attributes as $attribute ) {
   545 		foreach ( $attributes as $attribute ) {
   548 			if ( ! empty( $node->meta[ $attribute ] ) ) {
   546 			if ( empty( $node->meta[ $attribute ] ) ) {
       
   547 				continue;
       
   548 			}
       
   549 
       
   550 			if ( 'onclick' === $attribute ) {
       
   551 				echo " $attribute='" . esc_js( $node->meta[ $attribute ] ) . "'";
       
   552 			} else {
   549 				echo " $attribute='" . esc_attr( $node->meta[ $attribute ] ) . "'";
   553 				echo " $attribute='" . esc_attr( $node->meta[ $attribute ] ) . "'";
   550 			}
   554 			}
   551 		}
   555 		}
   552 
   556 
   553 		echo ">{$arrow}{$node->title}";
   557 		echo ">{$arrow}{$node->title}";
   590 	}
   594 	}
   591 
   595 
   592 	/**
   596 	/**
   593 	 */
   597 	 */
   594 	public function add_menus() {
   598 	public function add_menus() {
   595 		// User related, aligned right.
   599 		// User-related, aligned right.
   596 		add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
   600 		add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
   597 		add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
   601 		add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
   598 		add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
   602 		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 );
   603 		add_action( 'admin_bar_menu', 'wp_admin_bar_recovery_mode_menu', 8 );
   600 
   604 
   601 		// Site related.
   605 		// Site-related.
   602 		add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
   606 		add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
   603 		add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
   607 		add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
   604 		add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
   608 		add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
   605 		add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
   609 		add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
   606 		add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
   610 		add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
   607 		add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
   611 		add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
   608 
   612 
   609 		// Content related.
   613 		// Content-related.
   610 		if ( ! is_network_admin() && ! is_user_admin() ) {
   614 		if ( ! is_network_admin() && ! is_user_admin() ) {
   611 			add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
   615 			add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
   612 			add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
   616 			add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
   613 		}
   617 		}
   614 		add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
   618 		add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );