From e2a62a313486c771e676b6e2c8f5c15e92ddfea6 Mon Sep 17 00:00:00 2001 From: Daryl Koopersmith Date: Wed, 2 Nov 2011 20:34:54 +0000 Subject: [PATCH] Improve admin bar internal representation. Simpler signatures, with backwards compatibility. Add items without requiring parents to be added first. see #18197. git-svn-id: https://develop.svn.wordpress.org/trunk@19120 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/class-wp-admin-bar.php | 195 ++++++++++++++--------------- 1 file changed, 97 insertions(+), 98 deletions(-) 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() { ?>
menu = null; } - /* Helpers */ - function recursive_render( $id, &$menu_item ) { ?> - children ); $menuclass = $is_parent ? 'menupop' : ''; - if ( ! empty( $menu_item['meta']['class'] ) ) - $menuclass .= ' ' . $menu_item['meta']['class']; + if ( ! empty( $node->meta['class'] ) ) + $menuclass .= ' ' . $node->meta['class']; ?> -
  • " class=""> - onclick=""id}" ); ?>" class=""> + meta['onclick'] ) ) : + ?> onclick="meta['onclick'] ); ?>" target=""meta['target'] ) ) : + ?> target="meta['target'] ); ?>" title=""meta['title'] ) ) : + ?> title="meta['title'] ); ?>">title; if ( $is_parent ) : ?> - - + + meta['html'] ) ) + echo $node->meta['html']; + + ?>
  • $menu_item ) { - if ( $parent_id == $id ) { - if ( ! isset( $menu->{$parent_id}['children'] ) ) - $menu->{$parent_id}['children'] = new stdClass; - $menu->{$parent_id}['children']->{$child['id']} = $child; - $child = null; - return true; - } - - if ( ! empty( $menu->{$id}['children'] ) ) - $this->add_node( $parent_id, $menu->{$id}['children'], $child ); - } - - $child = null; - - return false; } function add_menus() { @@ -196,19 +209,5 @@ class WP_Admin_Bar { do_action( 'add_admin_bar_menus' ); } - - function remove_node( $id, &$menu ) { - if ( isset( $menu->$id ) ) { - unset( $menu->$id ); - return true; - } - - foreach( $menu as $menu_item_id => $menu_item ) { - if ( ! empty( $menu->{$menu_item_id}['children'] ) ) - $this->remove_node( $id, $menu->{$menu_item_id}['children'] ); - } - - return false; - } } ?> \ No newline at end of file