Ticket #18197: 18197.internals.diff
File 18197.internals.diff, 7.5 KB (added by , 13 years ago) |
---|
-
wp-includes/class-wp-admin-bar.php
1 1 <?php 2 2 class WP_Admin_Bar { 3 var $menu; 4 var $proto = 'http://'; 5 var $user; 3 private $nodes = array(); 4 private $root = array(); 6 5 6 public $proto = 'http://'; 7 public $user; 8 7 9 function initialize() { 8 10 /* Set the protocol used throughout this code */ 9 11 if ( is_ssl() ) 10 12 $this->proto = 'https://'; 11 13 12 14 $this->user = new stdClass; 13 $this->menu = new stdClass;14 15 15 16 if ( is_user_logged_in() ) { 16 17 /* Populate settings we need for the menu based on the current user. */ … … 46 47 do_action( 'admin_bar_init' ); 47 48 } 48 49 49 function add_menu( $args = array() ) { 50 $defaults = array( 51 'title' => false, 52 'href' => false, 53 'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu. 54 'id' => false, // defaults to a sanitized title value. 55 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' ); 56 ); 50 public function add_menu( $node ) { 51 $this->add_node( $node ); 52 } 57 53 58 $r = wp_parse_args( $args, $defaults ); 59 extract( $r, EXTR_SKIP ); 54 public function remove_menu( $id ) { 55 $this->remove_node( $id ); 56 } 60 57 61 if ( empty( $title ) ) 58 /** 59 * Add a node to the menu. 60 * 61 * @param array $args - The arguments for each node. 62 * - id - string - The ID of the item. 63 * - title - string - The title of the node. 64 * - parent - string - The ID of the parent node. Optional. 65 * - href - string - The link for the item. Optional. 66 * - meta - array - Meta data including the following keys: html, class, onclick, target, title. 67 */ 68 public function add_node( $args ) { 69 // Ensure we have a valid ID and title. 70 if ( empty( $args['title'] ) || empty( $args['id'] ) ) 62 71 return false; 63 72 64 /* Make sure we have a valid ID */ 65 if ( empty( $id ) ) 66 $id = esc_attr( sanitize_title( trim( $title ) ) ); 73 $defaults = array( 74 'id' => false, 75 'title' => false, 76 'parent' => false, 77 'href' => false, 78 'meta' => array(), 79 ); 67 80 68 if ( ! empty( $parent ) ) {69 /* Add the menu to the parent item */70 $ child = array( 'id' => $id, 'title' => $title, 'href' => $href );81 // If the node already exists, keep any data that isn't provided. 82 if ( isset( $this->nodes[ $args['id'] ] ) ) 83 $defaults = (array) $this->nodes[ $args['id'] ]; 71 84 72 if ( ! empty( $meta ) ) 73 $child['meta'] = $meta; 85 $args = wp_parse_args( $args, $defaults ); 74 86 75 $this->add_node( $parent, $this->menu, $child ); 76 } else { 77 /* Add the menu item */ 78 $this->menu->{$id} = array( 'title' => $title, 'href' => $href ); 79 80 if ( ! empty( $meta ) ) 81 $this->menu->{$id}['meta'] = $meta; 82 } 87 $this->nodes[ $args['id'] ] = (object) $args; 83 88 } 84 89 85 function remove_menu( $id ) {86 return $this->remove_node( $id, $this->menu);90 public function remove_node( $id ) { 91 unset( $this->nodes[ $id ] ); 87 92 } 88 93 89 function render() { 94 public function render() { 95 // Link nodes to parents. 96 foreach ( $this->nodes as $node ) { 97 98 // Handle root menu items 99 if ( empty( $node->parent ) ) { 100 $this->root[] = $node; 101 continue; 102 } 103 104 // If the parent node isn't registered, ignore the node. 105 if ( ! isset( $this->nodes[ $node->parent ] ) ) 106 continue; 107 108 $parent = $this->nodes[ $node->parent ]; 109 if ( ! isset( $parent->children ) ) 110 $parent->children = array(); 111 112 $parent->children[] = $node; 113 } 114 90 115 ?> 91 116 <div id="wpadminbar" class="nojq nojs"> 92 117 <div class="quicklinks"> 93 <ul class="ab-top-menu"> 94 <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?> 95 <?php $this->recursive_render( $id, $menu_item ) ?> 96 <?php endforeach; ?> 97 </ul> 118 <ul class="ab-top-menu"><?php 119 120 foreach ( $this->root as $node ) { 121 $this->recursive_render( $node ); 122 } 123 124 ?></ul> 98 125 </div> 99 126 </div> 100 127 101 128 <?php 102 /* Wipe the menu, might reduce memory usage, but probably not. */103 $this->menu = null;104 129 } 105 130 106 /* Helpers */ 107 function recursive_render( $id, &$menu_item ) { ?> 108 <?php 109 $is_parent = ! empty( $menu_item['children'] ); 131 function recursive_render( $node ) { 132 $is_parent = ! empty( $node->children ); 110 133 111 134 $menuclass = $is_parent ? 'menupop' : ''; 112 if ( ! empty( $ menu_item['meta']['class'] ) )113 $menuclass .= ' ' . $ menu_item['meta']['class'];135 if ( ! empty( $node->meta['class'] ) ) 136 $menuclass .= ' ' . $node->meta['class']; 114 137 ?> 115 138 116 <li id="<?php echo esc_attr( "wp-admin-bar- $id" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">117 <a href="<?php echo esc_url( $ menu_item['href']) ?>"<?php118 if ( ! empty( $ menu_item['meta']['onclick'] ) ) :119 ?> onclick="<?php echo esc_js( $ menu_item['meta']['onclick'] ); ?>"<?php139 <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"> 140 <a href="<?php echo esc_url( $node->href ) ?>"<?php 141 if ( ! empty( $node->meta['onclick'] ) ) : 142 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php 120 143 endif; 121 if ( ! empty( $ menu_item['meta']['target'] ) ) :122 ?> target="<?php echo esc_attr( $ menu_item['meta']['target'] ); ?>"<?php144 if ( ! empty( $node->meta['target'] ) ) : 145 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php 123 146 endif; 124 if ( ! empty( $ menu_item['meta']['title'] ) ) :125 ?> title="<?php echo esc_attr( $ menu_item['meta']['title'] ); ?>"<?php147 if ( ! empty( $node->meta['title'] ) ) : 148 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php 126 149 endif; 127 150 128 151 ?>><?php … … 131 154 ?><span><?php 132 155 endif; 133 156 134 echo $ menu_item['title'];157 echo $node->title; 135 158 136 159 if ( $is_parent ) : 137 160 ?></span><?php … … 140 163 ?></a> 141 164 142 165 <?php if ( $is_parent ) : ?> 143 <ul> 144 <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?> 145 <?php $this->recursive_render( $child_id, $child_menu_item ); ?> 146 <?php endforeach; ?> 147 </ul> 148 <?php endif; ?> 166 <ul><?php 149 167 150 <?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?> 151 <?php echo $menu_item['meta']['html']; ?> 152 <?php endif; ?> 153 </li><?php 154 } 168 // Render children. 169 foreach ( $node->children as $child_node ) { 170 $this->recursive_render( $child_node ); 171 } 155 172 156 function add_node( $parent_id, &$menu, $child ) { 157 foreach( $menu as $id => $menu_item ) { 158 if ( $parent_id == $id ) { 159 if ( ! isset( $menu->{$parent_id}['children'] ) ) 160 $menu->{$parent_id}['children'] = new stdClass; 161 $menu->{$parent_id}['children']->{$child['id']} = $child; 162 $child = null; 163 return true; 164 } 173 ?></ul> 174 <?php endif; 165 175 166 if ( ! empty( $menu->{$id}['children'] ) ) 167 $this->add_node( $parent_id, $menu->{$id}['children'], $child ); 168 } 176 if ( ! empty( $node->meta['html'] ) ) 177 echo $node->meta['html']; 169 178 170 $child = null; 179 ?> 180 </li><?php 171 181 172 return false;173 182 } 174 183 175 184 function add_menus() { … … 196 205 197 206 do_action( 'add_admin_bar_menus' ); 198 207 } 199 200 function remove_node( $id, &$menu ) {201 if ( isset( $menu->$id ) ) {202 unset( $menu->$id );203 return true;204 }205 206 foreach( $menu as $menu_item_id => $menu_item ) {207 if ( ! empty( $menu->{$menu_item_id}['children'] ) )208 $this->remove_node( $id, $menu->{$menu_item_id}['children'] );209 }210 211 return false;212 }213 208 } 214 209 ?> 215 No newline at end of file