2010-09-27 22:26:36 +02:00
|
|
|
<?php
|
|
|
|
class WP_Admin_Bar {
|
2011-11-02 21:34:54 +01:00
|
|
|
private $nodes = array();
|
|
|
|
private $root = array();
|
|
|
|
|
|
|
|
public $proto = 'http://';
|
|
|
|
public $user;
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
function initialize() {
|
|
|
|
/* Set the protocol used throughout this code */
|
2010-11-17 19:47:34 +01:00
|
|
|
if ( is_ssl() )
|
2010-10-28 10:31:36 +02:00
|
|
|
$this->proto = 'https://';
|
2010-09-27 22:26:36 +02:00
|
|
|
|
|
|
|
$this->user = new stdClass;
|
|
|
|
|
2011-09-16 12:56:06 +02: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 22:26:36 +02:00
|
|
|
}
|
2010-10-28 10:31:36 +02:00
|
|
|
|
|
|
|
add_action( 'wp_head', 'wp_admin_bar_header' );
|
2010-10-29 09:25:58 +02:00
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
add_action( 'admin_head', 'wp_admin_bar_header' );
|
|
|
|
|
2010-11-30 22:50:57 +01: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 09:25:58 +02:00
|
|
|
wp_enqueue_script( 'admin-bar' );
|
2010-10-28 10:31:36 +02:00
|
|
|
wp_enqueue_style( 'admin-bar' );
|
|
|
|
|
|
|
|
do_action( 'admin_bar_init' );
|
2010-09-27 22:26:36 +02:00
|
|
|
}
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
public function add_menu( $node ) {
|
|
|
|
$this->add_node( $node );
|
|
|
|
}
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
public function remove_menu( $id ) {
|
|
|
|
$this->remove_node( $id );
|
|
|
|
}
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
/**
|
|
|
|
* Add a node to the menu.
|
|
|
|
*
|
|
|
|
* @param array $args - The arguments for each node.
|
|
|
|
* - 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.
|
|
|
|
* - meta - array - Meta data including the following keys: html, class, onclick, target, title.
|
|
|
|
*/
|
|
|
|
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 18:41:38 +01:00
|
|
|
// Ensure we have a valid title.
|
|
|
|
if ( empty( $args['title'] ) )
|
2010-09-27 22:26:36 +02:00
|
|
|
return false;
|
|
|
|
|
2011-11-04 18:41:38 +01: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 21:34:54 +01:00
|
|
|
$defaults = array(
|
|
|
|
'id' => false,
|
|
|
|
'title' => false,
|
|
|
|
'parent' => false,
|
|
|
|
'href' => false,
|
|
|
|
'meta' => array(),
|
|
|
|
);
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01: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 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
$args = wp_parse_args( $args, $defaults );
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
$this->nodes[ $args['id'] ] = (object) $args;
|
2010-09-27 22:26:36 +02:00
|
|
|
}
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
public function remove_node( $id ) {
|
|
|
|
unset( $this->nodes[ $id ] );
|
2010-09-27 22:26:36 +02:00
|
|
|
}
|
2010-10-28 17:46:11 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
public function render() {
|
|
|
|
// Link nodes to parents.
|
|
|
|
foreach ( $this->nodes as $node ) {
|
|
|
|
|
|
|
|
// Handle root menu items
|
|
|
|
if ( empty( $node->parent ) ) {
|
|
|
|
$this->root[] = $node;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the parent node isn't registered, ignore the node.
|
|
|
|
if ( ! isset( $this->nodes[ $node->parent ] ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$parent = $this->nodes[ $node->parent ];
|
|
|
|
if ( ! isset( $parent->children ) )
|
|
|
|
$parent->children = array();
|
|
|
|
|
|
|
|
$parent->children[] = $node;
|
|
|
|
}
|
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
?>
|
2011-09-26 01:30:40 +02:00
|
|
|
<div id="wpadminbar" class="nojq nojs">
|
2010-09-27 22:26:36 +02:00
|
|
|
<div class="quicklinks">
|
2011-11-02 21:34:54 +01:00
|
|
|
<ul class="ab-top-menu"><?php
|
|
|
|
|
|
|
|
foreach ( $this->root as $node ) {
|
|
|
|
$this->recursive_render( $node );
|
|
|
|
}
|
|
|
|
|
|
|
|
?></ul>
|
2010-09-27 22:26:36 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
function recursive_render( $node ) {
|
|
|
|
$is_parent = ! empty( $node->children );
|
2011-02-09 18:35:36 +01:00
|
|
|
|
2011-01-13 02:34:15 +01:00
|
|
|
$menuclass = $is_parent ? 'menupop' : '';
|
2011-11-02 21:34:54 +01:00
|
|
|
if ( ! empty( $node->meta['class'] ) )
|
|
|
|
$menuclass .= ' ' . $node->meta['class'];
|
2011-01-13 02:34:15 +01:00
|
|
|
?>
|
2010-10-28 10:31:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
<li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
|
|
|
|
<a href="<?php echo esc_url( $node->href ) ?>"<?php
|
|
|
|
if ( ! empty( $node->meta['onclick'] ) ) :
|
|
|
|
?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
|
2010-10-28 10:31:36 +02:00
|
|
|
endif;
|
2011-11-02 21:34:54 +01:00
|
|
|
if ( ! empty( $node->meta['target'] ) ) :
|
|
|
|
?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
|
2011-01-12 17:02:56 +01:00
|
|
|
endif;
|
2011-11-02 21:34:54 +01:00
|
|
|
if ( ! empty( $node->meta['title'] ) ) :
|
|
|
|
?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
|
2010-11-17 19:47:34 +01:00
|
|
|
endif;
|
|
|
|
|
|
|
|
?>><?php
|
|
|
|
|
2011-01-13 02:34:15 +01:00
|
|
|
if ( $is_parent ) :
|
2010-11-17 19:47:34 +01:00
|
|
|
?><span><?php
|
|
|
|
endif;
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
echo $node->title;
|
2010-11-17 19:47:34 +01:00
|
|
|
|
2011-01-13 02:34:15 +01:00
|
|
|
if ( $is_parent ) :
|
2010-11-17 19:47:34 +01:00
|
|
|
?></span><?php
|
|
|
|
endif;
|
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
?></a>
|
|
|
|
|
2011-01-13 02:34:15 +01:00
|
|
|
<?php if ( $is_parent ) : ?>
|
2011-11-02 21:34:54 +01:00
|
|
|
<ul><?php
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
// Render children.
|
|
|
|
foreach ( $node->children as $child_node ) {
|
|
|
|
$this->recursive_render( $child_node );
|
|
|
|
}
|
2010-09-27 22:26:36 +02:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
?></ul>
|
|
|
|
<?php endif;
|
2010-11-17 19:47:34 +01:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
if ( ! empty( $node->meta['html'] ) )
|
|
|
|
echo $node->meta['html'];
|
|
|
|
|
|
|
|
?>
|
|
|
|
</li><?php
|
2010-09-27 22:26:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
function add_menus() {
|
2011-10-06 02:02:18 +02:00
|
|
|
// User related, aligned right.
|
2010-12-13 21:35:28 +01:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
|
2011-10-06 02:02:18 +02:00
|
|
|
|
|
|
|
// Site related.
|
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
|
2011-10-05 20:45:32 +02:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
|
2011-10-07 21:52:26 +02:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
|
2011-10-06 02:02:18 +02: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 20:40:00 +02: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 22:21:50 +01:00
|
|
|
|
2011-11-03 18:08:12 +01:00
|
|
|
if ( ! is_admin() )
|
2011-09-16 07:01:54 +02:00
|
|
|
add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
|
2010-12-13 22:21:50 +01:00
|
|
|
|
2010-12-09 17:55:09 +01:00
|
|
|
do_action( 'add_admin_bar_menus' );
|
2010-10-28 10:31:36 +02:00
|
|
|
}
|
2010-09-27 22:26:36 +02:00
|
|
|
}
|
2011-11-03 18:08:12 +01:00
|
|
|
?>
|