diff --git a/wp-includes/class-wp-admin-bar.php b/wp-includes/class-wp-admin-bar.php index 30fe18075e..816bd7325f 100644 --- a/wp-includes/class-wp-admin-bar.php +++ b/wp-includes/class-wp-admin-bar.php @@ -1,8 +1,10 @@ proto = 'https://'; $this->user = new stdClass; - $this->menu = new stdClass; if ( is_user_logged_in() ) { /* Populate settings we need for the menu based on the current user. */ @@ -46,83 +47,109 @@ class WP_Admin_Bar { do_action( 'admin_bar_init' ); } - function add_menu( $args = array() ) { - $defaults = array( - 'title' => false, - 'href' => false, - 'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu. - 'id' => false, // defaults to a sanitized title value. - 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' ); - ); + public function add_menu( $node ) { + $this->add_node( $node ); + } - $r = wp_parse_args( $args, $defaults ); - extract( $r, EXTR_SKIP ); + public function remove_menu( $id ) { + $this->remove_node( $id ); + } - if ( empty( $title ) ) + /** + * Add a node to the menu. + * + * @param array $args - The arguments for each node. + * - id - string - The ID of the item. + * - title - string - The title of the node. + * - parent - string - The ID of the parent node. Optional. + * - href - string - The link for the item. Optional. + * - meta - array - Meta data including the following keys: html, class, onclick, target, title. + */ + public function add_node( $args ) { + // Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) + if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) + $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); + + // Ensure we have a valid ID and title. + if ( empty( $args['title'] ) || empty( $args['id'] ) ) return false; - /* Make sure we have a valid ID */ - if ( empty( $id ) ) - $id = esc_attr( sanitize_title( trim( $title ) ) ); + $defaults = array( + 'id' => false, + 'title' => false, + 'parent' => false, + 'href' => false, + 'meta' => array(), + ); - if ( ! empty( $parent ) ) { - /* Add the menu to the parent item */ - $child = array( 'id' => $id, 'title' => $title, 'href' => $href ); + // If the node already exists, keep any data that isn't provided. + if ( isset( $this->nodes[ $args['id'] ] ) ) + $defaults = (array) $this->nodes[ $args['id'] ]; - if ( ! empty( $meta ) ) - $child['meta'] = $meta; + $args = wp_parse_args( $args, $defaults ); - $this->add_node( $parent, $this->menu, $child ); - } else { - /* Add the menu item */ - $this->menu->{$id} = array( 'title' => $title, 'href' => $href ); + $this->nodes[ $args['id'] ] = (object) $args; + } - if ( ! empty( $meta ) ) - $this->menu->{$id}['meta'] = $meta; + public function remove_node( $id ) { + unset( $this->nodes[ $id ] ); + } + + public function render() { + // Link nodes to parents. + foreach ( $this->nodes as $node ) { + + // Handle root menu items + if ( empty( $node->parent ) ) { + $this->root[] = $node; + continue; + } + + // If the parent node isn't registered, ignore the node. + if ( ! isset( $this->nodes[ $node->parent ] ) ) + continue; + + $parent = $this->nodes[ $node->parent ]; + if ( ! isset( $parent->children ) ) + $parent->children = array(); + + $parent->children[] = $node; } - } - function remove_menu( $id ) { - return $this->remove_node( $id, $this->menu ); - } - - function render() { ?>