mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Admin Bar: Secondary is so passé. Groups are the new black. fixes #19136.
git-svn-id: https://develop.svn.wordpress.org/trunk@19429 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -12,10 +12,10 @@ class WP_Admin_Bar {
|
||||
$this->proto = 'https://';
|
||||
|
||||
$this->user = new stdClass;
|
||||
$this->root = new stdClass;
|
||||
$this->root->children = (object) array(
|
||||
'primary' => array(),
|
||||
'secondary' => array(),
|
||||
$this->root = (object) array(
|
||||
'id' => 'root',
|
||||
'group' => false,
|
||||
'children' => array(),
|
||||
);
|
||||
|
||||
if ( is_user_logged_in() ) {
|
||||
@@ -68,7 +68,7 @@ class WP_Admin_Bar {
|
||||
* - title - string - The title of the node.
|
||||
* - parent - string - The ID of the parent node. Optional.
|
||||
* - href - string - The link for the item. Optional.
|
||||
* - secondary - boolean - If the item should be part of a secondary menu. Optional. Default false.
|
||||
* - group - boolean - If the node is a group. Optional. Default false.
|
||||
* - meta - array - Meta data including the following keys: html, class, onclick, target, title.
|
||||
*/
|
||||
public function add_node( $args ) {
|
||||
@@ -77,21 +77,22 @@ class WP_Admin_Bar {
|
||||
$args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
|
||||
|
||||
// Ensure we have a valid title.
|
||||
if ( empty( $args['title'] ) )
|
||||
return false;
|
||||
|
||||
if ( empty( $args['id'] ) ) {
|
||||
if ( empty( $args['title'] ) )
|
||||
return;
|
||||
|
||||
_doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
|
||||
// Deprecated: Generate an ID from the title.
|
||||
$args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
'id' => false,
|
||||
'title' => false,
|
||||
'parent' => false,
|
||||
'href' => false,
|
||||
'secondary' => false,
|
||||
'meta' => array(),
|
||||
'id' => false,
|
||||
'title' => false,
|
||||
'parent' => false,
|
||||
'href' => false,
|
||||
'group' => false,
|
||||
'meta' => array(),
|
||||
);
|
||||
|
||||
// If the node already exists, keep any data that isn't provided.
|
||||
@@ -99,14 +100,25 @@ class WP_Admin_Bar {
|
||||
$defaults = (array) $this->nodes[ $args['id'] ];
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$args['children'] = (object) array(
|
||||
'primary' => array(),
|
||||
'secondary' => array(),
|
||||
);
|
||||
$args['children'] = array();
|
||||
|
||||
$this->nodes[ $args['id'] ] = (object) $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a group to a menu node.
|
||||
*
|
||||
* @param array $args - The arguments for each node.
|
||||
* - id - string - The ID of the item.
|
||||
* - parent - string - The ID of the parent node. Optional. Default root.
|
||||
* - meta - array - Meta data including the following keys: class, onclick, target, title.
|
||||
*/
|
||||
public function add_group( $args ) {
|
||||
$args['group'] = true;
|
||||
|
||||
$this->add_node( $args );
|
||||
}
|
||||
|
||||
public function remove_node( $id ) {
|
||||
unset( $this->nodes[ $id ] );
|
||||
}
|
||||
@@ -136,10 +148,27 @@ class WP_Admin_Bar {
|
||||
$parent = $this->nodes[ $node->parent ];
|
||||
}
|
||||
|
||||
if ( $node->secondary )
|
||||
$parent->children->secondary[] = $node;
|
||||
else
|
||||
$parent->children->primary[] = $node;
|
||||
|
||||
// Ensure that our tree is of the form "item -> group -> item -> group -> ..."
|
||||
if ( ! $parent->group && ! $node->group ) { // Both are items.
|
||||
// The default group is added here to allow groups that are
|
||||
// added before standard menu items to render first.
|
||||
if ( ! isset( $parent->children['default'] ) ) {
|
||||
$parent->children['default'] = (object) array(
|
||||
'id' => "{$parent->id}-default",
|
||||
'parent' => $parent->id,
|
||||
'group' => true,
|
||||
'children' => array(),
|
||||
);
|
||||
}
|
||||
$parent = $parent->children['default'];
|
||||
}
|
||||
|
||||
// Update the parent ID (it might have changed).
|
||||
$node->parent = $parent->id;
|
||||
|
||||
// Add the node to the tree.
|
||||
$parent->children[] = $node;
|
||||
}
|
||||
|
||||
// Add browser classes.
|
||||
@@ -159,33 +188,64 @@ class WP_Admin_Bar {
|
||||
?>
|
||||
<div id="wpadminbar" class="<?php echo $class; ?>">
|
||||
<div class="quicklinks">
|
||||
<ul class="ab-top-menu"><?php
|
||||
|
||||
foreach ( $this->root->children->primary as $node ) {
|
||||
$this->recursive_render( $node );
|
||||
}
|
||||
|
||||
?></ul>
|
||||
<ul class="ab-top-menu ab-top-secondary"><?php
|
||||
|
||||
foreach ( $this->root->children->secondary as $node ) {
|
||||
$this->recursive_render( $node );
|
||||
}
|
||||
|
||||
?></ul>
|
||||
<?php foreach ( $this->root->children as $group ) {
|
||||
$this->render_group( $group, 'ab-top-menu' );
|
||||
} ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
function recursive_render( $node ) {
|
||||
if ( ! $node->children->primary && $node->children->secondary ) {
|
||||
$node->children->primary = $node->children->secondary;
|
||||
$node->children->secondary = array();
|
||||
private function render_group( $node, $class = '' ) {
|
||||
if ( ! $node->group )
|
||||
return;
|
||||
|
||||
// Check for groups within groups.
|
||||
$groups = array();
|
||||
foreach ( $node->children as $child ) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
$is_parent = (bool) $node->children->primary;
|
||||
$is_single_group = count( $groups ) === 1;
|
||||
|
||||
|
||||
// If we don't have any subgroups, render the group.
|
||||
if ( $is_single_group && ! empty( $node->children ) ):
|
||||
|
||||
if ( ! empty( $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
|
||||
foreach ( $node->children as $item ) {
|
||||
$this->render_item( $item );
|
||||
}
|
||||
?></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;
|
||||
}
|
||||
|
||||
private function render_item( $node ) {
|
||||
if ( $node->group )
|
||||
return;
|
||||
|
||||
$is_parent = (bool) $node->children;
|
||||
$has_link = (bool) $node->href;
|
||||
|
||||
$menuclass = $is_parent ? 'menupop' : '';
|
||||
@@ -222,23 +282,9 @@ class WP_Admin_Bar {
|
||||
|
||||
if ( $is_parent ) :
|
||||
?><div class="ab-sub-wrapper"><?php
|
||||
|
||||
// Render primary submenu
|
||||
?><ul class="ab-submenu"><?php
|
||||
foreach ( $node->children->primary as $child_node ) {
|
||||
$this->recursive_render( $child_node );
|
||||
foreach ( $node->children as $group ) {
|
||||
$this->render_group( $group, 'ab-submenu' );
|
||||
}
|
||||
?></ul><?php
|
||||
|
||||
// Render secondary submenu
|
||||
if ( ! empty( $node->children->secondary ) ):
|
||||
?><ul class="ab-submenu ab-sub-secondary"><?php
|
||||
foreach ( $node->children->secondary as $child_node ) {
|
||||
$this->recursive_render( $child_node );
|
||||
}
|
||||
?></ul><?php
|
||||
endif;
|
||||
|
||||
?></div><?php
|
||||
endif;
|
||||
|
||||
@@ -247,7 +293,10 @@ class WP_Admin_Bar {
|
||||
|
||||
?>
|
||||
</li><?php
|
||||
}
|
||||
|
||||
function recursive_render( $node ) {
|
||||
$this->render_item( $node );
|
||||
}
|
||||
|
||||
function add_menus() {
|
||||
@@ -271,6 +320,8 @@ class WP_Admin_Bar {
|
||||
if ( ! is_admin() )
|
||||
add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
|
||||
|
||||
add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
|
||||
|
||||
do_action( 'add_admin_bar_menus' );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user