2010-02-24 03:06:36 -05:00
< ? php
/**
* Navigation Menu functions
*
* @ package WordPress
2010-03-31 03:54:09 -04:00
* @ subpackage Nav_Menus
2010-02-24 03:06:36 -05:00
* @ since 3.0 . 0
*/
2010-02-28 15:00:49 -05:00
/**
2010-03-22 15:56:16 -04:00
* Returns a navigation menu object .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
* @ param string $menu Menu id
2010-03-22 15:56:16 -04:00
* @ return mixed $menu | false Or WP_Error
2010-02-28 15:00:49 -05:00
*/
function wp_get_nav_menu_object ( $menu ) {
return is_nav_menu ( $menu );
}
2010-02-24 03:06:36 -05:00
2010-02-28 15:00:49 -05:00
/**
2010-03-22 15:56:16 -04:00
* Check if navigation menu exists .
2010-02-28 15:00:49 -05:00
*
* Returns the menu object , or false if the term doesn ' t exist .
*
* @ since 3.0 . 0
*
* @ param int | string $menu The menu to check
2010-03-22 15:56:16 -04:00
* @ return mixed Menu Object , if it exists . Else , false or WP_Error
2010-02-28 15:00:49 -05:00
*/
function is_nav_menu ( $menu ) {
2010-03-02 12:56:24 -05:00
if ( ! $menu )
return false ;
$menu_obj = get_term ( $menu , 'nav_menu' );
if ( ! $menu_obj )
$menu_obj = get_term_by ( 'slug' , $menu , 'nav_menu' );
if ( ! $menu_obj )
$menu_obj = get_term_by ( 'name' , $menu , 'nav_menu' );
if ( ! $menu_obj ) {
$menu_obj = false ;
2010-02-24 03:06:36 -05:00
}
2010-03-02 12:56:24 -05:00
return $menu_obj ;
2010-02-24 03:06:36 -05:00
}
2010-02-28 15:00:49 -05:00
/**
2010-03-22 15:56:16 -04:00
* Create a Navigation Menu .
2010-02-28 15:00:49 -05:00
*
* Optional args :
* slug - the url friendly version of the nav menu .
*
* @ since 3.0 . 0
*
* @ param string $menu_name Menu Name
* @ param string $args Optional .
2010-03-22 15:56:16 -04:00
* @ return mixed Menu object on sucess | WP_Error on failure
2010-02-28 15:00:49 -05:00
*/
2010-02-25 16:06:44 -05:00
function wp_create_nav_menu ( $menu_name , $args = array () ) {
2010-02-24 03:06:36 -05:00
$menu_exists = get_term_by ( 'name' , $menu_name , 'nav_menu' );
if ( $menu_exists )
2010-03-22 15:56:16 -04:00
return new WP_Error ( 'menu_exists' , sprintf ( __ ( 'A menu named <strong>%s</strong> already exists; please try another name.' ), esc_html ( $menu_exists -> name ) ) );
2010-02-24 03:06:36 -05:00
2010-02-25 16:06:44 -05:00
if ( isset ( $args [ 'slug' ]) )
$slug = $args [ 'slug' ];
else
$slug = $menu_name ;
$menu = wp_insert_term ( $menu_name , 'nav_menu' , array ( 'slug' => $slug ) );
2010-02-24 03:06:36 -05:00
if ( is_wp_error ( $menu ) )
return $menu ;
2010-03-22 15:56:16 -04:00
$result = get_term ( $menu [ 'term_id' ], 'nav_menu' );
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
if ( $result && ! is_wp_error ( $result ) ) {
do_action ( 'wp_create_nav_menu' , $menu [ 'term_id' ] );
return $result ;
} else {
return $result ;
}
2010-02-24 03:06:36 -05:00
}
2010-02-28 15:00:49 -05:00
/**
2010-03-22 15:56:16 -04:00
* Delete a Navigation Menu .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
* @ param string $menu name | id | slug
2010-03-22 15:56:16 -04:00
* @ return mixed Menu object on sucess | WP_Error on failure
2010-02-28 15:00:49 -05:00
*/
function wp_delete_nav_menu ( $menu ) {
$menu = wp_get_nav_menu_object ( $menu );
if ( ! $menu )
return false ;
$menu_objects = get_objects_in_term ( $menu -> term_id , 'nav_menu' );
if ( ! empty ( $menu_objects ) ) {
foreach ( $menu_objects as $item ) {
wp_delete_post ( $item );
}
}
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
$result = wp_delete_term ( $menu -> term_id , 'nav_menu' );
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
if ( $result && ! is_wp_error ( $result ) ) {
do_action ( 'wp_delete_nav_menu' , $menu -> term_id );
return $result ;
} else {
return $result ;
}
2010-02-28 15:00:49 -05:00
}
/**
2010-03-22 15:56:16 -04:00
* Returns all navigation menu objects .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
2010-03-15 12:26:46 -04:00
*
* @ return array menu objects
2010-02-28 15:00:49 -05:00
*/
2010-03-15 12:26:46 -04:00
function wp_get_nav_menus () {
return get_terms ( 'nav_menu' , array ( 'hide_empty' => false , 'orderby' => 'id' ) );
2010-02-25 16:06:44 -05:00
}
2010-02-28 15:00:49 -05:00
/**
2010-03-15 12:26:46 -04:00
* Returns all menu items of a navigation menu .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
* @ param string $menu menu name , id , or slug
2010-03-17 12:27:25 -04:00
* @ param string $args
2010-02-28 15:00:49 -05:00
* @ return mixed $items array of menu items , else false .
*/
2010-02-24 03:06:36 -05:00
function wp_get_nav_menu_items ( $menu , $args = array () ) {
2010-02-28 15:00:49 -05:00
$menu = wp_get_nav_menu_object ( $menu );
2010-03-17 12:27:25 -04:00
2010-02-28 15:00:49 -05:00
if ( ! $menu )
return false ;
2010-03-17 12:27:25 -04:00
2010-02-28 15:00:49 -05:00
$items = get_objects_in_term ( $menu -> term_id , 'nav_menu' );
2010-02-24 03:06:36 -05:00
if ( ! empty ( $items ) ) {
$defaults = array ( 'orderby' => 'menu_order' , 'post_type' => 'nav_menu_item' , 'post_status' => 'publish' , 'output' => ARRAY_A , 'output_key' => 'menu_order' );
2010-02-28 15:00:49 -05:00
$args = wp_parse_args ( $args , $defaults );
2010-03-17 12:27:25 -04:00
if ( count ( $items ) > 1 )
2010-02-24 03:06:36 -05:00
$args [ 'include' ] = implode ( ',' , $items );
else
$args [ 'include' ] = $items [ 0 ];
$items = get_posts ( $args );
if ( ARRAY_A == $args [ 'output' ] ) {
$output = array ();
foreach ( $items as $item ) {
$output [ $item -> $args [ 'output_key' ]] = $item ;
}
2010-02-28 15:00:49 -05:00
unset ( $items );
ksort ( $output );
2010-02-24 03:06:36 -05:00
return $output ;
}
}
return $items ;
}
2010-02-28 15:00:49 -05:00
/**
2010-03-22 15:56:16 -04:00
* Retrieve the HTML list content for nav menu items .
*
* @ uses Walker_Nav_Menu to create HTML list content .
* @ since 2.1 . 0
* @ see Walker :: walk () for parameters and return description .
*/
function walk_nav_menu_tree ( $items , $depth , $r ) {
$walker = ( empty ( $r -> walker ) ) ? new Walker_Nav_Menu : $r -> walker ;
$args = array ( $items , $depth , $r );
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
return call_user_func_array ( array ( & $walker , 'walk' ), $args );
}
/**
* Adds all the navigation menu properties to the menu item .
2010-02-28 15:00:49 -05:00
*
* @ since 3.0 . 0
*
2010-03-15 12:26:46 -04:00
* @ param string $menu_item The menu item to modify
2010-03-22 15:56:16 -04:00
* @ param string $menu_item_type The menu item type ( frontend , custom , post_type , taxonomy ) .
2010-03-15 12:26:46 -04:00
* @ param string $menu_item_object Optional . The menu item object type ( post type or taxonomy ) .
2010-03-22 15:56:16 -04:00
* @ return object $menu_item The modified menu item .
2010-02-28 15:00:49 -05:00
*/
2010-03-15 12:26:46 -04:00
function wp_setup_nav_menu_item ( $menu_item , $menu_item_type = null , $menu_item_object = '' ) {
switch ( $menu_item_type ) {
case 'frontend' :
$menu_item -> db_id = ( int ) $menu_item -> ID ;
2010-03-22 15:56:16 -04:00
$menu_item -> object_id = get_post_meta ( $menu_item -> ID , '_menu_item_object_id' , true );
$menu_item -> object = get_post_meta ( $menu_item -> ID , '_menu_item_object' , true );
$menu_item -> type = get_post_meta ( $menu_item -> ID , '_menu_item_type' , true );
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
if ( 'post_type' == $menu_item -> type ) {
$object = get_post_type_object ( $menu_item -> object );
$menu_item -> append = $object -> singular_label ;
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
} elseif ( 'taxonomy' == $menu_item -> type ) {
$object = get_taxonomy ( $menu_item -> object );
$menu_item -> append = $object -> singular_label ;
2010-03-26 15:36:49 -04:00
2010-03-22 15:56:16 -04:00
} else {
$menu_item -> append = __ ( 'Custom' );
}
2010-03-26 15:36:49 -04:00
2010-03-15 12:26:46 -04:00
$menu_item -> title = $menu_item -> post_title ;
2010-03-22 15:56:16 -04:00
$menu_item -> url = get_post_meta ( $menu_item -> ID , '_menu_item_url' , true );
$menu_item -> target = get_post_meta ( $menu_item -> ID , '_menu_item_target' , true );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$menu_item -> attr_title = strip_tags ( $menu_item -> post_excerpt );
$menu_item -> description = strip_tags ( $menu_item -> post_content );
2010-03-17 12:27:25 -04:00
2010-03-26 15:36:49 -04:00
$menu_item -> classes = get_post_meta ( $menu_item -> ID , '_menu_item_classes' , true );
2010-03-22 15:56:16 -04:00
$menu_item -> xfn = get_post_meta ( $menu_item -> ID , '_menu_item_xfn' , true );
2010-02-25 03:48:17 -05:00
break ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
case 'custom' :
2010-03-22 15:56:16 -04:00
$menu_item -> db_id = 0 ;
2010-03-15 12:26:46 -04:00
$menu_item -> object_id = ( int ) $menu_item -> ID ;
2010-03-22 15:56:16 -04:00
$menu_item -> object = 'custom' ;
$menu_item -> type = 'custom' ;
$menu_item -> append = __ ( 'custom' );
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$menu_item -> attr_title = strip_tags ( $menu_item -> post_excerpt );
$menu_item -> description = strip_tags ( $menu_item -> post_content );
$menu_item -> title = $menu_item -> post_title ;
2010-03-22 15:56:16 -04:00
$menu_item -> url = get_post_meta ( $menu_item -> ID , '_menu_item_url' , true );
$menu_item -> target = get_post_meta ( $menu_item -> ID , '_menu_item_target' , true );
$menu_item -> classes = '' ;
$menu_item -> xfn = '' ;
2010-02-25 03:48:17 -05:00
break ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
case 'post_type' :
$menu_item -> db_id = 0 ;
$menu_item -> object_id = ( int ) $menu_item -> ID ;
$menu_item -> type = $menu_item_type ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$object = get_post_type_object ( $menu_item_object );
2010-03-22 15:56:16 -04:00
$menu_item -> object = $object -> name ;
$menu_item -> append = strtolower ( $object -> singular_label );
2010-03-15 12:26:46 -04:00
$menu_item -> title = $menu_item -> post_title ;
2010-03-16 12:34:30 -04:00
$menu_item -> url = get_permalink ( $menu_item -> ID );
2010-03-16 15:20:10 -04:00
$menu_item -> target = '_self' ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$menu_item -> attr_title = '' ;
$menu_item -> description = strip_tags ( $menu_item -> post_content );
2010-03-22 15:56:16 -04:00
$menu_item -> classes = '' ;
$menu_item -> xfn = '' ;
2010-03-15 12:26:46 -04:00
break ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
case 'taxonomy' :
$menu_item -> ID = $menu_item -> term_id ;
$menu_item -> db_id = 0 ;
$menu_item -> object_id = ( int ) $menu_item -> term_id ;
2010-03-22 15:56:16 -04:00
$menu_item -> post_parent = ( int ) $menu_item -> parent ;
2010-03-15 12:26:46 -04:00
$menu_item -> type = $menu_item_type ;
2010-03-17 12:27:25 -04:00
2010-03-15 12:26:46 -04:00
$object = get_taxonomy ( $menu_item_object );
2010-03-22 15:56:16 -04:00
$menu_item -> object = $object -> name ;
$menu_item -> append = strtolower ( $object -> singular_label );
2010-03-15 12:26:46 -04:00
$menu_item -> title = $menu_item -> name ;
$menu_item -> url = get_term_link ( $menu_item , $menu_item_object );
2010-03-16 15:20:10 -04:00
$menu_item -> target = '_self' ;
2010-03-15 12:26:46 -04:00
$menu_item -> attr_title = '' ;
$menu_item -> description = strip_tags ( $menu_item -> description );
2010-03-22 15:56:16 -04:00
$menu_item -> classes = '' ;
$menu_item -> xfn = '' ;
2010-02-25 03:48:17 -05:00
break ;
2010-03-26 15:36:49 -04:00
}
2010-02-24 03:06:36 -05:00
return $menu_item ;
}
2010-03-26 15:36:49 -04:00
?>