Make WordPress Core

Ticket #19371: 19371.4.diff

File 19371.4.diff, 8.2 KB (added by nacin, 13 years ago)
  • wp-includes/class-wp-admin-bar.php

     
    11<?php
    22class WP_Admin_Bar {
    33        private $nodes = array();
    4         private $root = array();
    5 
    6         public $proto = 'http://';
    74        public $user;
    85
    9         function initialize() {
    10                 /* Set the protocol used throughout this code */
    11                 if ( is_ssl() )
    12                         $this->proto = 'https://';
     6        public function __get( $name ) {
     7                switch ( $name ) {
     8                        case 'proto' :
     9                                return is_ssl() ? 'https://' : 'http://';
     10                                break;
     11                        case 'menu' :
     12                                $this->_bind();
     13                                return $this->get_nodes();
     14                                break;
     15                }
     16        }
    1317
     18        public function initialize() {
    1419                $this->user = new stdClass;
    15                 $this->root = (object) array(
     20
     21                $this->add_node( array(
    1622                        'id'       => 'root',
    1723                        'group'    => false,
    18                         'children' => array(),
    19                 );
     24                ) );
    2025
    2126                if ( is_user_logged_in() ) {
    2227                        /* Populate settings we need for the menu based on the current user. */
     
    6974         * - parent     - string    - The ID of the parent node. Optional.
    7075         * - href       - string    - The link for the item. Optional.
    7176         * - group      - boolean   - If the node is a group. Optional. Default false.
    72          * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title.
     77         * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title, tabindex.
    7378         */
    7479        public function add_node( $args ) {
    7580                // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
    7681                if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
    7782                        $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
    7883
     84                if ( is_object( $args ) )
     85                        $args = get_object_vars( $args );
     86
    7987                // Ensure we have a valid title.
    8088                if ( empty( $args['id'] ) ) {
    8189                        if ( empty( $args['title'] ) )
     
    96104                );
    97105
    98106                // If the node already exists, keep any data that isn't provided.
    99                 if ( isset( $this->nodes[ $args['id'] ] ) )
    100                         $defaults = (array) $this->nodes[ $args['id'] ];
     107                if ( $this->get_node( $args['id'] ) )
     108                        $defaults = get_object_vars( $this->get_node( $args['id'] ) );
    101109
     110                // Do the same for 'meta' items.
     111                if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
     112                        $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
     113
    102114                $args = wp_parse_args( $args, $defaults );
    103                 $args['children'] = array();
    104115
     116                $this->_set_node( $args );
     117        }
     118
     119        final protected function _set_node( $args ) {
    105120                $this->nodes[ $args['id'] ] = (object) $args;
    106121        }
    107122
    108123        /**
     124         * Gets a node.
     125         *
     126         * @return object Node.
     127         */
     128        final public function get_node( $id ) {
     129                if ( isset( $this->nodes[ $id ] ) )
     130                        return $this->nodes[ $id ];
     131        }
     132
     133        final protected function get_nodes() {
     134                return $this->nodes;
     135        }
     136
     137        /**
    109138         * Add a group to a menu node.
    110139         *
     140         * @since 3.3.0
     141         *
    111142         * @param array $args - The arguments for each node.
    112143         * - id         - string    - The ID of the item.
    113144         * - parent     - string    - The ID of the parent node. Optional. Default root.
    114145         * - meta       - array     - Meta data including the following keys: class, onclick, target, title.
    115146         */
    116         public function add_group( $args ) {
     147        final public function add_group( $args ) {
    117148                $args['group'] = true;
    118149
    119150                $this->add_node( $args );
    120151        }
    121152
     153        /**
     154         * Remove a node.
     155         *
     156         * @return object The removed node.
     157         */
    122158        public function remove_node( $id ) {
     159                $node = $this->get_node( $id );
     160                $this->_unset_node( $id );
     161                return $node;
     162        }
     163
     164        final protected function _unset_node( $id ) {
    123165                unset( $this->nodes[ $id ] );
    124166        }
    125167
    126168        public function render() {
    127                 global $is_IE, $is_iphone;
     169                $this->_bind();
     170                $this->_render();
     171        }
    128172
    129                 // Link nodes to parents.
    130                 foreach ( $this->nodes as $node ) {
     173        final protected function _bind() {
     174                static $bound = false;
     175                if ( $bound )
     176                        return;
     177                $bound = true;
    131178
     179                foreach ( $this->get_nodes() as $node ) {
     180                        if ( 'root' == $node->id )
     181                                continue;
     182
    132183                        // Handle root menu items
    133184                        if ( empty( $node->parent ) ) {
    134                                 $parent = $this->root;
    135 
    136                         // If the parent node isn't registered, ignore the node.
    137                         } elseif ( ! isset( $this->nodes[ $node->parent ] ) ) {
     185                                $parent = $this->get_node( 'root' );
     186                        } elseif ( ! $parent = $this->get_node( $node->parent ) ) {
     187                                // If the parent node isn't registered, ignore the node.
    138188                                continue;
    139 
    140                         } else {
    141                                 $parent = $this->nodes[ $node->parent ];
    142189                        }
    143190
    144 
    145191                        // Ensure that our tree is of the form "item -> group -> item -> group -> ..."
    146192                        if ( ! $parent->group && ! $node->group ) { // Both are items.
    147193                                // The default group is added here to allow groups that are
     
    160206                        // Update the parent ID (it might have changed).
    161207                        $node->parent = $parent->id;
    162208
     209                        if ( ! isset( $parent->children ) )
     210                                $parent->children = array();
     211
    163212                        // Add the node to the tree.
    164213                        $parent->children[] = $node;
    165214                }
     215        }
    166216
     217        protected function _render() {
     218                global $is_IE, $is_iphone;
     219
    167220                // Add browser classes.
    168221                // We have to do this here since admin bar shows on the front end.
    169222                $class = 'nojq nojs';
     
    181234                ?>
    182235                <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
    183236                        <div class="quicklinks" role="menubar">
    184                                 <?php foreach ( $this->root->children as $group ) {
    185                                         $this->render_group( $group, 'ab-top-menu' );
     237                                <?php foreach ( $this->get_node( 'root' )->children as $group ) {
     238                                        $this->_render_group( $group, 'ab-top-menu' );
    186239                                } ?>
    187240                        </div>
    188241                </div>
     
    190243                <?php
    191244        }
    192245
    193         private function render_group( $node, $class = '' ) {
     246        protected function _render_group( $node, $class = '' ) {
    194247                if ( ! $node->group )
    195248                        return;
    196249
     
    211264
    212265                $is_single_group = count( $groups ) === 1;
    213266
    214 
    215267                // If we don't have any subgroups, render the group.
    216                 if ( $is_single_group && ! empty( $node->children ) ):
     268                if ( $is_single_group && ! empty( $node->children ) ) :
    217269
    218270                        if ( ! empty( $node->meta['class'] ) )
    219271                                $class .= ' ' . $node->meta['class'];
    220272
    221273                        ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php
    222274                                foreach ( $node->children as $item ) {
    223                                         $this->render_item( $item );
     275                                        $this->_render_item( $item );
    224276                                }
    225277                        ?></ul><?php
    226278
    227279                // Wrap the subgroups in a div and render each individual subgroup.
    228                 elseif ( ! $is_single_group ):
     280                elseif ( ! $is_single_group ) :
    229281                        ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php
    230282                                foreach ( $groups as $group ) {
    231                                         $this->render_group( $group, $class );
     283                                        $this->_render_group( $group, $class );
    232284                                }
    233285                        ?></div><?php
    234286                endif;
    235287        }
    236288
    237         private function render_item( $node ) {
     289        protected function _render_item( $node ) {
    238290                if ( $node->group )
    239291                        return;
    240292
    241                 $is_parent = (bool) $node->children;
    242                 $has_link  = (bool) $node->href;
    243                 $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10;
     293                $is_parent = ! empty( $node->children );
     294                $has_link  = ! empty( $node->href );
    244295
     296                $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
     297
    245298                $menuclass = '';
    246299                $aria_attributes = 'tabindex="' . $tabindex . '" role="menuitem"';
    247300
     
    269322                                endif;
    270323                                ?>><?php
    271324                        else:
    272                                 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?>><?php
     325                                ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?>"><?php
    273326                        endif;
    274327
    275328                        echo $node->title;
     
    283336                        if ( $is_parent ) :
    284337                                ?><div class="ab-sub-wrapper"><?php
    285338                                        foreach ( $node->children as $group ) {
    286                                                 $this->render_group( $group, 'ab-submenu' );
     339                                                $this->_render_group( $group, 'ab-submenu' );
    287340                                        }
    288341                                ?></div><?php
    289342                        endif;
     
    295348                </li><?php
    296349        }
    297350
    298         function recursive_render( $node ) {
    299                 $this->render_item( $node );
     351        public function recursive_render( $id, $node ) {
     352                _deprecated_function( __METHOD__, '3.3', 'WP_Admin_Bar::_render_item()' );
     353                $this->_render_item( $node );
    300354        }
    301355
    302         function add_menus() {
     356        public function add_menus() {
    303357                // User related, aligned right.
    304358                add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
    305359