2010-02-24 03:06:36 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Navigation Menu functions
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Navigation Menus
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
2010-02-28 15:00:49 -05:00
|
|
|
/**
|
|
|
|
* Returns a Navigation Menu object
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param string $menu Menu id
|
|
|
|
* @return mixed $menu|false
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Check if Menu exists.
|
|
|
|
*
|
|
|
|
* Returns the menu object, or false if the term doesn't exist.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param int|string $menu The menu to check
|
|
|
|
* @return mixed Menu Object, if exists.
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Creates a navigation menu.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* @return mixed Menu object|WP_Error
|
|
|
|
*/
|
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-02-27 18:06:56 -05:00
|
|
|
return new WP_Error( 'menu_exists', sprintf( __('A menu named “%s” 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-02-28 15:00:49 -05:00
|
|
|
return get_term( $menu['term_id'], 'nav_menu') ;
|
2010-02-24 03:06:36 -05:00
|
|
|
}
|
|
|
|
|
2010-02-28 15:00:49 -05:00
|
|
|
/**
|
|
|
|
* Deletes a navigation menu.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param string $menu name|id|slug
|
|
|
|
* @return bool true on success, else false.
|
|
|
|
*/
|
|
|
|
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-03 21:23:30 -05:00
|
|
|
wp_delete_term( $menu->term_id, 'nav_menu' );
|
2010-02-28 15:00:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-15 12:26:46 -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-15 12:26:46 -04:00
|
|
|
* Adds all the nav 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
|
|
|
|
* @param string $menu_item_type The menu item type (template, custom, post_type, taxonomy).
|
|
|
|
* @param string $menu_item_object Optional. The menu item object type (post type or taxonomy).
|
|
|
|
* @return object $menu_item The modtified 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 = '' ) {
|
|
|
|
global $wp_query;
|
2010-03-17 12:27:25 -04:00
|
|
|
|
2010-03-15 12:26:46 -04:00
|
|
|
switch ( $menu_item_type ) {
|
|
|
|
case 'frontend':
|
|
|
|
$menu_item->db_id = (int) $menu_item->ID;
|
|
|
|
$menu_item->object_id = get_post_meta( $menu_item->ID, 'menu_item_object_id', true );
|
|
|
|
$menu_item->parent_id = (int) $menu_item->post_parent;
|
|
|
|
$menu_item->type = get_post_meta( $menu_item->ID, 'menu_item_type', true );
|
2010-03-16 18:01:49 -04:00
|
|
|
|
|
|
|
$menu_item->append = get_post_meta( $menu_item->ID, 'menu_item_append', true );
|
2010-03-17 12:27:25 -04:00
|
|
|
|
2010-03-15 12:26:46 -04:00
|
|
|
$menu_item->title = $menu_item->post_title;
|
|
|
|
$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-15 12:26:46 -04:00
|
|
|
$menu_item->classes = get_post_meta( $menu_item->ID, 'menu_item_classes', true );;
|
|
|
|
$menu_item->xfn = get_post_meta( $menu_item->ID, 'menu_item_xfn', true );
|
|
|
|
$menu_item->li_class = ( $menu_item->object_id == $wp_query->get_queried_object_id() ) ? ' current_page_item' : '';
|
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':
|
|
|
|
$menu_item->db_id = (int) $menu_item->ID;
|
|
|
|
$menu_item->object_id = (int) $menu_item->ID;
|
|
|
|
$menu_item->parent_id = (int) $menu_item->post_parent;
|
|
|
|
$menu_item->type = 'custom'; //$menu_item_type
|
2010-03-16 18:01:49 -04:00
|
|
|
$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;
|
|
|
|
$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-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->parent_id = (int) $menu_item->post_parent;
|
|
|
|
$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-16 18:01:49 -04:00
|
|
|
$menu_item->append = $object->name;
|
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 );
|
|
|
|
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;
|
|
|
|
$menu_item->parent_id = (int) $menu_item->parent;
|
|
|
|
$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-16 18:01:49 -04:00
|
|
|
$menu_item->append = $object->name;
|
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-02-25 03:48:17 -05:00
|
|
|
break;
|
2010-02-24 03:06:36 -05:00
|
|
|
}
|
2010-03-16 18:01:49 -04:00
|
|
|
|
2010-03-15 12:26:46 -04:00
|
|
|
$menu_item->classes = get_post_meta( $menu_item->ID, 'menu_item_classes', true );
|
|
|
|
$menu_item->xfn = get_post_meta( $menu_item->ID, 'menu_item_xfn', true );
|
2010-03-17 12:27:25 -04:00
|
|
|
|
2010-02-24 03:06:36 -05:00
|
|
|
return $menu_item;
|
|
|
|
}
|
2010-03-15 12:26:46 -04:00
|
|
|
?>
|