X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/699231ae09f7057a4d0000cdf32e50a3df6a04ca..61343b82c4f0da4c68e4c6373daafff4a81efdd1:/wp-includes/nav-menu.php diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 0d6dd322..794f4b2d 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -78,6 +78,23 @@ function register_nav_menus( $locations = array() ) { $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations ); } +/** + * Unregisters a navigation menu for a theme. + * + * @param array $location the menu location identifier + * + * @return bool True on success, false on failure. + */ +function unregister_nav_menu( $location ) { + global $_wp_registered_nav_menus; + + if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) { + unset( $_wp_registered_nav_menus[$location] ); + return true; + } + return false; +} + /** * Register a navigation menu for a theme. * @@ -110,7 +127,8 @@ function get_registered_nav_menus() { */ 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(); } /** @@ -171,6 +189,14 @@ function wp_delete_nav_menu( $menu ) { $result = wp_delete_term( $menu->term_id, 'nav_menu' ); + // Remove this menu from any locations. + $locations = get_theme_mod( 'nav_menu_locations' ); + foreach ( (array) $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 ); @@ -262,10 +288,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, @@ -288,8 +310,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; @@ -313,13 +336,12 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item $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 @@ -333,20 +355,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'] ) { @@ -354,14 +375,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']) ); @@ -372,10 +390,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; @@ -386,7 +411,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item * * @since 3.0.0 * - * @param $args array Array of arguments passed on to get_terms(). + * @param array $args Array of arguments passed on to get_terms(). * @return array menu objects */ function wp_get_nav_menus( $args = array() ) { @@ -425,6 +450,22 @@ function _sort_nav_menu_items( $a, $b ) { return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop ); } +/** + * Returns if a menu item is valid. Bug #13958 + * + * @since 3.2.0 + * @access private + * + * @param object $menu_item The menu item to check + * @return bool false if invalid, else true. + */ +function _is_valid_nav_menu_item( $item ) { + if ( ! empty( $item->_invalid ) ) + return false; + + return true; +} + /** * Returns all menu items of a navigation menu. * @@ -450,8 +491,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 ); @@ -496,6 +536,9 @@ function wp_get_nav_menu_items( $menu, $args = array() ) { $items = array_map( 'wp_setup_nav_menu_item', $items ); + if ( ! is_admin() ) // Remove invalid items only in frontend + $items = array_filter( $items, '_is_valid_nav_menu_item' ); + if ( ARRAY_A == $args['output'] ) { $GLOBALS['_menu_item_sort_prop'] = $args['output_key']; usort($items, '_sort_nav_menu_items'); @@ -518,7 +561,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. @@ -543,7 +586,13 @@ function wp_setup_nav_menu_item( $menu_item ) { if ( 'post_type' == $menu_item->type ) { $object = get_post_type_object( $menu_item->object ); - $menu_item->type_label = $object->labels->singular_name; + if ( $object ) { + $menu_item->type_label = $object->labels->singular_name; + } else { + $menu_item->type_label = $menu_item->object; + $menu_item->_invalid = true; + } + $menu_item->url = get_permalink( $menu_item->object_id ); $original_object = get_post( $menu_item->object_id ); @@ -552,11 +601,19 @@ function wp_setup_nav_menu_item( $menu_item ) { } elseif ( 'taxonomy' == $menu_item->type ) { $object = get_taxonomy( $menu_item->object ); - $menu_item->type_label = $object->labels->singular_name; + if ( $object ) { + $menu_item->type_label = $object->labels->singular_name; + } else { + $menu_item->type_label = $menu_item->object; + $menu_item->_invalid = true; + } + $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); $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 { @@ -568,7 +625,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; @@ -582,12 +641,15 @@ function wp_setup_nav_menu_item( $menu_item ) { $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 = ''; } @@ -636,7 +698,7 @@ function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_ 'meta_value' => $object_id, 'post_status' => 'any', 'post_type' => 'nav_menu_item', - 'showposts' => -1, + 'posts_per_page' => -1, ) ); foreach( (array) $menu_items as $menu_item ) { @@ -730,5 +792,3 @@ function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { wp_update_nav_menu_item( $menu_id, 0, $args ); } } - -?>