]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/nav-menu.php
WordPress 3.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / nav-menu.php
index 03032750c6d4f9b7b880750a7f3a659f7a64c66f..7a5da9e20269a49e9c6e2198f5cafd9aaadc6286 100644 (file)
@@ -279,10 +279,6 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
        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,
@@ -305,8 +301,9 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
        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;
@@ -350,20 +347,19 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
                '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'] ) {
@@ -371,14 +367,11 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
                $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']) );
 
@@ -389,10 +382,17 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
        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;
@@ -483,8 +483,7 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
                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 );
@@ -554,7 +553,7 @@ function wp_get_nav_menu_items( $menu, $args = 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.
@@ -605,6 +604,8 @@ function wp_setup_nav_menu_item( $menu_item ) {
                                $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
 
                                $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
+                               if ( is_wp_error( $original_title ) )
+                                       $original_title = false;
                                $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
 
                        } else {
@@ -616,7 +617,9 @@ function wp_setup_nav_menu_item( $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;
@@ -634,8 +637,8 @@ function wp_setup_nav_menu_item( $menu_item ) {
                        $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 = '';
                }
@@ -778,5 +781,3 @@ function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
                wp_update_nav_menu_item( $menu_id, 0, $args );
        }
 }
-
-?>