git-svn-id: http://svn.automattic.com/wordpress/trunk@19558 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
086828df52
commit
06a1675a3c
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
class WP_Admin_Bar {
|
class WP_Admin_Bar {
|
||||||
private $nodes = array();
|
private $nodes = array();
|
||||||
|
private $bound = false;
|
||||||
public $user;
|
public $user;
|
||||||
|
|
||||||
public function __get( $name ) {
|
public function __get( $name ) {
|
||||||
|
@ -18,11 +19,6 @@ class WP_Admin_Bar {
|
||||||
public function initialize() {
|
public function initialize() {
|
||||||
$this->user = new stdClass;
|
$this->user = new stdClass;
|
||||||
|
|
||||||
$this->add_node( array(
|
|
||||||
'id' => 'root',
|
|
||||||
'group' => false,
|
|
||||||
) );
|
|
||||||
|
|
||||||
if ( is_user_logged_in() ) {
|
if ( is_user_logged_in() ) {
|
||||||
/* Populate settings we need for the menu based on the current user. */
|
/* Populate settings we need for the menu based on the current user. */
|
||||||
$this->user->blogs = get_blogs_of_user( get_current_user_id() );
|
$this->user->blogs = get_blogs_of_user( get_current_user_id() );
|
||||||
|
@ -104,8 +100,8 @@ class WP_Admin_Bar {
|
||||||
);
|
);
|
||||||
|
|
||||||
// If the node already exists, keep any data that isn't provided.
|
// If the node already exists, keep any data that isn't provided.
|
||||||
if ( $this->get_node( $args['id'] ) )
|
if ( $maybe_defaults = $this->get_node( $args['id'] ) )
|
||||||
$defaults = get_object_vars( $this->get_node( $args['id'] ) );
|
$defaults = get_object_vars( $maybe_defaults );
|
||||||
|
|
||||||
// Do the same for 'meta' items.
|
// Do the same for 'meta' items.
|
||||||
if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
|
if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
|
||||||
|
@ -114,8 +110,8 @@ class WP_Admin_Bar {
|
||||||
$args = wp_parse_args( $args, $defaults );
|
$args = wp_parse_args( $args, $defaults );
|
||||||
|
|
||||||
$back_compat_parents = array(
|
$back_compat_parents = array(
|
||||||
'my-account-with-avatar' => array( 'my-account', '3.3' ),
|
'my-account-with-avatar' => array( 'my-account', '3.3' ),
|
||||||
'my-blogs' => array( 'my-sites', '3.3' ),
|
'my-blogs' => array( 'my-sites', '3.3' ),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
|
if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
|
||||||
|
@ -137,11 +133,35 @@ class WP_Admin_Bar {
|
||||||
* @return object Node.
|
* @return object Node.
|
||||||
*/
|
*/
|
||||||
final public function get_node( $id ) {
|
final public function get_node( $id ) {
|
||||||
|
if ( $node = $this->_get_node( $id ) )
|
||||||
|
return clone $node;
|
||||||
|
}
|
||||||
|
|
||||||
|
final protected function _get_node( $id ) {
|
||||||
|
if ( $this->bound )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ( empty( $id ) )
|
||||||
|
$id = 'root';
|
||||||
|
|
||||||
if ( isset( $this->nodes[ $id ] ) )
|
if ( isset( $this->nodes[ $id ] ) )
|
||||||
return $this->nodes[ $id ];
|
return $this->nodes[ $id ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final public function get_nodes() {
|
||||||
|
if ( ! $nodes = $this->_get_nodes() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach ( $nodes as &$node ) {
|
||||||
|
$node = clone $node;
|
||||||
|
}
|
||||||
|
return $nodes;
|
||||||
|
}
|
||||||
|
|
||||||
final protected function _get_nodes() {
|
final protected function _get_nodes() {
|
||||||
|
if ( $this->bound )
|
||||||
|
return;
|
||||||
|
|
||||||
return $this->nodes;
|
return $this->nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,50 +195,131 @@ class WP_Admin_Bar {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render() {
|
public function render() {
|
||||||
$this->_bind();
|
$root = $this->_bind();
|
||||||
$this->_render();
|
$this->_render( $root );
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected function _bind() {
|
final protected function _bind() {
|
||||||
|
if ( $this->bound )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Add the root node.
|
||||||
|
// Clear it first, just in case. Don't mess with The Root.
|
||||||
|
$this->remove_node( 'root' );
|
||||||
|
$this->add_node( array(
|
||||||
|
'id' => 'root',
|
||||||
|
'group' => false,
|
||||||
|
) );
|
||||||
|
|
||||||
|
// Normalize nodes: define internal 'children' and 'type' properties.
|
||||||
|
foreach ( $this->_get_nodes() as $node ) {
|
||||||
|
$node->children = array();
|
||||||
|
$node->type = ( $node->group ) ? 'group' : 'item';
|
||||||
|
unset( $node->group );
|
||||||
|
|
||||||
|
// The Root wants your orphans. No lonely items allowed.
|
||||||
|
if ( ! $node->parent )
|
||||||
|
$node->parent = 'root';
|
||||||
|
}
|
||||||
|
|
||||||
foreach ( $this->_get_nodes() as $node ) {
|
foreach ( $this->_get_nodes() as $node ) {
|
||||||
if ( 'root' == $node->id )
|
if ( 'root' == $node->id )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Handle root menu items
|
// Fetch the parent node. If it isn't registered, ignore the node.
|
||||||
if ( empty( $node->parent ) ) {
|
if ( ! $parent = $this->_get_node( $node->parent ) ) {
|
||||||
$parent = $this->get_node( 'root' );
|
|
||||||
} elseif ( ! $parent = $this->get_node( $node->parent ) ) {
|
|
||||||
// If the parent node isn't registered, ignore the node.
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that our tree is of the form "item -> group -> item -> group -> ..."
|
// Generate the group class (we distinguish between top level and other level groups).
|
||||||
if ( ! $parent->group && ! $node->group ) { // Both are items.
|
$group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
|
||||||
|
|
||||||
|
if ( $node->type == 'group' ) {
|
||||||
|
if ( empty( $node->meta['class'] ) )
|
||||||
|
$node->meta['class'] = '';
|
||||||
|
$node->meta['class'] .= ' ' . $group_class;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Items in items aren't allowed. Wrap nested items in 'default' groups.
|
||||||
|
if ( $parent->type == 'item' && $node->type == 'item' ) {
|
||||||
|
$default_id = $parent->id . '-default';
|
||||||
|
$default = $this->_get_node( $default_id );
|
||||||
|
|
||||||
// The default group is added here to allow groups that are
|
// The default group is added here to allow groups that are
|
||||||
// added before standard menu items to render first.
|
// added before standard menu items to render first.
|
||||||
if ( ! isset( $parent->children['default'] ) ) {
|
if ( ! $default ) {
|
||||||
$parent->children['default'] = (object) array(
|
// Use _set_node because add_node can be overloaded.
|
||||||
'id' => "{$parent->id}-default",
|
// Make sure to specify default settings for all properties.
|
||||||
'parent' => $parent->id,
|
$this->_set_node( array(
|
||||||
'group' => true,
|
'id' => $default_id,
|
||||||
'children' => array(),
|
'parent' => $parent->id,
|
||||||
);
|
'type' => 'group',
|
||||||
|
'children' => array(),
|
||||||
|
'meta' => array(
|
||||||
|
'class' => $group_class,
|
||||||
|
),
|
||||||
|
'title' => false,
|
||||||
|
'href' => false,
|
||||||
|
) );
|
||||||
|
$default = $this->_get_node( $default_id );
|
||||||
|
$parent->children[] = $default;
|
||||||
}
|
}
|
||||||
$parent = $parent->children['default'];
|
$parent = $default;
|
||||||
|
|
||||||
|
// Groups in groups aren't allowed. Add a special 'container' node.
|
||||||
|
// The container will invisibly wrap both groups.
|
||||||
|
} elseif ( $parent->type == 'group' && $node->type == 'group' ) {
|
||||||
|
$container_id = $parent->id . '-container';
|
||||||
|
$container = $this->_get_node( $container_id );
|
||||||
|
|
||||||
|
// We need to create a container for this group, life is sad.
|
||||||
|
if ( ! $container ) {
|
||||||
|
// Use _set_node because add_node can be overloaded.
|
||||||
|
// Make sure to specify default settings for all properties.
|
||||||
|
$this->_set_node( array(
|
||||||
|
'id' => $container_id,
|
||||||
|
'type' => 'container',
|
||||||
|
'children' => array( $parent ),
|
||||||
|
'parent' => false,
|
||||||
|
'title' => false,
|
||||||
|
'href' => false,
|
||||||
|
'meta' => array(),
|
||||||
|
) );
|
||||||
|
|
||||||
|
$container = $this->_get_node( $container_id );
|
||||||
|
|
||||||
|
// Link the container node if a grandparent node exists.
|
||||||
|
$grandparent = $this->_get_node( $parent->parent );
|
||||||
|
|
||||||
|
if ( $grandparent ) {
|
||||||
|
$container->parent = $grandparent->id;
|
||||||
|
|
||||||
|
$index = array_search( $parent, $grandparent->children, true );
|
||||||
|
if ( $index === false )
|
||||||
|
$grandparent->children[] = $container;
|
||||||
|
else
|
||||||
|
array_splice( $grandparent->children, $index, 1, array( $container ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent->parent = $container->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = $container;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the parent ID (it might have changed).
|
// Update the parent ID (it might have changed).
|
||||||
$node->parent = $parent->id;
|
$node->parent = $parent->id;
|
||||||
|
|
||||||
if ( ! isset( $parent->children ) )
|
|
||||||
$parent->children = array();
|
|
||||||
|
|
||||||
// Add the node to the tree.
|
// Add the node to the tree.
|
||||||
$parent->children[] = $node;
|
$parent->children[] = $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$root = $this->_get_node( 'root' );
|
||||||
|
$this->bound = true;
|
||||||
|
return $root;
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected function _render() {
|
final protected function _render( $root ) {
|
||||||
global $is_IE, $is_iphone;
|
global $is_IE, $is_iphone;
|
||||||
|
|
||||||
// Add browser classes.
|
// Add browser classes.
|
||||||
|
@ -238,8 +339,8 @@ class WP_Admin_Bar {
|
||||||
?>
|
?>
|
||||||
<div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
|
<div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
|
||||||
<div class="quicklinks">
|
<div class="quicklinks">
|
||||||
<?php foreach ( $this->get_node( 'root' )->children as $group ) {
|
<?php foreach ( $root->children as $group ) {
|
||||||
$this->_render_group( $group, 'ab-top-menu' );
|
$this->_render_group( $group );
|
||||||
} ?>
|
} ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -247,51 +348,35 @@ class WP_Admin_Bar {
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected function _render_group( $node, $class = '' ) {
|
final protected function _render_container( $node ) {
|
||||||
if ( ! $node->group )
|
if ( $node->type != 'container' || empty( $node->children ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Check for groups within groups.
|
?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
|
||||||
$groups = array();
|
foreach ( $node->children as $group ) {
|
||||||
foreach ( $node->children as $child ) {
|
$this->_render_group( $group );
|
||||||
if ( $child->group ) {
|
|
||||||
$groups[] = $child;
|
|
||||||
} else {
|
|
||||||
if ( ! isset( $default ) ) {
|
|
||||||
// Create a default proxy item to be used in the case of nested groups.
|
|
||||||
$default = (object) wp_parse_args( array( 'children' => array() ), (array) $node );
|
|
||||||
$groups[] = $default;
|
|
||||||
}
|
|
||||||
$default->children[] = $child;
|
|
||||||
}
|
}
|
||||||
}
|
?></div><?php
|
||||||
|
}
|
||||||
|
|
||||||
$is_single_group = count( $groups ) === 1;
|
final protected function _render_group( $node ) {
|
||||||
|
if ( $node->type == 'container' )
|
||||||
|
return $this->_render_container( $node );
|
||||||
|
|
||||||
// If we don't have any subgroups, render the group.
|
if ( $node->type != 'group' || empty( $node->children ) )
|
||||||
if ( $is_single_group && ! empty( $node->children ) ) :
|
return;
|
||||||
|
|
||||||
if ( ! empty( $node->meta['class'] ) )
|
$class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
|
||||||
$class .= ' ' . $node->meta['class'];
|
|
||||||
|
|
||||||
?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
|
?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
|
||||||
foreach ( $node->children as $item ) {
|
foreach ( $node->children as $item ) {
|
||||||
$this->_render_item( $item );
|
$this->_render_item( $item );
|
||||||
}
|
}
|
||||||
?></ul><?php
|
?></ul><?php
|
||||||
|
|
||||||
// Wrap the subgroups in a div and render each individual subgroup.
|
|
||||||
elseif ( ! $is_single_group ) :
|
|
||||||
?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php
|
|
||||||
foreach ( $groups as $group ) {
|
|
||||||
$this->_render_group( $group, $class );
|
|
||||||
}
|
|
||||||
?></div><?php
|
|
||||||
endif;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected function _render_item( $node ) {
|
final protected function _render_item( $node ) {
|
||||||
if ( $node->group )
|
if ( $node->type != 'item' )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$is_parent = ! empty( $node->children );
|
$is_parent = ! empty( $node->children );
|
||||||
|
@ -312,7 +397,7 @@ class WP_Admin_Bar {
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
|
<li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
|
||||||
if ( $has_link ):
|
if ( $has_link ):
|
||||||
?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
|
?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
|
||||||
if ( ! empty( $node->meta['onclick'] ) ) :
|
if ( ! empty( $node->meta['onclick'] ) ) :
|
||||||
|
@ -344,7 +429,7 @@ class WP_Admin_Bar {
|
||||||
if ( $is_parent ) :
|
if ( $is_parent ) :
|
||||||
?><div class="ab-sub-wrapper"><?php
|
?><div class="ab-sub-wrapper"><?php
|
||||||
foreach ( $node->children as $group ) {
|
foreach ( $node->children as $group ) {
|
||||||
$this->_render_group( $group, 'ab-submenu' );
|
$this->_render_group( $group );
|
||||||
}
|
}
|
||||||
?></div><?php
|
?></div><?php
|
||||||
endif;
|
endif;
|
||||||
|
|
Loading…
Reference in New Issue