Skip to content

Instantly share code, notes, and snippets.

@keesiemeijer
Last active February 3, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keesiemeijer/6498235 to your computer and use it in GitHub Desktop.
Save keesiemeijer/6498235 to your computer and use it in GitHub Desktop.
Adds WordPress top level admin menu items to the toolbar (admin bar) on the front end.Adds post types items and their taxonomies to the toolbar on the front end.Items are added based on the current users capabilities.
<?php
/**
* Add back end wp-admin menu items to the front end toolbar 'site-name' node.
* Menu items are added based on the current users capabilities.
*/
class KM_WP_Admin_Bar_Front_End_Menu {
/**
* Prefix for toolbars ids
*
* @var string
*/
private $prefix;
/**
* Global $wp_admin_bar object
*
* @var WP_Admin_Bar
*/
private $admin_bar;
public function __construct() {
add_action( 'admin_bar_menu', array( $this, 'add_nodes' ), 999 );
}
/**
* Add back end wp-admin menu items to the front end toolbar.
* Items are added based on the current users capabilities.
*
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
*/
public function add_nodes( $wp_admin_bar ) {
if ( is_admin() ) {
return;
}
$this->prefix = 'km_admin_bar_';
$this->admin_bar = $wp_admin_bar;
// remove dashboard node
$this->admin_bar->remove_node( 'dashboard' );
// remove appearance menu
$this->admin_bar->remove_node( 'appearance' );
// add new group node
$args = array(
'id' => 'km_admin_menu_nodes',
'parent' => 'site-name',
'group' => true,
'title' => __( 'Admin Menu' ),
);
$this->admin_bar->add_node( $args );
$this->add_post_type_nodes();
$this->add_settings_nodes();
$this->add_appearance_nodes();
$this->add_plugin_nodes();
$this->add_media_nodes();
$this->add_comment_nodes();
$this->add_user_nodes();
}
/**
* Add post type and taxonomy nodes to the toolbar
*/
private function add_post_type_nodes() {
$menu = $submenu = array();
// default post types
$post_types = array(
'post' => get_post_type_object( 'post' ),
'page' => get_post_type_object( 'page' ),
);
// additional post types
foreach ( (array) get_post_types( array( 'show_ui' => true, '_builtin' => false, 'show_in_menu' => true ), 'objects' ) as $ptype ) {
$post_types[ $ptype->name ]= $ptype;
}
foreach ( $post_types as $ptype_obj ) {
$ptype = $ptype_obj->name;
if ( $ptype_obj->show_in_menu !== true ) {
continue;
}
if ( ! current_user_can( $ptype_obj->cap->edit_posts ) ) {
continue;
}
$menu[] = array( $ptype, esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
foreach ( get_taxonomies( array(), 'objects' ) as $tax ) {
if ( ! $tax->show_ui || ! in_array( $ptype, (array) $tax->object_type ) ) {
continue;
}
$submenu[ $ptype ][] = array( $tax->name, esc_attr( $tax->labels->menu_name ), $tax->cap->manage_terms, "edit-tags.php?taxonomy=$tax->name&amp;post_type=$ptype" );
}
}
if ( empty( $menu ) ) {
return;
}
// add new top level post types menu
$args = array(
'id' => $this->prefix . 'post_types',
'parent' => 'km_admin_menu_nodes',
'title' => __( 'Post Types' ),
);
$this->admin_bar->add_node( $args );
$this->km_admin_bar_walk_nodes( $menu, $submenu, $this->prefix . 'post_types' );
}
/**
* Add settings nodes to the toolbar.
*/
private function add_settings_nodes() {
$menu = $submenu = array();
$menu[] = array( 'settings', __( 'Settings' ), 'manage_options', 'options-general.php' );
$submenu['settings'][] = array( 'general', _x( 'General', 'settings screen' ), 'manage_options', 'options-general.php' );
$submenu['settings'][] = array( 'writing', __( 'Writing' ), 'manage_options', 'options-writing.php' );
$submenu['settings'][] = array( 'reading', __( 'Reading' ), 'manage_options', 'options-reading.php' );
$submenu['settings'][] = array( 'discussion', __( 'Discussion' ), 'manage_options', 'options-discussion.php' );
$submenu['settings'][] = array( 'media', __( 'Media' ), 'manage_options', 'options-media.php' );
$submenu['settings'][] = array( 'permalinks', __( 'Permalinks' ), 'manage_options', 'options-permalink.php' );
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add appearance nodes to the toolbar.
*/
private function add_appearance_nodes() {
$menu = $submenu = array();
$appearance_cap = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
$menu[] = array( 'appearance', __( 'Appearance' ), $appearance_cap, 'themes.php' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
return;
}
// menus
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
$submenu['appearance'][] = array( 'menus', __( 'Menus' ), 'edit_theme_options', 'nav-menus.php' );
}
// widgets
if ( current_theme_supports( 'widgets' ) ) {
$submenu['appearance'][] = array( 'widgets', __( 'Widgets' ), 'edit_theme_options', 'widgets.php' );
}
// custom background
if ( current_theme_supports( 'custom-background' ) ) {
$class = 'hide-if-customize';
$url = 'themes.php?page=custom-background';
$submenu['appearance'][] = array( 'background', __( 'Background' ), 'edit_theme_options', $url, $class );
}
// custom header
if ( current_theme_supports( 'custom-header' ) ) {
$class = 'hide-if-customize';
$url = 'themes.php?page=custom-header';
$submenu['appearance'][] = array( 'header', __( 'Header' ), 'edit_theme_options', $url, $class );
}
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add plugin nodes to the toolbar.
*/
private function add_plugin_nodes() {
$menu = $submenu = array();
if ( ! is_multisite() || is_super_admin() ) {
$update_data = wp_get_update_data();
}
$count = '';
if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
if ( ! isset( $update_data ) ) {
$update_data = wp_get_update_data();
}
$count = number_format_i18n( $update_data['counts']['plugins'] );
}
$plugin_title = ( absint( $count ) > 0 ) ? sprintf( __( 'Plugins (%s)' ), $count ) : __( 'Plugins' );
$menu[] = array( 'plugins', $plugin_title , 'activate_plugins', 'plugins.php' );
if ( ! is_multisite() ) {
$submenu['plugins'][] = array( 'add_plugins', _x( 'Add New', 'plugin' ), 'install_plugins', 'plugin-install.php' );
}
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add media nodes to the toolbar.
*/
private function add_media_nodes() {
$menu = $submenu = array();
$menu[] = array( 'media', __( 'Media' ), 'upload_files', 'upload.php' );
$submenu['media'][] = array( 'add_new_file', _x( 'Add New', 'file' ), 'upload_files', 'media-new.php' );
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add comment nodes to the toolbar.
*/
private function add_comment_nodes() {
$menu = $submenu = array();
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
$awaiting_mod = wp_count_comments();
$awaiting_mod = $awaiting_mod->moderated;
$count = absint( number_format_i18n( $awaiting_mod ) );
$comment_title = ( $count > 0 ) ? sprintf( __( 'Comments (%s)' ), $count ) : __( 'Comments' );
$menu[] = array( 'comments', $comment_title, 'edit_posts', 'edit-comments.php' );
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add user nodes to the toolbar.
*/
private function add_user_nodes() {
$menu = $submenu = array();
if ( current_user_can( 'list_users' ) ) {
$menu[] = array( 'users', __( 'Users' ), 'list_users', 'users.php' );
if ( current_user_can( 'create_users' ) ) {
$submenu['users'][] = array( 'add_user', _x( 'Add New', 'user' ), 'create_users', 'user-new.php' );
} elseif ( is_multisite() ) {
$submenu['users'][] = array( 'ms_add_user', _x( 'Add New', 'user' ), 'promote_users', 'user-new.php' );
}
$submenu['users'][] = array( 'your_profile', __( 'Your Profile' ), 'read', 'profile.php' );
} else {
$menu[] = array( 'profile', __( 'Profile' ), 'read', 'profile.php' );
if ( current_user_can( 'create_users' ) ) {
$submenu['profile'][] = array( 'add_user', __( 'Add New User' ), 'create_users', 'user-new.php' );
} elseif ( is_multisite() ) {
$submenu['profile'][] = array( 'ms_add_user', __( 'Add New User' ), 'promote_users', 'user-new.php' );
}
}
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add tools nodes to the toolbar.
*/
private function add_tools_nodes() {
$menu = $submenu = array();
$menu[] = array( 'tools', __( 'Tools' ), 'edit_posts', 'tools.php', );
$submenu['tools'][] = array( 'import', __( 'Import' ), 'import', 'import.php' );
$submenu['tools'][] = array( 'export', __( 'Export' ), 'export', 'export.php' );
if ( is_multisite() && ! is_main_site() ) {
$submenu['tools'][] = array( 'delete_site', __( 'Delete Site' ), 'delete_site', 'ms-delete-site.php' );
}
if ( ! is_multisite() && defined( 'WP_ALLOW_MULTISITE' ) && WP_ALLOW_MULTISITE ) {
$submenu['tools'][] = array( 'netword_setup', __( 'Network Setup' ), 'manage_options', 'network.php' );
}
$this->km_admin_bar_walk_nodes( $menu, $submenu, 'km_admin_menu_nodes' );
}
/**
* Add the menu and submenu nodes to the toolbar.
*
* @param array $menu Top level toolbar menu nodes.
* @param array $submenu Sub level toolbar menu nodes.
* @param string $parent_node Parent node id.
*/
private function km_admin_bar_walk_nodes( $menu, $submenu, $parent_node ) {
// add admin menu nodes
foreach ( $menu as $key => $node ) {
if ( ! current_user_can( $node[2] ) ) {
continue;
}
// for submenu nodes add the parent id to the prefix.
$prefix = isset( $node[5] ) ? $this->prefix . $node[5] . '_' : $this->prefix;
$args = array(
'id' => $prefix . $node[0],
'parent' => $parent_node,
'title' => $node[1],
'href' => admin_url( $node[3] ),
);
if ( isset( $node[4] ) && $node[4] ) {
$args['meta'] = array(
'class' => $node[4]
);
}
$this->admin_bar->add_node( $args );
if ( ! isset( $submenu[ $node[0] ] ) ) {
continue;
}
// Add parent node to ensure we get unique toolbar ids.
foreach ( $submenu[ $node[0] ] as $key => $value ) {
if ( 4 === count( $submenu[ $node[0] ][ $key ] ) ) {
// add empty class
$submenu[ $node[0] ][ $key ][] = '';
}
$submenu[ $node[0] ][ $key ][] = $node[0];
}
// Add submenu nodes.
$this->km_admin_bar_walk_nodes( $submenu[ $node[0] ], array(), $this->prefix . $node[0] );
}
}
}
new KM_WP_Admin_Bar_Front_End_Menu();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment