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-11-09 20:12:48 +01:00
|
|
|
$this->root = new stdClass;
|
|
|
|
$this->root->children = (object) array(
|
|
|
|
'primary' => array(),
|
|
|
|
'secondary' => array(),
|
|
|
|
);
|
2010-09-27 22:26:36 +02:00
|
|
|
|
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.
|
2011-11-09 20:12:48 +01: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 21:34:54 +01: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 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(
|
2011-11-09 20:12:48 +01:00
|
|
|
'id' => false,
|
|
|
|
'title' => false,
|
|
|
|
'parent' => false,
|
|
|
|
'href' => false,
|
|
|
|
'secondary' => false,
|
|
|
|
'meta' => array(),
|
2011-11-02 21:34:54 +01:00
|
|
|
);
|
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 );
|
2011-11-09 20:12:48 +01:00
|
|
|
$args['children'] = (object) array(
|
|
|
|
'primary' => array(),
|
|
|
|
'secondary' => array(),
|
|
|
|
);
|
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() {
|
2011-11-21 03:03:02 +01:00
|
|
|
global $is_IE, $is_iphone;
|
2011-11-20 19:39:54 +01:00
|
|
|
|
|
|
|
$back_compat_parents = array(
|
|
|
|
'appearance' => 'site-name',
|
|
|
|
);
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
// Link nodes to parents.
|
|
|
|
foreach ( $this->nodes as $node ) {
|
|
|
|
|
2011-11-20 19:39:54 +01:00
|
|
|
if ( isset( $back_compat_parents[ $node->parent ] ) )
|
|
|
|
$node->parent = $back_compat_parents[ $node->parent ];
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
// Handle root menu items
|
|
|
|
if ( empty( $node->parent ) ) {
|
2011-11-09 20:12:48 +01:00
|
|
|
$parent = $this->root;
|
2011-11-02 21:34:54 +01:00
|
|
|
|
|
|
|
// If the parent node isn't registered, ignore the node.
|
2011-11-09 20:12:48 +01:00
|
|
|
} elseif ( ! isset( $this->nodes[ $node->parent ] ) ) {
|
2011-11-02 21:34:54 +01:00
|
|
|
continue;
|
|
|
|
|
2011-11-09 20:12:48 +01:00
|
|
|
} else {
|
|
|
|
$parent = $this->nodes[ $node->parent ];
|
|
|
|
}
|
2011-11-02 21:34:54 +01:00
|
|
|
|
2011-11-09 20:12:48 +01:00
|
|
|
if ( $node->secondary )
|
|
|
|
$parent->children->secondary[] = $node;
|
|
|
|
else
|
|
|
|
$parent->children->primary[] = $node;
|
2011-11-02 21:34:54 +01:00
|
|
|
}
|
|
|
|
|
2011-11-22 00:07:54 +01:00
|
|
|
// Add browser classes.
|
|
|
|
// We have to do this here since admin bar shows on the front end.
|
|
|
|
$class = 'nojq nojs';
|
|
|
|
if ( $is_IE ) {
|
|
|
|
if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
|
|
|
|
$class .= ' ie7';
|
|
|
|
elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
|
|
|
|
$class .= ' ie8';
|
2011-11-22 22:35:08 +01:00
|
|
|
elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
|
|
|
|
$class .= ' ie9';
|
2011-11-22 00:07:54 +01:00
|
|
|
} elseif ( $is_iphone ) {
|
|
|
|
$class .= ' mobile';
|
|
|
|
}
|
|
|
|
|
2010-10-28 10:31:36 +02:00
|
|
|
?>
|
2011-11-21 03:03:02 +01:00
|
|
|
<div id="wpadminbar" class="<?php echo $class; ?>">
|
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
|
|
|
|
|
2011-11-09 20:12:48 +01:00
|
|
|
foreach ( $this->root->children->primary as $node ) {
|
2011-11-02 21:34:54 +01:00
|
|
|
$this->recursive_render( $node );
|
|
|
|
}
|
|
|
|
|
2011-11-12 01:26:55 +01:00
|
|
|
?></ul>
|
|
|
|
<ul class="ab-top-menu ab-top-secondary"><?php
|
2011-11-09 20:12:48 +01:00
|
|
|
|
2011-11-12 01:26:55 +01:00
|
|
|
foreach ( $this->root->children->secondary as $node ) {
|
|
|
|
$this->recursive_render( $node );
|
|
|
|
}
|
2011-11-09 20:12:48 +01:00
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
?></ul>
|
2010-09-27 22:26:36 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
function recursive_render( $node ) {
|
2011-11-10 01:42:39 +01: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-11-18 01:12:57 +01:00
|
|
|
$has_link = (bool) $node->href;
|
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-11-07 21:38:38 +01:00
|
|
|
|
|
|
|
$tabindex = !empty($node->meta['tabindex']) ? $node->meta['tabindex'] : 10;
|
2011-01-13 02:34:15 +01:00
|
|
|
?>
|
2010-10-28 10:31:36 +02:00
|
|
|
|
2011-11-18 01:12:57 +01:00
|
|
|
<li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
|
|
|
|
if ( $has_link ):
|
|
|
|
?><a class="ab-item" tabindex="<?php echo (int) $tabindex; ?>" href="<?php echo esc_url( $node->href ) ?>"<?php
|
|
|
|
if ( ! empty( $node->meta['onclick'] ) ) :
|
|
|
|
?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
|
|
|
|
endif;
|
|
|
|
if ( ! empty( $node->meta['target'] ) ) :
|
|
|
|
?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
|
2010-10-28 10:31:36 +02:00
|
|
|
endif;
|
2011-11-18 01:12:57 +01:00
|
|
|
if ( ! empty( $node->meta['title'] ) ) :
|
|
|
|
?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
|
|
|
|
endif;
|
|
|
|
?>><?php
|
|
|
|
else:
|
|
|
|
?><div class="ab-item ab-empty-item" tabindex="<?php echo (int) $tabindex; ?>"><?php
|
2010-11-17 19:47:34 +01:00
|
|
|
endif;
|
|
|
|
|
2011-11-02 21:34:54 +01:00
|
|
|
echo $node->title;
|
2010-11-17 19:47:34 +01:00
|
|
|
|
2011-11-18 01:12:57 +01:00
|
|
|
if ( $has_link ):
|
|
|
|
?></a><?php
|
|
|
|
else:
|
|
|
|
?></div><?php
|
|
|
|
endif;
|
2010-10-28 10:31:36 +02:00
|
|
|
|
2011-11-09 20:12:48 +01:00
|
|
|
if ( $is_parent ) :
|
2011-11-14 12:05:37 +01: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 20:12:48 +01:00
|
|
|
$this->recursive_render( $child_node );
|
|
|
|
}
|
|
|
|
?></ul><?php
|
2011-11-14 12:05:37 +01: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 20:12:48 +01:00
|
|
|
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.
|
2011-11-22 17:19:05 +01:00
|
|
|
if ( ! is_network_admin() && ! is_user_admin() ) {
|
|
|
|
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
|
|
|
?>
|