| 1 | Index: wp-includes/class-wp-admin-bar.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- wp-includes/class-wp-admin-bar.php (revision 19491) |
|---|
| 4 | +++ wp-includes/class-wp-admin-bar.php (working copy) |
|---|
| 5 | @@ -1,22 +1,27 @@ |
|---|
| 6 | <?php |
|---|
| 7 | class WP_Admin_Bar { |
|---|
| 8 | private $nodes = array(); |
|---|
| 9 | - private $root = array(); |
|---|
| 10 | - |
|---|
| 11 | - public $proto = 'http://'; |
|---|
| 12 | public $user; |
|---|
| 13 | |
|---|
| 14 | - function initialize() { |
|---|
| 15 | - /* Set the protocol used throughout this code */ |
|---|
| 16 | - if ( is_ssl() ) |
|---|
| 17 | - $this->proto = 'https://'; |
|---|
| 18 | + public function __get( $name ) { |
|---|
| 19 | + switch ( $name ) { |
|---|
| 20 | + case 'proto' : |
|---|
| 21 | + return is_ssl() ? 'https://' : 'http://'; |
|---|
| 22 | + break; |
|---|
| 23 | + case 'menu' : |
|---|
| 24 | + $this->_bind(); |
|---|
| 25 | + return $this->get_nodes(); |
|---|
| 26 | + break; |
|---|
| 27 | + } |
|---|
| 28 | + } |
|---|
| 29 | |
|---|
| 30 | + public function initialize() { |
|---|
| 31 | $this->user = new stdClass; |
|---|
| 32 | - $this->root = (object) array( |
|---|
| 33 | + |
|---|
| 34 | + $this->add_node( array( |
|---|
| 35 | 'id' => 'root', |
|---|
| 36 | 'group' => false, |
|---|
| 37 | - 'children' => array(), |
|---|
| 38 | - ); |
|---|
| 39 | + ) ); |
|---|
| 40 | |
|---|
| 41 | if ( is_user_logged_in() ) { |
|---|
| 42 | /* Populate settings we need for the menu based on the current user. */ |
|---|
| 43 | @@ -69,13 +74,16 @@ |
|---|
| 44 | * - parent - string - The ID of the parent node. Optional. |
|---|
| 45 | * - href - string - The link for the item. Optional. |
|---|
| 46 | * - group - boolean - If the node is a group. Optional. Default false. |
|---|
| 47 | - * - meta - array - Meta data including the following keys: html, class, onclick, target, title. |
|---|
| 48 | + * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex. |
|---|
| 49 | */ |
|---|
| 50 | public function add_node( $args ) { |
|---|
| 51 | // Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) |
|---|
| 52 | if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) |
|---|
| 53 | $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); |
|---|
| 54 | |
|---|
| 55 | + if ( is_object( $args ) ) |
|---|
| 56 | + $args = get_object_vars( $args ); |
|---|
| 57 | + |
|---|
| 58 | // Ensure we have a valid title. |
|---|
| 59 | if ( empty( $args['id'] ) ) { |
|---|
| 60 | if ( empty( $args['title'] ) ) |
|---|
| 61 | @@ -96,52 +104,90 @@ |
|---|
| 62 | ); |
|---|
| 63 | |
|---|
| 64 | // If the node already exists, keep any data that isn't provided. |
|---|
| 65 | - if ( isset( $this->nodes[ $args['id'] ] ) ) |
|---|
| 66 | - $defaults = (array) $this->nodes[ $args['id'] ]; |
|---|
| 67 | + if ( $this->get_node( $args['id'] ) ) |
|---|
| 68 | + $defaults = get_object_vars( $this->get_node( $args['id'] ) ); |
|---|
| 69 | |
|---|
| 70 | + // Do the same for 'meta' items. |
|---|
| 71 | + if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) ) |
|---|
| 72 | + $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] ); |
|---|
| 73 | + |
|---|
| 74 | $args = wp_parse_args( $args, $defaults ); |
|---|
| 75 | - $args['children'] = array(); |
|---|
| 76 | |
|---|
| 77 | + $this->_set_node( $args ); |
|---|
| 78 | + } |
|---|
| 79 | + |
|---|
| 80 | + final protected function _set_node( $args ) { |
|---|
| 81 | $this->nodes[ $args['id'] ] = (object) $args; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | + * Gets a node. |
|---|
| 86 | + * |
|---|
| 87 | + * @return object Node. |
|---|
| 88 | + */ |
|---|
| 89 | + final public function get_node( $id ) { |
|---|
| 90 | + if ( isset( $this->nodes[ $id ] ) ) |
|---|
| 91 | + return $this->nodes[ $id ]; |
|---|
| 92 | + } |
|---|
| 93 | + |
|---|
| 94 | + final protected function get_nodes() { |
|---|
| 95 | + return $this->nodes; |
|---|
| 96 | + } |
|---|
| 97 | + |
|---|
| 98 | + /** |
|---|
| 99 | * Add a group to a menu node. |
|---|
| 100 | * |
|---|
| 101 | + * @since 3.3.0 |
|---|
| 102 | + * |
|---|
| 103 | * @param array $args - The arguments for each node. |
|---|
| 104 | * - id - string - The ID of the item. |
|---|
| 105 | * - parent - string - The ID of the parent node. Optional. Default root. |
|---|
| 106 | * - meta - array - Meta data including the following keys: class, onclick, target, title. |
|---|
| 107 | */ |
|---|
| 108 | - public function add_group( $args ) { |
|---|
| 109 | + final public function add_group( $args ) { |
|---|
| 110 | $args['group'] = true; |
|---|
| 111 | |
|---|
| 112 | $this->add_node( $args ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | + /** |
|---|
| 116 | + * Remove a node. |
|---|
| 117 | + * |
|---|
| 118 | + * @return object The removed node. |
|---|
| 119 | + */ |
|---|
| 120 | public function remove_node( $id ) { |
|---|
| 121 | + $node = $this->get_node( $id ); |
|---|
| 122 | + $this->_unset_node( $id ); |
|---|
| 123 | + return $node; |
|---|
| 124 | + } |
|---|
| 125 | + |
|---|
| 126 | + final protected function _unset_node( $id ) { |
|---|
| 127 | unset( $this->nodes[ $id ] ); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | public function render() { |
|---|
| 131 | - global $is_IE, $is_iphone; |
|---|
| 132 | + $this->_bind(); |
|---|
| 133 | + $this->_render(); |
|---|
| 134 | + } |
|---|
| 135 | |
|---|
| 136 | - // Link nodes to parents. |
|---|
| 137 | - foreach ( $this->nodes as $node ) { |
|---|
| 138 | + final protected function _bind() { |
|---|
| 139 | + static $bound = false; |
|---|
| 140 | + if ( $bound ) |
|---|
| 141 | + return; |
|---|
| 142 | + $bound = true; |
|---|
| 143 | |
|---|
| 144 | + foreach ( $this->get_nodes() as $node ) { |
|---|
| 145 | + if ( 'root' == $node->id ) |
|---|
| 146 | + continue; |
|---|
| 147 | + |
|---|
| 148 | // Handle root menu items |
|---|
| 149 | if ( empty( $node->parent ) ) { |
|---|
| 150 | - $parent = $this->root; |
|---|
| 151 | - |
|---|
| 152 | - // If the parent node isn't registered, ignore the node. |
|---|
| 153 | - } elseif ( ! isset( $this->nodes[ $node->parent ] ) ) { |
|---|
| 154 | + $parent = $this->get_node( 'root' ); |
|---|
| 155 | + } elseif ( ! $parent = $this->get_node( $node->parent ) ) { |
|---|
| 156 | + // If the parent node isn't registered, ignore the node. |
|---|
| 157 | continue; |
|---|
| 158 | - |
|---|
| 159 | - } else { |
|---|
| 160 | - $parent = $this->nodes[ $node->parent ]; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | - |
|---|
| 164 | // Ensure that our tree is of the form "item -> group -> item -> group -> ..." |
|---|
| 165 | if ( ! $parent->group && ! $node->group ) { // Both are items. |
|---|
| 166 | // The default group is added here to allow groups that are |
|---|
| 167 | @@ -160,10 +206,17 @@ |
|---|
| 168 | // Update the parent ID (it might have changed). |
|---|
| 169 | $node->parent = $parent->id; |
|---|
| 170 | |
|---|
| 171 | + if ( ! isset( $parent->children ) ) |
|---|
| 172 | + $parent->children = array(); |
|---|
| 173 | + |
|---|
| 174 | // Add the node to the tree. |
|---|
| 175 | $parent->children[] = $node; |
|---|
| 176 | } |
|---|
| 177 | + } |
|---|
| 178 | |
|---|
| 179 | + protected function _render() { |
|---|
| 180 | + global $is_IE, $is_iphone; |
|---|
| 181 | + |
|---|
| 182 | // Add browser classes. |
|---|
| 183 | // We have to do this here since admin bar shows on the front end. |
|---|
| 184 | $class = 'nojq nojs'; |
|---|
| 185 | @@ -181,8 +234,8 @@ |
|---|
| 186 | ?> |
|---|
| 187 | <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation"> |
|---|
| 188 | <div class="quicklinks" role="menubar"> |
|---|
| 189 | - <?php foreach ( $this->root->children as $group ) { |
|---|
| 190 | - $this->render_group( $group, 'ab-top-menu' ); |
|---|
| 191 | + <?php foreach ( $this->get_node( 'root' )->children as $group ) { |
|---|
| 192 | + $this->_render_group( $group, 'ab-top-menu' ); |
|---|
| 193 | } ?> |
|---|
| 194 | </div> |
|---|
| 195 | </div> |
|---|
| 196 | @@ -190,7 +243,7 @@ |
|---|
| 197 | <?php |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | - private function render_group( $node, $class = '' ) { |
|---|
| 201 | + protected function _render_group( $node, $class = '' ) { |
|---|
| 202 | if ( ! $node->group ) |
|---|
| 203 | return; |
|---|
| 204 | |
|---|
| 205 | @@ -211,37 +264,37 @@ |
|---|
| 206 | |
|---|
| 207 | $is_single_group = count( $groups ) === 1; |
|---|
| 208 | |
|---|
| 209 | - |
|---|
| 210 | // If we don't have any subgroups, render the group. |
|---|
| 211 | - if ( $is_single_group && ! empty( $node->children ) ): |
|---|
| 212 | + if ( $is_single_group && ! empty( $node->children ) ) : |
|---|
| 213 | |
|---|
| 214 | if ( ! empty( $node->meta['class'] ) ) |
|---|
| 215 | $class .= ' ' . $node->meta['class']; |
|---|
| 216 | |
|---|
| 217 | ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php |
|---|
| 218 | foreach ( $node->children as $item ) { |
|---|
| 219 | - $this->render_item( $item ); |
|---|
| 220 | + $this->_render_item( $item ); |
|---|
| 221 | } |
|---|
| 222 | ?></ul><?php |
|---|
| 223 | |
|---|
| 224 | // Wrap the subgroups in a div and render each individual subgroup. |
|---|
| 225 | - elseif ( ! $is_single_group ): |
|---|
| 226 | + elseif ( ! $is_single_group ) : |
|---|
| 227 | ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php |
|---|
| 228 | foreach ( $groups as $group ) { |
|---|
| 229 | - $this->render_group( $group, $class ); |
|---|
| 230 | + $this->_render_group( $group, $class ); |
|---|
| 231 | } |
|---|
| 232 | ?></div><?php |
|---|
| 233 | endif; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | - private function render_item( $node ) { |
|---|
| 237 | + protected function _render_item( $node ) { |
|---|
| 238 | if ( $node->group ) |
|---|
| 239 | return; |
|---|
| 240 | |
|---|
| 241 | - $is_parent = (bool) $node->children; |
|---|
| 242 | - $has_link = (bool) $node->href; |
|---|
| 243 | - $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10; |
|---|
| 244 | + $is_parent = ! empty( $node->children ); |
|---|
| 245 | + $has_link = ! empty( $node->href ); |
|---|
| 246 | |
|---|
| 247 | + $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10; |
|---|
| 248 | + |
|---|
| 249 | $menuclass = ''; |
|---|
| 250 | $aria_attributes = 'tabindex="' . $tabindex . '" role="menuitem"'; |
|---|
| 251 | |
|---|
| 252 | @@ -269,7 +322,7 @@ |
|---|
| 253 | endif; |
|---|
| 254 | ?>><?php |
|---|
| 255 | else: |
|---|
| 256 | - ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?>><?php |
|---|
| 257 | + ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?>"><?php |
|---|
| 258 | endif; |
|---|
| 259 | |
|---|
| 260 | echo $node->title; |
|---|
| 261 | @@ -283,7 +336,7 @@ |
|---|
| 262 | if ( $is_parent ) : |
|---|
| 263 | ?><div class="ab-sub-wrapper"><?php |
|---|
| 264 | foreach ( $node->children as $group ) { |
|---|
| 265 | - $this->render_group( $group, 'ab-submenu' ); |
|---|
| 266 | + $this->_render_group( $group, 'ab-submenu' ); |
|---|
| 267 | } |
|---|
| 268 | ?></div><?php |
|---|
| 269 | endif; |
|---|
| 270 | @@ -295,11 +348,12 @@ |
|---|
| 271 | </li><?php |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | - function recursive_render( $node ) { |
|---|
| 275 | - $this->render_item( $node ); |
|---|
| 276 | + public function recursive_render( $id, $node ) { |
|---|
| 277 | + _deprecated_function( __METHOD__, '3.3', 'WP_Admin_Bar::_render_item()' ); |
|---|
| 278 | + $this->_render_item( $node ); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | - function add_menus() { |
|---|
| 282 | + public function add_menus() { |
|---|
| 283 | // User related, aligned right. |
|---|
| 284 | add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 ); |
|---|
| 285 | |
|---|