*/
function get_nav_menu_locations() {
- return get_theme_mod( 'nav_menu_locations' );
+ $locations = get_theme_mod( 'nav_menu_locations' );
+ return ( is_array( $locations ) ) ? $locations : array();
}
/**
$result = wp_delete_term( $menu->term_id, 'nav_menu' );
+ // Remove this menu from any locations.
+ $locations = get_nav_menu_locations();
+ foreach ( $locations as $location => $menu_id ) {
+ if ( $menu_id == $menu->term_id )
+ $locations[ $location ] = 0;
+ }
+ set_theme_mod( 'nav_menu_locations', $locations );
+
if ( $result && !is_wp_error($result) )
do_action( 'wp_delete_nav_menu', $menu->term_id );
if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
return $menu;
- $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
-
- $count = count( $menu_items );
-
$defaults = array(
'menu-item-db-id' => $menu_item_db_id,
'menu-item-object-id' => 0,
if ( 0 == $menu_id ) {
$args['menu-item-position'] = 1;
} elseif ( 0 == (int) $args['menu-item-position'] ) {
+ $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
$last_item = array_pop( $menu_items );
- $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : $count;
+ $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
}
$original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
$original_title = $original_object->post_title;
}
- if ( empty( $args['menu-item-title'] ) || $args['menu-item-title'] == $original_title ) {
+ if ( $args['menu-item-title'] == $original_title )
$args['menu-item-title'] = '';
- // hack to get wp to create a post object when too many properties are empty
- if ( empty( $args['menu-item-description'] ) )
- $args['menu-item-description'] = ' ';
- }
+ // hack to get wp to create a post object when too many properties are empty
+ if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
+ $args['menu-item-description'] = ' ';
}
// Populate the menu item object
'post_type' => 'nav_menu_item',
);
- if ( 0 != $menu_id )
+ $update = 0 != $menu_item_db_id;
+
+ // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
+ if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) )
$post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
// New menu item. Default is draft status
- if ( 0 == $menu_item_db_id ) {
+ if ( ! $update ) {
$post['ID'] = 0;
$post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
$menu_item_db_id = wp_insert_post( $post );
-
- // Update existing menu item. Default is publish status
- } else {
- $post['ID'] = $menu_item_db_id;
- $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
- wp_update_post( $post );
+ if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
+ return $menu_item_db_id;
}
if ( 'custom' == $args['menu-item-type'] ) {
$args['menu-item-object'] = 'custom';
}
- if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
- return $menu_item_db_id;
-
$menu_item_db_id = (int) $menu_item_db_id;
update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
- update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (int) $args['menu-item-parent-id'] );
- update_post_meta( $menu_item_db_id, '_menu_item_object_id', (int) $args['menu-item-object-id'] );
+ update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
+ update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
if ( 0 == $menu_id )
- update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() );
- else
+ update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
+ elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
+ // Update existing menu item. Default is publish status
+ if ( $update ) {
+ $post['ID'] = $menu_item_db_id;
+ $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
+ wp_update_post( $post );
+ }
+
do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
return $menu_item_db_id;
* @return mixed $items array of menu items, else false.
*/
function wp_get_nav_menu_items( $menu, $args = array() ) {
- global $_wp_using_ext_object_cache;
-
$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu )
return $items;
$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
- 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true,
- 'update_post_term_cache' => false );
+ 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
$args = wp_parse_args( $args, $defaults );
if ( count( $items ) > 1 )
$args['include'] = implode( ',', $items );
return false;
// Get all posts and terms at once to prime the caches
- if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) {
+ if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
$fetched[$menu->term_id] = true;
$posts = array();
$terms = array();
* - object: The type of object originally represented, such as "category," "post", or "attachment."
* - type_label: The singular label used to describe this type of menu item.
* - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
- * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
+ * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
* - url: The URL to which this menu item points.
* - title: The title of this menu item.
* - target: The target attribute of the link element for this menu item.
$menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
$menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
- $menu_item->description = empty( $menu_item->description ) ? apply_filters( 'nav_menu_description', $menu_item->post_content ) : $menu_item->description;
+
+ if ( empty( $menu_item->description ) )
+ $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
$menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
$menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
$menu_item->object = $object->name;
$menu_item->type_label = $object->labels->singular_name;
+ if ( '' === $menu_item->post_title )
+ $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
+
$menu_item->title = $menu_item->post_title;
$menu_item->url = get_permalink( $menu_item->ID );
$menu_item->target = '';
- $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt );
- $menu_item->description = apply_filters( 'nav_menu_description', $menu_item->post_content );
+ $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
+ $menu_item->description = apply_filters( 'nav_menu_description', '' );
$menu_item->classes = array();
$menu_item->xfn = '';
}
*
* @param int $object_id The ID of the original object.
* @param string $object_type The type of object, such as "taxonomy" or "post_type."
+ * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
* @return array The array of menu item IDs; empty array if none;
*/
-function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type' ) {
+function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
$object_id = (int) $object_id;
$menu_item_ids = array();
);
foreach( (array) $menu_items as $menu_item ) {
if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
- if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) != $object_type )
+ if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) !== $object_type ||
+ get_post_meta( $menu_item->ID, '_menu_item_object', true ) !== $taxonomy )
continue;
$menu_item_ids[] = (int) $menu_item->ID;
* @param int $object_id The ID of the original object being trashed.
*
*/
-function _wp_delete_tax_menu_item( $object_id = 0 ) {
+function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
$object_id = (int) $object_id;
- $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy' );
+ $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
foreach( (array) $menu_item_ids as $menu_item_id ) {
wp_delete_post( $menu_item_id, true );
wp_update_nav_menu_item( $menu_id, 0, $args );
}
}
-
-?>