2010-09-27 16:26:36 -04:00
|
|
|
<?php
|
|
|
|
class WP_Admin_Bar {
|
|
|
|
var $menu;
|
2010-10-28 04:31:36 -04:00
|
|
|
var $proto = 'http://';
|
|
|
|
var $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;
|
|
|
|
$this->menu = new stdClass;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function add_menu( $args = array() ) {
|
|
|
|
$defaults = array(
|
|
|
|
'title' => false,
|
|
|
|
'href' => false,
|
|
|
|
'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
|
|
|
|
'id' => false, // defaults to a sanitized title value.
|
2011-01-12 11:02:56 -05:00
|
|
|
'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
|
2010-09-27 16:26:36 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$r = wp_parse_args( $args, $defaults );
|
|
|
|
extract( $r, EXTR_SKIP );
|
|
|
|
|
|
|
|
if ( empty( $title ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Make sure we have a valid ID */
|
|
|
|
if ( empty( $id ) )
|
|
|
|
$id = esc_attr( sanitize_title( trim( $title ) ) );
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $parent ) ) {
|
2010-09-27 16:26:36 -04:00
|
|
|
/* Add the menu to the parent item */
|
2010-10-28 04:31:36 -04:00
|
|
|
$child = array( 'id' => $id, 'title' => $title, 'href' => $href );
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $meta ) )
|
2010-09-27 16:26:36 -04:00
|
|
|
$child['meta'] = $meta;
|
|
|
|
|
|
|
|
$this->add_node( $parent, $this->menu, $child );
|
|
|
|
} else {
|
|
|
|
/* Add the menu item */
|
2010-10-28 04:31:36 -04:00
|
|
|
$this->menu->{$id} = array( 'title' => $title, 'href' => $href );
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $meta ) )
|
2010-09-27 16:26:36 -04:00
|
|
|
$this->menu->{$id}['meta'] = $meta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_menu( $id ) {
|
|
|
|
return $this->remove_node( $id, $this->menu );
|
|
|
|
}
|
2010-10-28 11:46:11 -04:00
|
|
|
|
2010-09-27 16:26:36 -04:00
|
|
|
function render() {
|
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-09-25 19:30:40 -04:00
|
|
|
<ul class="ab-top-menu">
|
2010-09-27 16:26:36 -04:00
|
|
|
<?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>
|
|
|
|
<?php $this->recursive_render( $id, $menu_item ) ?>
|
|
|
|
<?php endforeach; ?>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
/* Wipe the menu, might reduce memory usage, but probably not. */
|
|
|
|
$this->menu = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
function recursive_render( $id, &$menu_item ) { ?>
|
2011-01-12 20:34:15 -05:00
|
|
|
<?php
|
|
|
|
$is_parent = ! empty( $menu_item['children'] );
|
2011-02-09 12:35:36 -05:00
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
$menuclass = $is_parent ? 'menupop' : '';
|
|
|
|
if ( ! empty( $menu_item['meta']['class'] ) )
|
|
|
|
$menuclass .= ' ' . $menu_item['meta']['class'];
|
|
|
|
?>
|
2010-10-28 04:31:36 -04:00
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
<li id="<?php echo esc_attr( "wp-admin-bar-$id" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
|
2011-01-12 11:02:56 -05:00
|
|
|
<a href="<?php echo esc_url( $menu_item['href'] ) ?>"<?php
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $menu_item['meta']['onclick'] ) ) :
|
2011-01-12 11:02:56 -05:00
|
|
|
?> onclick="<?php echo esc_js( $menu_item['meta']['onclick'] ); ?>"<?php
|
2010-10-28 04:31:36 -04:00
|
|
|
endif;
|
|
|
|
if ( ! empty( $menu_item['meta']['target'] ) ) :
|
2011-01-12 11:02:56 -05:00
|
|
|
?> target="<?php echo esc_attr( $menu_item['meta']['target'] ); ?>"<?php
|
|
|
|
endif;
|
|
|
|
if ( ! empty( $menu_item['meta']['title'] ) ) :
|
|
|
|
?> title="<?php echo esc_attr( $menu_item['meta']['title'] ); ?>"<?php
|
2010-11-17 13:47:34 -05:00
|
|
|
endif;
|
|
|
|
|
|
|
|
?>><?php
|
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
if ( $is_parent ) :
|
2010-11-17 13:47:34 -05:00
|
|
|
?><span><?php
|
|
|
|
endif;
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
echo $menu_item['title'];
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
if ( $is_parent ) :
|
2010-11-17 13:47:34 -05:00
|
|
|
?></span><?php
|
|
|
|
endif;
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
?></a>
|
|
|
|
|
2011-01-12 20:34:15 -05:00
|
|
|
<?php if ( $is_parent ) : ?>
|
2010-09-27 16:26:36 -04:00
|
|
|
<ul>
|
|
|
|
<?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?>
|
|
|
|
<?php $this->recursive_render( $child_id, $child_menu_item ); ?>
|
|
|
|
<?php endforeach; ?>
|
|
|
|
</ul>
|
|
|
|
<?php endif; ?>
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
<?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?>
|
2010-09-27 16:26:36 -04:00
|
|
|
<?php echo $menu_item['meta']['html']; ?>
|
|
|
|
<?php endif; ?>
|
|
|
|
</li><?php
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_node( $parent_id, &$menu, $child ) {
|
2011-01-03 14:19:34 -05:00
|
|
|
foreach( $menu as $id => $menu_item ) {
|
2010-09-27 16:26:36 -04:00
|
|
|
if ( $parent_id == $id ) {
|
2011-08-18 02:16:57 -04:00
|
|
|
if ( ! isset( $menu->{$parent_id}['children'] ) )
|
|
|
|
$menu->{$parent_id}['children'] = new stdClass;
|
2010-09-27 16:26:36 -04:00
|
|
|
$menu->{$parent_id}['children']->{$child['id']} = $child;
|
|
|
|
$child = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $menu->{$id}['children'] ) )
|
2010-09-27 16:26:36 -04:00
|
|
|
$this->add_node( $parent_id, $menu->{$id}['children'], $child );
|
|
|
|
}
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2010-09-27 16:26:36 -04:00
|
|
|
$child = null;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-09-16 01:01:54 -04:00
|
|
|
if ( ! is_admin() ) {
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
|
|
|
|
} else {
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_help_menu', 90 );
|
2010-12-09 11:55:09 -05:00
|
|
|
}
|
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
|
|
|
function remove_node( $id, &$menu ) {
|
2010-12-30 14:04:32 -05:00
|
|
|
if ( isset( $menu->$id ) ) {
|
|
|
|
unset( $menu->$id );
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-27 16:26:36 -04:00
|
|
|
|
2010-12-30 14:04:32 -05:00
|
|
|
foreach( $menu as $menu_item_id => $menu_item ) {
|
2010-10-28 04:31:36 -04:00
|
|
|
if ( ! empty( $menu->{$menu_item_id}['children'] ) )
|
2010-09-27 16:26:36 -04:00
|
|
|
$this->remove_node( $id, $menu->{$menu_item_id}['children'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 07:12:37 -04:00
|
|
|
?>
|