2010-02-24 03:06:36 -05:00
< ? php
2010-03-22 15:56:16 -04:00
/**
* Register nav menu metaboxes
*
* @ since 3.0 . 0
**/
function wp_nav_menu_metaboxes_setup () {
add_meta_box ( 'add-custom-links' , __ ( 'Add Custom Links' ), 'wp_nav_menu_item_link_metabox' , 'nav-menus' , 'side' , 'default' );
wp_nav_menu_post_type_metaboxes ();
wp_nav_menu_taxonomy_metaboxes ();
}
2010-02-24 03:06:36 -05:00
2010-03-22 15:56:16 -04:00
/**
* Limit the amount of meta boxes to just links , pages and cats for first time users .
*
* @ since 3.0 . 0
**/
function wp_initial_nav_menu_meta_boxes () {
global $wp_meta_boxes ;
if ( ! get_user_option ( 'meta-box-hidden_nav-menus' ) && is_array ( $wp_meta_boxes ) ) {
$initial_meta_boxes = array ( 'manage-menu' , 'create-menu' , 'add-custom-links' , 'add-page' , 'add-category' );
$hidden_meta_boxes = array ();
foreach ( array_keys ( $wp_meta_boxes [ 'nav-menus' ]) as $context ) {
foreach ( array_keys ( $wp_meta_boxes [ 'nav-menus' ][ $context ]) as $priority ) {
foreach ( $wp_meta_boxes [ 'nav-menus' ][ $context ][ $priority ] as $box ) {
if ( in_array ( $box [ 'id' ], $initial_meta_boxes ) ) {
unset ( $box [ 'id' ] );
} else {
$hidden_meta_boxes [] = $box [ 'id' ];
}
}
}
}
$user = wp_get_current_user ();
update_user_meta ( $user -> ID , 'meta-box-hidden_nav-menus' , $hidden_meta_boxes );
// returns all the hidden metaboxes to the js function: wpNavMenu.initial_meta_boxes()
return join ( ',' , $hidden_meta_boxes );
}
}
2010-03-15 12:26:46 -04:00
2010-02-28 15:00:49 -05:00
/**
2010-03-15 12:26:46 -04:00
* Creates metaboxes for any post type menu item .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*/
2010-03-15 12:26:46 -04:00
function wp_nav_menu_post_type_metaboxes () {
$post_types = get_post_types ( array ( 'public' => true ), 'object' );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
if ( ! $post_types )
2010-03-22 15:56:16 -04:00
return ;
2010-03-16 13:22:24 -04:00
foreach ( $post_types as $post_type ) {
2010-03-16 18:01:49 -04:00
$id = $post_type -> name ;
2010-03-22 15:56:16 -04:00
add_meta_box ( " add- { $id } " , sprintf ( __ ( 'Add an Existing %s' ), $post_type -> singular_label ), 'wp_nav_menu_item_post_type_metabox' , 'nav-menus' , 'side' , 'default' , $post_type );
2010-02-28 15:00:49 -05:00
}
2010-03-15 12:26:46 -04:00
}
/**
* Creates metaboxes for any taxonomy menu item .
*
* @ since 3.0 . 0
*/
function wp_nav_menu_taxonomy_metaboxes () {
$taxonomies = get_taxonomies ( array ( 'show_ui' => true ), 'object' );
2010-03-16 18:01:49 -04:00
2010-03-15 12:26:46 -04:00
if ( ! $taxonomies )
2010-03-22 15:56:16 -04:00
return ;
2010-03-16 13:22:24 -04:00
2010-03-15 12:26:46 -04:00
foreach ( $taxonomies as $tax ) {
2010-03-16 18:01:49 -04:00
$id = $tax -> name ;
2010-03-16 13:22:24 -04:00
2010-03-22 15:56:16 -04:00
add_meta_box ( " add- { $id } " , sprintf ( __ ( 'Add an Existing %s' ), $tax -> singular_label ), 'wp_nav_menu_item_taxonomy_metabox' , 'nav-menus' , 'side' , 'default' , $tax );
2010-02-28 15:00:49 -05:00
}
}
2010-02-24 03:06:36 -05:00
2010-02-28 15:00:49 -05:00
/**
2010-03-15 12:26:46 -04:00
* Displays a metabox for managing the active menu being edited .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*/
2010-03-15 12:26:46 -04:00
function wp_nav_menu_manage_menu_metabox ( $object , $menu ) { ?>
< div id = " submitpost " class = " submitbox " >
< div id = " minor-publishing " >
< div class = " misc-pub-section misc-pub-section-last " >
< label class = " howto " for = " menu-name " >
< span >< ? php _e ( 'Name' ); ?> </span>
< input id = " menu-name " name = " menu-name " type = " text " class = " regular-text menu-item-textbox " value = " <?php esc_attr_e( $menu['args'] [1] ); ?> " />
< br class = " clear " />
</ label >
</ div ><!-- END . misc - pub - section misc - pub - section - last -->
< br class = " clear " />
</ div ><!-- END #misc-publishing-actions-->
< div id = " major-publishing-actions " >
< div id = " delete-action " >
< a class = " submitdelete deletion " href = " <?php echo wp_nonce_url( admin_url('nav-menus.php?action=delete&menu=' . $menu['args'] [0]), 'delete-nav_menu-' . $menu['args'] [0] ); ?> " >< ? php _e ( 'Delete Menu' ); ?> </a>
</ div ><!-- END #delete-action-->
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
< div id = " publishing-action " >
< input class = " button-primary " name = " save_menu " type = " submit " value = " <?php esc_attr_e('Save Menu'); ?> " />
</ div ><!-- END #publishing-action-->
< br class = " clear " />
</ div ><!-- END #major-publishing-actions-->
</ div ><!-- END #submitpost .submitbox-->
< ? php
}
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
/**
* Displays a metabox for creating a new menu .
*
* @ since 3.0 . 0
*/
function wp_nav_menu_create_metabox () { ?>
< p >
2010-03-17 15:57:08 -04:00
< input type = " text " name = " create-menu-name " id = " create-menu-name " class = " regular-text " value = " <?php esc_attr_e( 'Menu name' ); ?> " />
< input type = " submit " name = " create-menu-button " id = " create-menu-button " class = " button " value = " <?php esc_attr_e('Create Menu'); ?> " />
2010-03-15 12:26:46 -04:00
</ p >
< ? php
}
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
/**
* Displays a metabox for the custom links menu item .
*
* @ since 3.0 . 0
*/
function wp_nav_menu_item_link_metabox () {
2010-03-22 15:56:16 -04:00
// @note: hacky query, see #12660
$args = array ( 'post_type' => 'nav_menu_item' , 'post_status' => 'any' , 'meta_key' => '_menu_item_type' , 'numberposts' => - 1 , 'orderby' => 'title' , );
2010-03-17 12:27:25 -04:00
2010-03-17 02:10:47 -04:00
// @todo transient caching of these results with proper invalidation on updating links
2010-03-22 15:56:16 -04:00
$links = get_posts ( $args );
2010-03-15 12:26:46 -04:00
?>
< p id = " menu-item-url-wrap " >
< label class = " howto " for = " menu-item-url " >
< span >< ? php _e ( 'URL' ); ?> </span>
< input id = " custom-menu-item-url " name = " custom-menu-item-url " type = " text " class = " code menu-item-textbox " value = " http:// " />
</ label >
</ p >
< p id = " menu-item-name-wrap " >
< label class = " howto " for = " custom-menu-item-name " >
< span >< ? php _e ( 'Text' ); ?> </span>
< input id = " custom-menu-item-name " name = " custom-menu-item-name " type = " text " class = " regular-text menu-item-textbox " value = " <?php echo esc_attr( __('Menu Item') ); ?> " />
</ label >
</ p >
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
< p class = " button-controls " >
2010-03-22 15:56:16 -04:00
< span class = " lists-controls " >
< a class = " show-all " >< ? php _e ( 'View All' ); ?> </a>
< a class = " hide-all " >< ? php _e ( 'Hide All' ); ?> </a>
</ span >
< span class = " add-to-menu " >
< a class = " button " >< ? php _e ( 'Add to Menu' ); ?> </a>
</ span >
2010-03-15 12:26:46 -04:00
</ p >
< div id = " available-links " class = " list-wrap " >
< div class = " list-container " >
< ul class = " list " >
2010-03-22 15:56:16 -04:00
< ? php echo wp_nav_menu_get_items ( $links , 'custom' , 'custom' ); ?>
2010-03-15 12:26:46 -04:00
</ ul >
</ div ><!-- /. list - container -->
</ div ><!-- / #available-links-->
< div class = " clear " ></ div >
< ? php
}
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
/**
* Displays a metabox for a post type menu item .
*
* @ since 3.0 . 0
*
* @ param string $object Not used .
* @ param string $post_type The post type object .
*/
function wp_nav_menu_item_post_type_metabox ( $object , $post_type ) {
2010-03-22 15:56:16 -04:00
$args = array ( 'post_type' => $post_type [ 'args' ] -> name , 'numberposts' => - 1 , 'orderby' => 'title' , );
2010-03-17 12:27:25 -04:00
2010-03-17 02:10:47 -04:00
// @todo transient caching of these results with proper invalidation on updating of a post of this type
2010-03-22 15:56:16 -04:00
$posts = get_posts ( $args );
2010-03-17 12:27:25 -04:00
2010-03-22 15:56:16 -04:00
if ( ! $posts )
2010-03-15 12:26:46 -04:00
$error = '<li id="error">' . sprintf ( __ ( 'No %s exists' ), $post_type [ 'args' ] -> label ) . '</li>' ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$pt_names = '' ;
2010-03-22 15:56:16 -04:00
if ( is_array ( $posts ) ) {
foreach ( $posts as $post ) {
2010-03-15 12:26:46 -04:00
if ( $post -> post_title ) {
$pt_names .= htmlentities ( $post -> post_title ) . '|' ;
2010-02-24 03:06:36 -05:00
}
}
}
2010-03-17 12:27:25 -04:00
2010-03-16 18:01:49 -04:00
$id = $post_type [ 'args' ] -> name ;
2010-03-15 12:26:46 -04:00
?>
< p class = " quick-search-wrap " >
< input type = " text " class = " quick-search regular-text " value = " " />
< a class = " quick-search-submit button-secondary " >< ? php _e ( 'Search' ); ?> </a>
</ p >
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
< p class = " button-controls " >
2010-03-22 15:56:16 -04:00
< span class = " lists-controls " >
< a class = " show-all " >< ? php _e ( 'View All' ); ?> </a>
< a class = " hide-all " >< ? php _e ( 'Hide All' ); ?> </a>
</ span >
< span class = " add-to-menu " >
< a class = " button " >< ? php _e ( 'Add to Menu' ); ?> </a>
</ span >
2010-03-15 12:26:46 -04:00
</ p >
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
< div id = " existing-<?php echo esc_attr( $id ); ?> " class = " list-wrap " >
< div class = " list-container " >
< ul class = " list " >
2010-03-22 15:56:16 -04:00
< ? php echo isset ( $error ) ? $error : wp_nav_menu_get_items ( $posts , 'post_type' , $id ); ?>
2010-03-15 12:26:46 -04:00
</ ul >
</ div ><!-- /. list - container -->
</ div ><!-- / #existing-categories-->
2010-03-16 18:01:49 -04:00
< input type = " hidden " class = " autocomplete " name = " autocomplete-<?php echo esc_attr( $id ); ?>-names " value = " <?php echo esc_js( $pt_names ); ?> " />
2010-03-15 12:26:46 -04:00
< br class = " clear " />
< script type = " text/javascript " charset = " utf-8 " >
// <![CDATA[
jQuery ( document ) . ready ( function (){
2010-03-22 15:56:16 -04:00
wpNavMenu . autocomplete ( '<?php echo esc_attr($id); ?>' );
2010-03-15 12:26:46 -04:00
});
// ]]>
</ script >
< ? php
2010-02-24 03:06:36 -05:00
}
2010-02-28 15:00:49 -05:00
/**
2010-03-15 12:26:46 -04:00
* Displays a metabox for a taxonomy menu item .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
2010-03-15 12:26:46 -04:00
* @ param string $object Not used .
* @ param string $taxonomy The taxonomy object .
2010-02-28 15:00:49 -05:00
*/
2010-03-15 12:26:46 -04:00
function wp_nav_menu_item_taxonomy_metabox ( $object , $taxonomy ) {
$args = array (
'child_of' => 0 , 'orderby' => 'name' , 'order' => 'ASC' ,
'hide_empty' => false , 'include_last_update_time' => false , 'hierarchical' => 1 , 'exclude' => '' ,
'include' => '' , 'number' => '' , 'pad_counts' => false
);
2010-03-17 02:10:47 -04:00
// @todo transient caching of these results with proper invalidation on updating of a tax of this type
$terms = get_terms ( $taxonomy [ 'args' ] -> name , $args );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
if ( ! $terms )
$error = '<li id="error">' . sprintf ( __ ( 'No %s exists' ), $taxonomy [ 'args' ] -> label ) . '</li>' ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$term_names = '' ;
if ( is_array ( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term -> name ) {
$term_names .= htmlentities ( $term -> name ) . '|' ;
2010-02-24 03:06:36 -05:00
}
}
}
2010-03-17 12:27:25 -04:00
2010-03-16 18:01:49 -04:00
$id = $taxonomy [ 'args' ] -> name ;
2010-03-15 12:26:46 -04:00
?>
< p class = " quick-search-wrap " >
< input type = " text " class = " quick-search regular-text " value = " " />
< a class = " quick-search-submit button-secondary " >< ? php _e ( 'Search' ); ?> </a>
</ p >
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
< p class = " button-controls " >
2010-03-22 15:56:16 -04:00
< span class = " lists-controls " >
< a class = " show-all " >< ? php _e ( 'View All' ); ?> </a>
< a class = " hide-all " >< ? php _e ( 'Hide All' ); ?> </a>
</ span >
< span class = " add-to-menu " >
< a class = " button " >< ? php _e ( 'Add to Menu' ); ?> </a>
</ span >
2010-03-15 12:26:46 -04:00
</ p >
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
< div id = " existing-<?php echo esc_attr( $id ); ?> " class = " list-wrap " >
< div class = " list-container " >
< ul class = " list " >
2010-03-16 18:01:49 -04:00
< ? php echo isset ( $error ) ? $error : wp_nav_menu_get_items ( $terms , 'taxonomy' , $id ); ?>
2010-03-15 12:26:46 -04:00
</ ul >
</ div ><!-- /. list - container -->
</ div ><!-- / #existing-categories-->
2010-03-16 18:01:49 -04:00
< input type = " hidden " class = " autocomplete " name = " autocomplete-<?php echo esc_attr( $id ); ?>-names " value = " <?php echo esc_js( $term_names ); ?> " />
2010-03-15 12:26:46 -04:00
< br class = " clear " />
< script type = " text/javascript " charset = " utf-8 " >
// <![CDATA[
jQuery ( document ) . ready ( function (){
2010-03-22 15:56:16 -04:00
wpNavMenu . autocomplete ( '<?php echo esc_attr($id); ?>' );
2010-03-15 12:26:46 -04:00
});
// ]]>
</ script >
< ? php
2010-02-24 03:06:36 -05:00
}
2010-02-28 15:00:49 -05:00
/**
2010-03-15 12:26:46 -04:00
* Abstract function for returning all menu items of a menu item type .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
2010-03-15 12:26:46 -04:00
* @ param string $menu_items Array of objects containing all menu items to be displayed .
* @ param string $object_type Menu item type .
* @ param string $object Optional . Menu item type name .
* @ param string $context Optional . The context for how the menu items should be formatted .
* @ return string $ouput Menu items .
2010-02-28 15:00:49 -05:00
*/
2010-03-15 12:26:46 -04:00
function wp_nav_menu_get_items ( $menu_items , $object_type , $object = null , $context = 'frontend' ) {
if ( ! $menu_items )
return __ ( 'Not Found' );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$output = '' ;
$i = 1 ;
foreach ( $menu_items as $menu_item ) {
// convert the 'parent' taxonomy property to 'post_parent'
// so we don't have to duplicate this entire function.
if ( ! isset ( $menu_item -> post_parent ) )
$menu_item -> post_parent = $menu_item -> parent ;
2010-03-17 12:27:25 -04:00
2010-03-22 15:56:16 -04:00
// Get all attachements and links
if ( in_array ( $object , array ( 'attachment' , 'custom' )) )
2010-03-15 12:26:46 -04:00
$menu_item -> post_parent = 0 ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
if ( 0 == $menu_item -> post_parent ) {
2010-03-17 00:39:50 -04:00
// Set up the menu item
2010-03-17 12:27:25 -04:00
$menu_item = wp_setup_nav_menu_item ( $menu_item , $object_type , $object );
2010-03-22 15:56:16 -04:00
// No blank titles
if ( empty ( $menu_item -> title ) )
continue ;
2010-03-15 12:26:46 -04:00
$attributes = ( 'backend' == $context ) ? ' id="menu-item-' . $i . '" value="' . $i . '"' : '' ;
2010-03-22 15:56:16 -04:00
2010-03-15 12:26:46 -04:00
$output .= '<li' . $attributes . '>' ;
$output .= wp_get_nav_menu_item ( $menu_item , $object_type , $object );
$output .= wp_get_nav_menu_sub_items ( $menu_item -> ID , $object_type , $object , $context );
$output .= '</li>' ;
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
++ $i ;
}
2010-02-24 03:06:36 -05:00
}
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
return $output ;
}
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
/**
* Recursive function to retrieve sub menu items .
*
* @ since 3.0 . 0
*
* @ param string $childof The Parent ID .
* @ param string $object_type The object type .
* @ param string $object The object name .
* @ return string $output sub menu items .
*/
function wp_get_nav_menu_sub_items ( $childof , $object_type , $object = null , $context = 'frontend' ) {
$args = array ( 'child_of' => $childof , 'parent' => $childof , 'hide_empty' => false , );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
switch ( $object_type ) {
case 'post_type' :
$hierarchical_post_types = get_post_types ( array ( 'hierarchical' => true ) );
if ( in_array ( $object , $hierarchical_post_types ) ) {
$args [ 'post_type' ] = $object ;
$sub_menu_items = get_pages ( $args );
} else {
$sub_menu_items = array ();
}
break ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
case 'taxonomy' :
if ( is_taxonomy_hierarchical ( $object ) ) {
$sub_menu_items = get_terms ( $object , $args );
} else {
$sub_menu_items = array ();
}
break ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
default :
$sub_menu_items = array ();
break ;
2010-02-24 03:06:36 -05:00
}
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$output = '' ;
$i = 1 ;
if ( ! empty ( $sub_menu_items ) && ! is_wp_error ( $sub_menu_items ) ) {
$output .= '<ul class="sub-menu menu-item-type-' . $object_type . '">' ;
foreach ( $sub_menu_items as $menu_item ) {
2010-03-17 00:39:50 -04:00
// Set up the menu item
2010-03-15 12:26:46 -04:00
$menu_item = wp_setup_nav_menu_item ( $menu_item , $object_type , $object );
$attributes = ( 'backend' == $context ) ? ' id="menu-item-' . $i . '" value="' . $i . '"' : '' ;
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
$output .= '<li' . $attributes . '>' ;
$output .= wp_get_nav_menu_item ( $menu_item , $object_type , $object );
$output .= wp_get_nav_menu_sub_items ( $menu_item -> ID , $object_type , $object );
$output .= '</li>' ;
2010-02-24 03:06:36 -05:00
2010-03-15 12:26:46 -04:00
++ $i ;
2010-02-24 03:06:36 -05:00
}
2010-03-15 12:26:46 -04:00
$output .= '</ul>' ;
2010-02-24 03:06:36 -05:00
}
2010-03-15 12:26:46 -04:00
return $output ;
2010-02-24 03:06:36 -05:00
}
?>