Changeset 19429 for trunk/wp-includes/class-wp-admin-bar.php
- Timestamp:
- 11/23/2011 09:46:47 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/wp-includes/class-wp-admin-bar.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-wp-admin-bar.php
r19407 r19429 13 13 14 14 $this->user = new stdClass; 15 $this->root = new stdClass;16 $this->root->children = (object) array(17 ' primary' => array(),18 ' secondary' => array(),15 $this->root = (object) array( 16 'id' => 'root', 17 'group' => false, 18 'children' => array(), 19 19 ); 20 20 … … 69 69 * - parent - string - The ID of the parent node. Optional. 70 70 * - href - string - The link for the item. Optional. 71 * - secondary - boolean - If the item should be part of a secondary menu. Optional. Default false.71 * - group - boolean - If the node is a group. Optional. Default false. 72 72 * - meta - array - Meta data including the following keys: html, class, onclick, target, title. 73 73 */ … … 78 78 79 79 // Ensure we have a valid title. 80 if ( empty( $args['title'] ) )81 return false;82 83 80 if ( empty( $args['id'] ) ) { 81 if ( empty( $args['title'] ) ) 82 return; 83 84 84 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' ); 85 // Deprecated: Generate an ID from the title. 85 86 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) ); 86 87 } 87 88 88 89 $defaults = array( 89 'id' => false,90 'title' => false,91 'parent' => false,92 'href' => false,93 ' secondary'=> false,94 'meta' => array(),90 'id' => false, 91 'title' => false, 92 'parent' => false, 93 'href' => false, 94 'group' => false, 95 'meta' => array(), 95 96 ); 96 97 … … 100 101 101 102 $args = wp_parse_args( $args, $defaults ); 102 $args['children'] = (object) array( 103 'primary' => array(), 104 'secondary' => array(), 105 ); 103 $args['children'] = array(); 106 104 107 105 $this->nodes[ $args['id'] ] = (object) $args; 106 } 107 108 /** 109 * Add a group to a menu node. 110 * 111 * @param array $args - The arguments for each node. 112 * - id - string - The ID of the item. 113 * - parent - string - The ID of the parent node. Optional. Default root. 114 * - meta - array - Meta data including the following keys: class, onclick, target, title. 115 */ 116 public function add_group( $args ) { 117 $args['group'] = true; 118 119 $this->add_node( $args ); 108 120 } 109 121 … … 137 149 } 138 150 139 if ( $node->secondary ) 140 $parent->children->secondary[] = $node; 141 else 142 $parent->children->primary[] = $node; 151 152 // Ensure that our tree is of the form "item -> group -> item -> group -> ..." 153 if ( ! $parent->group && ! $node->group ) { // Both are items. 154 // The default group is added here to allow groups that are 155 // added before standard menu items to render first. 156 if ( ! isset( $parent->children['default'] ) ) { 157 $parent->children['default'] = (object) array( 158 'id' => "{$parent->id}-default", 159 'parent' => $parent->id, 160 'group' => true, 161 'children' => array(), 162 ); 163 } 164 $parent = $parent->children['default']; 165 } 166 167 // Update the parent ID (it might have changed). 168 $node->parent = $parent->id; 169 170 // Add the node to the tree. 171 $parent->children[] = $node; 143 172 } 144 173 … … 160 189 <div id="wpadminbar" class="<?php echo $class; ?>"> 161 190 <div class="quicklinks"> 162 <ul class="ab-top-menu"><?php 163 164 foreach ( $this->root->children->primary as $node ) { 165 $this->recursive_render( $node ); 166 } 167 168 ?></ul> 169 <ul class="ab-top-menu ab-top-secondary"><?php 170 171 foreach ( $this->root->children->secondary as $node ) { 172 $this->recursive_render( $node ); 173 } 174 175 ?></ul> 191 <?php foreach ( $this->root->children as $group ) { 192 $this->render_group( $group, 'ab-top-menu' ); 193 } ?> 176 194 </div> 177 195 </div> … … 180 198 } 181 199 182 function recursive_render( $node ) { 183 if ( ! $node->children->primary && $node->children->secondary ) { 184 $node->children->primary = $node->children->secondary; 185 $node->children->secondary = array(); 186 } 187 188 $is_parent = (bool) $node->children->primary; 200 private function render_group( $node, $class = '' ) { 201 if ( ! $node->group ) 202 return; 203 204 // Check for groups within groups. 205 $groups = array(); 206 foreach ( $node->children as $child ) { 207 if ( $child->group ) { 208 $groups[] = $child; 209 } else { 210 if ( ! isset( $default ) ) { 211 // Create a default proxy item to be used in the case of nested groups. 212 $default = (object) wp_parse_args( array( 'children' => array() ), (array) $node ); 213 $groups[] = $default; 214 } 215 $default->children[] = $child; 216 } 217 } 218 219 $is_single_group = count( $groups ) === 1; 220 221 222 // If we don't have any subgroups, render the group. 223 if ( $is_single_group && ! empty( $node->children ) ): 224 225 if ( ! empty( $node->meta['class'] ) ) 226 $class .= ' ' . $node->meta['class']; 227 228 ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php 229 foreach ( $node->children as $item ) { 230 $this->render_item( $item ); 231 } 232 ?></ul><?php 233 234 // Wrap the subgroups in a div and render each individual subgroup. 235 elseif ( ! $is_single_group ): 236 ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php 237 foreach ( $groups as $group ) { 238 $this->render_group( $group, $class ); 239 } 240 ?></div><?php 241 endif; 242 } 243 244 private function render_item( $node ) { 245 if ( $node->group ) 246 return; 247 248 $is_parent = (bool) $node->children; 189 249 $has_link = (bool) $node->href; 190 250 … … 223 283 if ( $is_parent ) : 224 284 ?><div class="ab-sub-wrapper"><?php 225 226 // Render primary submenu 227 ?><ul class="ab-submenu"><?php 228 foreach ( $node->children->primary as $child_node ) { 229 $this->recursive_render( $child_node ); 285 foreach ( $node->children as $group ) { 286 $this->render_group( $group, 'ab-submenu' ); 230 287 } 231 ?></ul><?php232 233 // Render secondary submenu234 if ( ! empty( $node->children->secondary ) ):235 ?><ul class="ab-submenu ab-sub-secondary"><?php236 foreach ( $node->children->secondary as $child_node ) {237 $this->recursive_render( $child_node );238 }239 ?></ul><?php240 endif;241 242 288 ?></div><?php 243 289 endif; … … 248 294 ?> 249 295 </li><?php 250 296 } 297 298 function recursive_render( $node ) { 299 $this->render_item( $node ); 251 300 } 252 301 … … 272 321 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 ); 273 322 323 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 ); 324 274 325 do_action( 'add_admin_bar_menus' ); 275 326 }
Note: See TracChangeset
for help on using the changeset viewer.