Make WordPress Core


Ignore:
Timestamp:
11/23/2011 09:46:47 PM (14 years ago)
Author:
koopersmith
Message:

Admin Bar: Secondary is so passé. Groups are the new black. fixes #19136.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-admin-bar.php

    r19407 r19429  
    1313
    1414        $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(),
    1919        );
    2020
     
    6969     * - parent     - string    - The ID of the parent node. Optional.
    7070     * - 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.
    7272     * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title.
    7373     */
     
    7878
    7979        // Ensure we have a valid title.
    80         if ( empty( $args['title'] ) )
    81             return false;
    82 
    8380        if ( empty( $args['id'] ) ) {
     81            if ( empty( $args['title'] ) )
     82                return;
     83
    8484            _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
     85            // Deprecated: Generate an ID from the title.
    8586            $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
    8687        }
    8788
    8889        $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(),
    9596        );
    9697
     
    100101
    101102        $args = wp_parse_args( $args, $defaults );
    102         $args['children'] = (object) array(
    103             'primary'   => array(),
    104             'secondary' => array(),
    105         );
     103        $args['children'] = array();
    106104
    107105        $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 );
    108120    }
    109121
     
    137149            }
    138150
    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;
    143172        }
    144173
     
    160189        <div id="wpadminbar" class="<?php echo $class; ?>">
    161190            <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                } ?>
    176194            </div>
    177195        </div>
     
    180198    }
    181199
    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;
    189249        $has_link  = (bool) $node->href;
    190250
     
    223283            if ( $is_parent ) :
    224284                ?><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' );
    230287                    }
    231                     ?></ul><?php
    232 
    233                     // Render secondary submenu
    234                     if ( ! empty( $node->children->secondary ) ):
    235                         ?><ul class="ab-submenu ab-sub-secondary"><?php
    236                         foreach ( $node->children->secondary as $child_node ) {
    237                             $this->recursive_render( $child_node );
    238                         }
    239                         ?></ul><?php
    240                     endif;
    241 
    242288                ?></div><?php
    243289            endif;
     
    248294            ?>
    249295        </li><?php
    250 
     296    }
     297
     298    function recursive_render( $node ) {
     299        $this->render_item( $node );
    251300    }
    252301
     
    272321            add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
    273322
     323        add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
     324
    274325        do_action( 'add_admin_bar_menus' );
    275326    }
Note: See TracChangeset for help on using the changeset viewer.