2010-09-27 16:26:36 -04:00
|
|
|
<?php
|
|
|
|
class WP_Admin_Bar {
|
2011-11-02 16:34:54 -04:00
|
|
|
private $nodes = array();
|
|
|
|
private $root = array();
|
|
|
|
|
|
|
|
public $proto = 'http://';
|
|
|
|
public $user;
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
function initialize() {
|
|
|
|
/* Set the protocol used throughout this code */
|
2010-11-17 13:47:34 -05:00
|
|
|
if ( is_ssl() )
|
2010-10-28 04:31:36 -04:00
|
|
|
$this->proto = 'https://';
|
2010-09-27 16:26:36 -04:00
|
|
|
|
|
|
|
$this->user = new stdClass;
|
2011-11-09 14:12:48 -05:00
|
|
|
$this->root = new stdClass;
|
|
|
|
$this->root->children = (object) array(
|
|
|
|
'primary' => array(),
|
|
|
|
'secondary' => array(),
|
|
|
|
);
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-09-16 06:56:06 -04:00
|
|
|
if ( is_user_logged_in() ) {
|
|
|
|
/* Populate settings we need for the menu based on the current user. */
|
|
|
|
$this->user->blogs = get_blogs_of_user( get_current_user_id() );
|
|
|
|
if ( is_multisite() ) {
|
|
|
|
$this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
|
|
|
|
$this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
|
|
|
|
$this->user->account_domain = $this->user->domain;
|
|
|
|
} else {
|
|
|
|
$this->user->active_blog = $this->user->blogs[get_current_blog_id()];
|
|
|
|
$this->user->domain = trailingslashit( home_url() );
|
|
|
|
$this->user->account_domain = $this->user->domain;
|
|
|
|
}
|
2010-09-27 16:26:36 -04:00
|
|
|
}
|
2010-10-28 04:31:36 -04:00
|
|
|
|
|
|
|
add_action( 'wp_head', 'wp_admin_bar_header' );
|
2010-10-29 03:25:58 -04:00
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
add_action( 'admin_head', 'wp_admin_bar_header' );
|
|
|
|
|
2010-11-30 16:50:57 -05:00
|
|
|
if ( current_theme_supports( 'admin-bar' ) ) {
|
|
|
|
$admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
|
|
|
|
$header_callback = $admin_bar_args[0]['callback'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty($header_callback) )
|
|
|
|
$header_callback = '_admin_bar_bump_cb';
|
|
|
|
|
|
|
|
add_action('wp_head', $header_callback);
|
|
|
|
|
2010-10-29 03:25:58 -04:00
|
|
|
wp_enqueue_script( 'admin-bar' );
|
2010-10-28 04:31:36 -04:00
|
|
|
wp_enqueue_style( 'admin-bar' );
|
|
|
|
|
|
|
|
do_action( 'admin_bar_init' );
|
2010-09-27 16:26:36 -04:00
|
|
|
}
|
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
public function add_menu( $node ) {
|
|
|
|
$this->add_node( $node );
|
|
|
|
}
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
public function remove_menu( $id ) {
|
|
|
|
$this->remove_node( $id );
|
|
|
|
}
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
/**
|
|
|
|
* Add a node to the menu.
|
|
|
|
*
|
|
|
|
* @param array $args - The arguments for each node.
|
2011-11-09 14:12:48 -05:00
|
|
|
* - id - string - The ID of the item.
|
|
|
|
* - 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.
|
|
|
|
* - meta - array - Meta data including the following keys: html, class, onclick, target, title.
|
2011-11-02 16:34:54 -04:00
|
|
|
*/
|
|
|
|
public function add_node( $args ) {
|
|
|
|
// Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
|
|
|
|
if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
|
|
|
|
$args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
|
|
|
|
|
2011-11-04 13:41:38 -04:00
|
|
|
// Ensure we have a valid title.
|
|
|
|
if ( empty( $args['title'] ) )
|
2010-09-27 16:26:36 -04:00
|
|
|
return false;
|
|
|
|
|
2011-11-04 13:41:38 -04:00
|
|
|
if ( empty( $args['id'] ) ) {
|
|
|
|
_doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
|
|
|
|
$args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
|
|
|
|
}
|
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
$defaults = array(
|
2011-11-09 14:12:48 -05:00
|
|
|
'id' => false,
|
|
|
|
'title' => false,
|
|
|
|
'parent' => false,
|
|
|
|
'href' => false,
|
|
|
|
'secondary' => false,
|
|
|
|
'meta' => array(),
|
2011-11-02 16:34:54 -04:00
|
|
|
);
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
// If the node already exists, keep any data that isn't provided.
|
|
|
|
if ( isset( $this->nodes[ $args['id'] ] ) )
|
|
|
|
$defaults = (array) $this->nodes[ $args['id'] ];
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
$args = wp_parse_args( $args, $defaults );
|
2011-11-09 14:12:48 -05:00
|
|
|
$args['children'] = (object) array(
|
|
|
|
'primary' => array(),
|
|
|
|
'secondary' => array(),
|
|
|
|
);
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
$this->nodes[ $args['id'] ] = (object) $args;
|
2010-09-27 16:26:36 -04:00
|
|
|
}
|
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
public function remove_node( $id ) {
|
|
|
|
unset( $this->nodes[ $id ] );
|
2010-09-27 16:26:36 -04:00
|
|
|
}
|
2010-10-28 11:46:11 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
public function render() {
|
|
|
|
// Link nodes to parents.
|
|
|
|
foreach ( $this->nodes as $node ) {
|
|
|
|
|
|
|
|
// Handle root menu items
|
|
|
|
if ( empty( $node->parent ) ) {
|
2011-11-09 14:12:48 -05:00
|
|
|
$parent = $this->root;
|
2011-11-02 16:34:54 -04:00
|
|
|
|
|
|
|
// If the parent node isn't registered, ignore the node.
|
2011-11-09 14:12:48 -05:00
|
|
|
} elseif ( ! isset( $this->nodes[ $node->parent ] ) ) {
|
2011-11-02 16:34:54 -04:00
|
|
|
continue;
|
|
|
|
|
2011-11-09 14:12:48 -05:00
|
|
|
} else {
|
|
|
|
$parent = $this->nodes[ $node->parent ];
|
|
|
|
}
|
2011-11-02 16:34:54 -04:00
|
|
|
|
2011-11-09 14:12:48 -05:00
|
|
|
if ( $node->secondary )
|
|
|
|
$parent->children->secondary[] = $node;
|
|
|
|
else
|
|
|
|
$parent->children->primary[] = $node;
|
2011-11-02 16:34:54 -04:00
|
|
|
}
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
?>
|
2011-09-25 19:30:40 -04:00
|
|
|
<div id="wpadminbar" class="nojq nojs">
|
2010-09-27 16:26:36 -04:00
|
|
|
<div class="quicklinks">
|
2011-11-02 16:34:54 -04:00
|
|
|
<ul class="ab-top-menu"><?php
|
|
|
|
|
2011-11-09 14:12:48 -05:00
|
|
|
foreach ( $this->root->children->primary as $node ) {
|
2011-11-02 16:34:54 -04:00
|
|
|
$this->recursive_render( $node );
|
|
|
|
}
|
|
|
|
|
2011-11-11 19:26:55 -05:00
|
|
|
?></ul>
|
|
|
|
<ul class="ab-top-menu ab-top-secondary"><?php
|
2011-11-09 14:12:48 -05:00
|
|
|
|
2011-11-11 19:26:55 -05:00
|
|
|
foreach ( $this->root->children->secondary as $node ) {
|
|
|
|
$this->recursive_render( $node );
|
|
|
|
}
|
2011-11-09 14:12:48 -05:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
?></ul>
|
2010-09-27 16:26:36 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
function recursive_render( $node ) {
|
2011-11-09 19:42:39 -05:00
|
|
|
if ( ! $node->children->primary && $node->children->secondary ) {
|
|
|
|
$node->children->primary = $node->children->secondary;
|
|
|
|
$node->children->secondary = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_parent = (bool) $node->children->primary;
|
2011-02-09 12:35:36 -05:00
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
$menuclass = $is_parent ? 'menupop' : '';
|
2011-11-02 16:34:54 -04:00
|
|
|
if ( ! empty( $node->meta['class'] ) )
|
|
|
|
$menuclass .= ' ' . $node->meta['class'];
|
2011-11-07 15:38:38 -05:00
|
|
|
|
|
|
|
$tabindex = !empty($node->meta['tabindex']) ? $node->meta['tabindex'] : 10;
|
2011-01-12 20:34:15 -05:00
|
|
|
?>
|
2010-10-28 04:31:36 -04:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
<li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
|
2011-11-07 15:38:38 -05:00
|
|
|
<a tabindex="<?php echo (int) $tabindex; ?>" href="<?php echo esc_url( $node->href ) ?>"<?php
|
2011-11-02 16:34:54 -04:00
|
|
|
if ( ! empty( $node->meta['onclick'] ) ) :
|
|
|
|
?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
|
2010-10-28 04:31:36 -04:00
|
|
|
endif;
|
2011-11-02 16:34:54 -04:00
|
|
|
if ( ! empty( $node->meta['target'] ) ) :
|
|
|
|
?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
|
2011-01-12 11:02:56 -05:00
|
|
|
endif;
|
2011-11-02 16:34:54 -04:00
|
|
|
if ( ! empty( $node->meta['title'] ) ) :
|
|
|
|
?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
|
2010-11-17 13:47:34 -05:00
|
|
|
endif;
|
|
|
|
|
|
|
|
?>><?php
|
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
echo $node->title;
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
?></a>
|
|
|
|
|
2011-11-09 14:12:48 -05:00
|
|
|
<?php
|
|
|
|
if ( $is_parent ) :
|
2011-11-14 06:05:37 -05:00
|
|
|
?><div class="ab-sub-wrapper"><?php
|
|
|
|
|
|
|
|
// Render primary submenu
|
|
|
|
?><ul class="ab-submenu"><?php
|
|
|
|
foreach ( $node->children->primary as $child_node ) {
|
2011-11-09 14:12:48 -05:00
|
|
|
$this->recursive_render( $child_node );
|
|
|
|
}
|
|
|
|
?></ul><?php
|
2011-11-14 06:05:37 -05:00
|
|
|
|
|
|
|
// 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
|
2011-11-09 14:12:48 -05:00
|
|
|
endif;
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2011-11-02 16:34:54 -04:00
|
|
|
if ( ! empty( $node->meta['html'] ) )
|
|
|
|
echo $node->meta['html'];
|
|
|
|
|
|
|
|
?>
|
|
|
|
</li><?php
|
2010-09-27 16:26:36 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
function add_menus() {
|
2011-10-05 20:02:18 -04:00
|
|
|
// User related, aligned right.
|
2010-12-13 15:35:28 -05:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
|
2011-10-05 20:02:18 -04:00
|
|
|
|
|
|
|
// Site related.
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
|
2011-10-05 14:45:32 -04:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
|
2011-10-07 15:52:26 -04:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
|
2011-10-05 20:02:18 -04:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
|
|
|
|
|
|
|
|
// Content related.
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
|
2011-10-10 14:40:00 -04:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 90 );
|
2010-12-13 16:21:50 -05:00
|
|
|
|
2011-11-03 13:08:12 -04:00
|
|
|
if ( ! is_admin() )
|
2011-09-16 01:01:54 -04:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
|
2010-12-13 16:21:50 -05:00
|
|
|
|
2010-12-09 11:55:09 -05:00
|
|
|
do_action( 'add_admin_bar_menus' );
|
2010-10-28 04:31:36 -04:00
|
|
|
}
|
2010-09-27 16:26:36 -04:00
|
|
|
}
|
2011-11-03 13:08:12 -04:00
|
|
|
?>
|