Make WordPress Core


Ignore:
Timestamp:
12/01/2011 12:25:04 AM (15 years ago)
Author:
nacin
Message:

Finalize the WP_Admin_Bar architecture for 3.3.

  • Introduce a get_node() method for plugins.
  • Deprecate $wp_admin_bar->menu. Plugins will need to use get_node(), remove_node(), add_node() to make modifications. This finalizes a backwards incompatible change made earlier in the cycle.
  • Allow add_node() to take a node object (which could come from get_node(), then be modified).
  • Ensure that our underlying storage (the nodes property) is private to core. Introduce _set_node, _unset_node, _get_nodes, get_nodes as the only ways to interface with this.
  • Protect and finalize _render_item, and _render_group. render() remains public and technically overridable, though I would discourage this of plugin authors.
  • Deprecate recursive_render(). Use render() or _render_item().

More about the internals:

  • Late-binds a node's 'children' array.
  • Eliminates the root property, leverages a 'root' node.
  • Splits render() into _bind() and _render(), both protected and finalized.

Fixes #19371.

File:
1 edited

Legend:

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

    r19499 r19501  
    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://';
    13 
     6        public function __get( $name ) {
     7                switch ( $name ) {
     8                        case 'proto' :
     9                                return is_ssl() ? 'https://' : 'http://';
     10                                break;
     11                        case 'menu' :
     12                                _deprecated_argument( 'WP_Admin_Bar', '3.3', 'Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node(), not the <code>menu</code> property.' );
     13                                return array(); // Sorry, folks.
     14                                break;
     15                }
     16        }
     17
     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() ) {
    … …  
    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 ) {
    … …  
    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) );
     83
     84                if ( is_object( $args ) )
     85                        $args = get_object_vars( $args );
    7886
    7987                // Ensure we have a valid title.
    … …  
    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'] ) );
     109
     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'] );
    101113
    102114                $args = wp_parse_args( $args, $defaults );
    103                 $args['children'] = array();
    104 
     115
     116                $this->_set_node( $args );
     117        }
     118
     119        final protected function _set_node( $args ) {
    105120                $this->nodes[ $args['id'] ] = (object) $args;
     121        }
     122
     123        /**
     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;
    106135        }
    107136
    108137        /**
    109138         * Add a group to a menu node.
     139         *
     140         * @since 3.3.0
    110141         *
    111142         * @param array $args - The arguments for each node.
    … …  
    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
    … …  
    120151        }
    121152
     153        /**
     154         * Remove a node.
     155         *
     156         * @return object The removed node.
     157         */
    122158        public function remove_node( $id ) {
     159                $this->_unset_node( $id );
     160        }
     161
     162        final protected function _unset_node( $id ) {
    123163                unset( $this->nodes[ $id ] );
    124164        }
    125165
    126166        public function render() {
    127                 global $is_IE, $is_iphone;
    128 
    129                 // Link nodes to parents.
    130                 foreach ( $this->nodes as $node ) {
     167                $this->_bind();
     168                $this->_render();
     169        }
     170
     171        final protected function _bind() {
     172                foreach ( $this->_get_nodes() as $node ) {
     173                        if ( 'root' == $node->id )
     174                                continue;
    131175
    132176                        // Handle root menu items
    133177                        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 ] ) ) {
     178                                $parent = $this->get_node( 'root' );
     179                        } elseif ( ! $parent = $this->get_node( $node->parent ) ) {
     180                                // If the parent node isn't registered, ignore the node.
    138181                                continue;
    139 
    140                         } else {
    141                                 $parent = $this->nodes[ $node->parent ];
    142182                        }
    143 
    144183
    145184                        // Ensure that our tree is of the form "item -> group -> item -> group -> ..."
    … …  
    161200                        $node->parent = $parent->id;
    162201
     202                        if ( ! isset( $parent->children ) )
     203                                $parent->children = array();
     204
    163205                        // Add the node to the tree.
    164206                        $parent->children[] = $node;
    165207                }
     208        }
     209
     210        final protected function _render() {
     211                global $is_IE, $is_iphone;
    166212
    167213                // Add browser classes.
    … …  
    182228                <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
    183229                        <div class="quicklinks" role="menubar">
    184                                 <?php foreach ( $this->root->children as $group ) {
    185                                         $this->render_group( $group, 'ab-top-menu' );
     230                                <?php foreach ( $this->get_node( 'root' )->children as $group ) {
     231                                        $this->_render_group( $group, 'ab-top-menu' );
    186232                                } ?>
    187233                        </div>
    … …  
    191237        }
    192238
    193         private function render_group( $node, $class = '' ) {
     239        final protected function _render_group( $node, $class = '' ) {
    194240                if ( ! $node->group )
    195241                        return;
    … …  
    212258                $is_single_group = count( $groups ) === 1;
    213259
    214 
    215260                // If we don't have any subgroups, render the group.
    216                 if ( $is_single_group && ! empty( $node->children ) ):
     261                if ( $is_single_group && ! empty( $node->children ) ) :
    217262
    218263                        if ( ! empty( $node->meta['class'] ) )
    … …  
    221266                        ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php
    222267                                foreach ( $node->children as $item ) {
    223                                         $this->render_item( $item );
     268                                        $this->_render_item( $item );
    224269                                }
    225270                        ?></ul><?php
    226271
    227272                // Wrap the subgroups in a div and render each individual subgroup.
    228                 elseif ( ! $is_single_group ):
     273                elseif ( ! $is_single_group ) :
    229274                        ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php
    230275                                foreach ( $groups as $group ) {
    231                                         $this->render_group( $group, $class );
     276                                        $this->_render_group( $group, $class );
    232277                                }
    233278                        ?></div><?php
    … …  
    235280        }
    236281
    237         private function render_item( $node ) {
     282        final protected function _render_item( $node ) {
    238283                if ( $node->group )
    239284                        return;
    240285
    241                 $is_parent = (bool) $node->children;
    242                 $has_link  = (bool) $node->href;
    243                 $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10;
     286                $is_parent = ! empty( $node->children );
     287                $has_link  = ! empty( $node->href );
     288
     289                $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
    244290
    245291                $menuclass = '';
    … …  
    279325                        echo $node->title;
    280326
    281                         if ( $has_link ):
     327                        if ( $has_link ) :
    282328                                ?></a><?php
    283329                        else:
    … …  
    288334                                ?><div class="ab-sub-wrapper"><?php
    289335                                        foreach ( $node->children as $group ) {
    290                                                 $this->render_group( $group, 'ab-submenu' );
     336                                                $this->_render_group( $group, 'ab-submenu' );
    291337                                        }
    292338                                ?></div><?php
    … …  
    300346        }
    301347
    302         function recursive_render( $node ) {
    303                 $this->render_item( $node );
    304         }
    305 
    306         function add_menus() {
     348        public function recursive_render( $id, $node ) {
     349                _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
     350                $this->_render_item( $node );
     351        }
     352
     353        public function add_menus() {
    307354                // User related, aligned right.
    308355                add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
Note: See TracChangeset for help on using the changeset viewer.