New menu UI. Props filosofo. see #12713

git-svn-id: https://develop.svn.wordpress.org/trunk@14248 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren
2010-04-27 01:05:58 +00:00
parent cb27ac1a30
commit 77be931ef4
20 changed files with 2278 additions and 1203 deletions
-96
View File
@@ -1123,102 +1123,6 @@ class Walker {
}
}
/**
* Create HTML list of nav menu items.
*
* @package WordPress
* @since 3.0.0
* @uses Walker
*/
class Walker_Nav_Menu extends Walker {
/**
* @see Walker::$tree_type
* @since 3.0.0
* @var string
*/
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* @see Walker::$db_fields
* @since 3.0.0
* @todo Decouple this.
* @var array
*/
var $db_fields = array( 'parent' => 'post_parent', 'id' => 'object_id' );
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = $value = '';
if ( 'frontend' == $args->context ) {
global $wp_query;
$classes = array( 'menu-item', 'menu-item-type-'. $item->type, $item->classes );
if ( 'custom' != $item->object )
$classes[] = 'menu-item-object-'. $item->object;
if ( $item->object_id == $wp_query->get_queried_object_id() )
$classes[] = 'current-menu-item';
// @todo add classes for parent/child relationships
$classes = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$classes = ' class="' . esc_attr( $classes ) . '"';
} else {
$value = ' value="' . $item->ID . '"';
}
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $classes .'>' . wp_get_nav_menu_item( $item, $args->context, $args );
}
/**
* @see Walker::end_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
*/
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}
/**
* Create HTML list of pages.
*
+173 -88
View File
@@ -7,6 +7,160 @@
* @since 3.0.0
*/
/**
* Create HTML list of nav menu items.
*
* @package WordPress
* @since 3.0.0
* @uses Walker
*/
class Walker_Nav_Menu extends Walker {
/**
* @see Walker::$tree_type
* @since 3.0.0
* @var string
*/
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* @see Walker::$db_fields
* @since 3.0.0
* @todo Decouple this.
* @var array
*/
var $db_fields = array( 'parent' => 'post_parent', 'id' => 'object_id' );
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = $value = '';
$classes = array( 'menu-item', 'menu-item-type-'. $item->type, $item->classes );
if ( 'custom' != $item->object )
$classes[] = 'menu-item-object-'. $item->object;
if ( $item->object_id == $wp_query->get_queried_object_id() )
$classes[] = 'current-menu-item';
// @todo add classes for parent/child relationships
$classes = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$classes = ' class="' . esc_attr( $classes ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $classes .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'wp_get_nav_menu_item', $item_output, $args );
}
/**
* @see Walker::end_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
*/
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}
/**
* Create HTML list of nav menu input items.
*
* @package WordPress
* @since 3.0.0
* @uses Walker_Nav_Menu
*/
class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
static $_placeholder;
$_placeholder = 0 > $_placeholder ? $_placeholder - 1 : -1;
$possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_placeholder;
$possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$output .= $indent . '<li>';
$output .= '<label class="menu-item-title">';
$output .= '<input type="checkbox" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" />';
$output .= $item->title .'</label>';
// Menu item hidden fields
$output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
$output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
$output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="'. esc_attr( $item->post_parent ) .'" />';
$output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="'. esc_attr( $item->type ) .'" />';
$output .= '<input type="hidden" class="menu-item-append" name="menu-item[' . $possible_object_id . '][menu-item-append]" value="'. esc_attr( $item->append ) .'" />';
$output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
$output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
$output .= '<input type="hidden" class="menu-item-append" name="menu-item[' . $possible_object_id . '][menu-item-append]" value="'. esc_attr( $item->append ) .'" />';
$output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
$output .= '<input type="hidden" class="menu-item-attr_title" name="menu-item[' . $possible_object_id . '][menu-item-attr_title]" value="'. esc_attr( $item->attr_title ) .'" />';
$output .= '<input type="hidden" class="menu-item-description" name="menu-item[' . $possible_object_id . '][menu-item-description]" value="'. esc_attr( $item->description ) .'" />';
$output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( $item->classes ) .'" />';
$output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
}
}
/**
* Displays a navigation menu.
*
@@ -17,9 +171,6 @@
* menu_class - CSS class to use for the div container of the menu list. Defaults to 'menu'.
* format - Whether to format the ul. Defaults to 'div'.
* fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
* container - Type of container tag. Avalible options div, p, or nav. Defaults to 'div'.
* container_class - Chooses a class for the container.
* container_id - Chooses an id for the container.
* before - Text before the link text.
* after - Text after the link text.
* link_before - Text before the link.
@@ -33,7 +184,7 @@
* @param array $args Arguments
*/
function wp_nav_menu( $args = array() ) {
$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'echo' => true,
$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'menu_class' => 'menu', 'echo' => true,
'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
'depth' => 0, 'walker' => '', 'context' => 'frontend' );
@@ -46,7 +197,7 @@ function wp_nav_menu( $args = array() ) {
// If we couldn't find a menu based off the name, id or slug,
// get the first menu that has items.
if ( !$menu ) {
if ( ! $menu ) {
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
if ( wp_get_nav_menu_items($menu_maybe->term_id) ) {
@@ -56,9 +207,9 @@ function wp_nav_menu( $args = array() ) {
}
}
// If the menu exists, get it's items.
if ( $menu && !is_wp_error($menu) )
$menu_items = wp_get_nav_menu_items( $menu->term_id, $args->context );
// If the menu exists, get its items.
if ( $menu && ! is_wp_error($menu) )
$menu_items = wp_get_nav_menu_items( $menu->term_id );
// If no menu was found or if the menu has no items, call the fallback_cb
if ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) ) ) {
@@ -73,15 +224,15 @@ function wp_nav_menu( $args = array() ) {
if ( in_array( $args->container, $container_allowedtags ) ) {
$class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-'. $menu->slug .'-container"';
$container_id = $args->container_id ? ' id="' . esc_attr($args->container_id) . '"' : '' ;
$nav_menu .= '<'. $args->container . $class . $container_id .'>';
$nav_menu .= '<'. $args->container . $class .'>';
}
// Set up the $menu_item variables
$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item( $menu_item, 'frontend' );
$sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item( $menu_item );
$items .= walk_nav_menu_tree( $menu_items, $args->depth, $args );
$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
// Attributes
$attributes = ' id="menu-' . $menu->slug . '"';
@@ -112,83 +263,17 @@ function wp_nav_menu( $args = array() ) {
}
/**
* Returns the menu item formatted based on it's context.
* Retrieve the HTML list content for nav menu items.
*
* @since 3.0.0
*
* @param string $menu_item The menu item to format.
* @param string $context The context to which the menu item will be formatted to.
* @param string $args Optional. Args used for the 'template' context.
* @return string $output The menu formatted menu item.
* @uses Walker_Nav_Menu to create HTML list content.
* @since 2.1.0
* @see Walker::walk() for parameters and return description.
*/
function wp_get_nav_menu_item( $menu_item, $context = 'frontend', $args = array() ) {
$output = '';
switch ( $context ) {
case 'frontend':
$attributes = ! empty( $menu_item->attr_title ) ? ' title="' . esc_attr( $menu_item->attr_title ) .'"' : '';
$attributes .= ! empty( $menu_item->target ) ? ' target="' . esc_attr( $menu_item->target ) .'"' : '';
$attributes .= ! empty( $menu_item->xfn ) ? ' rel="' . esc_attr( $menu_item->xfn ) .'"' : '';
$attributes .= ! empty( $menu_item->url ) ? ' href="' . esc_attr( $menu_item->url ) .'"' : '';
function walk_nav_menu_tree( $items, $depth, $r ) {
$walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
$args = array( $items, $depth, $r );
$output .= $args->before;
$output .= '<a'. $attributes .'>';
$output .= $args->link_before . apply_filters( 'the_title', $menu_item->title ) . $args->link_after;
$output .= '</a>';
$output .= $args->after;
break;
case 'backend':
$output .= '<dl><dt>';
$output .= '<span class="item-title">'. esc_html( $menu_item->title ) .'</span>';
$output .= '<span class="item-controls">';
$output .= '<span class="item-type">'. esc_html( $menu_item->append ) .'</span>';
// Actions
$output .= '<a class="item-edit thickbox" id="edit-'. esc_attr( $menu_item->ID ) .'" value="'. esc_attr( $menu_item->ID ) .'" title="'. __('Edit Menu Item') .'" href="#TB_inline?height=540&width=300&inlineId=menu-item-settings">'. __('Edit') .'</a> | ';
$output .= '<a class="item-delete" id="delete-'. esc_attr( $menu_item->ID ) .'" value="'. esc_attr( $menu_item->ID ) .'">'. __('Delete') .'</a>';
$output .= '</span></dt></dl>';
// Menu Item Settings
$output .= '<input type="hidden" name="menu-item-db-id[]" value="'. esc_attr( $menu_item->ID ) .'" />';
$output .= '<input type="hidden" name="menu-item-object-id[]" value="'. esc_attr( $menu_item->object_id ) .'" />';
$output .= '<input type="hidden" name="menu-item-object[]" value="'. esc_attr( $menu_item->object ) .'" />';
$output .= '<input type="hidden" name="menu-item-parent-id[]" value="'. esc_attr( $menu_item->post_parent ) .'" />';
$output .= '<input type="hidden" name="menu-item-position[]" value="'. esc_attr( $menu_item->menu_order ) .'" />';
$output .= '<input type="hidden" name="menu-item-type[]" value="'. esc_attr( $menu_item->type ) .'" />';
$output .= '<input type="hidden" name="menu-item-title[]" value="'. esc_attr( $menu_item->title ) .'" />';
$output .= '<input type="hidden" name="menu-item-url[]" value="'. esc_attr( $menu_item->url ) .'" />';
$output .= '<input type="hidden" name="menu-item-description[]" value="'. esc_attr( $menu_item->description ) .'" />';
$output .= '<input type="hidden" name="menu-item-classes[]" value="'. esc_attr( $menu_item->classes ) .'" />';
$output .= '<input type="hidden" name="menu-item-xfn[]" value="'. esc_attr( $menu_item->xfn ) .'" />';
$output .= '<input type="hidden" name="menu-item-attr-title[]" value="'.esc_attr( $menu_item->post_excerpt ) .'" />';
$output .= '<input type="hidden" name="menu-item-target[]" value="'. esc_attr( $menu_item->target ) .'" />';
break;
case 'custom':
case 'taxonomy':
case 'post_type':
$output .= '<label class="menu-item-title"><input type="checkbox" id="'. esc_attr( 'menu-item-' . $menu_item->object_id ) .'" value="'. esc_attr( $menu_item->url ) .'" />'. $menu_item->title .'</label>';
// Menu item hidden fields
$output .= '<input type="hidden" class="menu-item-db-id" value="0" />';
$output .= '<input type="hidden" class="menu-item-object-id" value="'. esc_attr( $menu_item->object_id ) .'" />';
$output .= '<input type="hidden" class="menu-item-object" value="'. esc_attr( $menu_item->object ) .'" />';
$output .= '<input type="hidden" class="menu-item-parent-id" value="'. esc_attr( $menu_item->post_parent ) .'" />';
$output .= '<input type="hidden" class="menu-item-type" value="'. esc_attr( $menu_item->type ) .'" />';
$output .= '<input type="hidden" class="menu-item-append" value="'. esc_attr( $menu_item->append ) .'" />';
$output .= '<input type="hidden" class="menu-item-title" value="'. esc_attr( $menu_item->title ) .'" />';
$output .= '<input type="hidden" class="menu-item-url" value="'. esc_attr( $menu_item->url ) .'" />';
$output .= '<input type="hidden" class="menu-item-append" value="'. esc_attr( $menu_item->append ) .'" />';
$output .= '<input type="hidden" class="menu-item-target" value="'. esc_attr( $menu_item->target ) .'" />';
$output .= '<input type="hidden" class="menu-item-attr_title" value="'. esc_attr( $menu_item->attr_title ) .'" />';
$output .= '<input type="hidden" class="menu-item-description" value="'. esc_attr( $menu_item->description ) .'" />';
$output .= '<input type="hidden" class="menu-item-classes" value="'. esc_attr( $menu_item->classes ) .'" />';
$output .= '<input type="hidden" class="menu-item-xfn" value="'. esc_attr( $menu_item->xfn ) .'" />';
break;
}
return apply_filters( 'wp_get_nav_menu_item', $output, $context, $args );
return call_user_func_array( array(&$walker, 'walk'), $args );
}
?>
?>
+261 -93
View File
@@ -16,7 +16,22 @@
* @return mixed $menu|false Or WP_Error
*/
function wp_get_nav_menu_object( $menu ) {
return is_nav_menu( $menu );
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;
}
return $menu_obj;
}
/**
@@ -27,25 +42,18 @@ function wp_get_nav_menu_object( $menu ) {
* @since 3.0.0
*
* @param int|string $menu The menu to check
* @return mixed Menu Object, if it exists. Else, false or WP_Error
* @return bool Whether the menu exists.
*/
function is_nav_menu( $menu ) {
if ( !$menu )
if ( ! $menu )
return false;
$menu_obj = wp_get_nav_menu_object( $menu );
$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;
}
return $menu_obj;
if ( $menu_obj && ! is_wp_error( $menu_obj ) && ! empty( $menu_obj->term_id ) )
return true;
return false;
}
/**
@@ -96,7 +104,7 @@ function wp_create_nav_menu( $menu_name, $args = array() ) {
*/
function wp_delete_nav_menu( $menu ) {
$menu = wp_get_nav_menu_object( $menu );
if ( !$menu )
if ( ! $menu )
return false;
$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
@@ -116,6 +124,151 @@ function wp_delete_nav_menu( $menu ) {
}
}
/**
* Save the properties of a menu or create a new menu with those properties.
*
* @since 3.0.0
*
* @param int $menu_id The ID of the menu
* @param array $menu_data The array of menu data.
* @return int The menu's ID.
*/
function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
$menu_id = (int) $menu_id;
$_menu = wp_get_nav_menu_object( $menu_id );
// menu doesn't already exist
if ( ! $_menu || is_wp_error( $_menu ) ) {
$_menu = wp_create_nav_menu( $menu_data['menu-name'] );
}
if ( $_menu && isset( $_menu->term_id ) && ! is_wp_error( $_menu ) ) {
$args = array(
'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
'slug' => null,
);
$menu_id = (int) $_menu->term_id;
$update_response = wp_update_term( $menu_id, 'nav_menu', $args );
if ( ! is_wp_error( $update_response ) ) {
return $menu_id;
}
} else {
return 0;
}
}
/**
* Save the properties of a menu item or create a new one.
*
* @since 3.0.0
*
* @param int $menu_id The ID of the menu. Required.
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
* @param array $menu_item_data The menu item's data.
* @return int The menu item's database ID or WP_Error object on failure.
*/
function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
$menu_id = (int) $menu_id;
$menu_item_db_id = (int) $menu_item_db_id;
$menu = wp_get_nav_menu_object( $menu_id );
if ( ! $menu || is_wp_error( $menu ) ) {
return $menu;
}
$menu_items = (array) wp_get_nav_menu_items( $menu_id );
$count = count( $menu_items );
$defaults = array(
'menu-item-db-id' => $menu_item_db_id,
'menu-item-object-id' => 0,
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 0,
'menu-item-type' => 'custom',
'menu-item-append' => 'custom',
'menu-item-title' => '',
'menu-item-url' => '',
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-target' => '',
'menu-item-classes' => '',
'menu-item-xfn' => '',
);
$args = wp_parse_args( $menu_item_data, $defaults );
if ( 0 == (int) $args['menu-item-position'] ) {
$last_item = array_pop( $menu_items );
if ( $last_item && isset( $last_item->ID ) ) {
$last_data = get_post( $last_item->ID );
if ( ! is_wp_error( $last_data ) && isset( $last_data->menu_order ) ) {
$args['menu-item-position'] = 1 + (int) $last_data->menu_order;
}
} else {
$args['menu-item-position'] = $count;
}
}
// Populate the menu item object
$post = array(
'menu_order' => $args['menu-item-position'],
'ping_status' => 0,
'post_content' => $args['menu-item-description'],
'post_excerpt' => $args['menu-item-attr-title'],
'post_parent' => $args['menu-item-parent-id'],
'post_status' => 'publish',
'post_title' => $args['menu-item-title'],
'post_type' => 'nav_menu_item',
'tax_input' => array( 'nav_menu' => $menu->name ),
);
// New menu item
if ( 0 == $menu_item_db_id ) {
$menu_item_db_id = wp_insert_post( $post );
// Update existing menu item
} else {
$post['ID'] = $menu_item_db_id;
wp_update_post( $post );
}
if ( 'custom' == $args['menu-item-type'] ) {
$args['menu-item-object-id'] = $menu_item_db_id;
$args['menu-item-object'] = 'custom';
}
if ( $menu_item_db_id && ! is_wp_error( $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_object_id', (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']) );
// @todo handle sanitizing multiple classes separated by whitespace.
update_post_meta( $menu_item_db_id, '_menu_item_classes', sanitize_html_class($args['menu-item-classes']) );
update_post_meta( $menu_item_db_id, '_menu_item_xfn', sanitize_html_class($args['menu-item-xfn']) );
// @todo: only save custom link urls.
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
}
return $menu_item_db_id;
}
/**
* Returns all navigation menu objects.
*
@@ -127,6 +280,43 @@ function wp_get_nav_menus() {
return get_terms( 'nav_menu', array( 'hide_empty' => false, 'orderby' => 'id' ) );
}
/**
* Sort menu items by the desired key.
*
* @since 3.0.0
* @access private
*
* @param object $a The first object to compare
* @param object $b The second object to compare
* @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
*/
function _sort_nav_menu_items($a, $b) {
global $_menu_item_sort_prop;
if ( empty( $_menu_item_sort_prop ) ) {
return 0;
}
if ( isset( $a->$_menu_item_sort_prop ) && isset( $b->$_menu_item_sort_prop ) ) {
$_a = (int) $a->$_menu_item_sort_prop;
$_b = (int) $b->$_menu_item_sort_prop;
if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop ) {
return 0;
} elseif (
( $_a == $a->$_menu_item_sort_prop ) &&
( $_b == $b->$_menu_item_sort_prop )
) {
return $_a < $_b ? -1 : 1;
} else {
return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
}
} else {
return 0;
}
}
/**
* Returns all menu items of a navigation menu.
*
@@ -139,13 +329,13 @@ function wp_get_nav_menus() {
function wp_get_nav_menu_items( $menu, $args = array() ) {
$menu = wp_get_nav_menu_object( $menu );
if ( !$menu )
if ( ! $menu )
return false;
$items = get_objects_in_term( $menu->term_id, 'nav_menu' );
if ( ! empty( $items ) ) {
$defaults = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order' );
$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order' );
$args = wp_parse_args( $args, $defaults );
if ( count( $items ) > 1 )
$args['include'] = implode( ',', $items );
@@ -155,50 +345,48 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
$items = get_posts( $args );
if ( ARRAY_A == $args['output'] ) {
$output = array();
foreach ( $items as $item ) {
$output[$item->$args['output_key']] = $item;
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
usort($items, '_sort_nav_menu_items');
$i = 1;
foreach( $items as $k => $item ) {
$items[$k]->$args['output_key'] = $i++;
}
unset( $items );
ksort( $output );
return $output;
}
}
return $items;
}
/**
* Retrieve the HTML list content for nav menu items.
* Decorates a menu item object with the shared navigation menu item properties.
*
* @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 );
return call_user_func_array( array(&$walker, 'walk'), $args );
}
/**
* Adds all the navigation menu properties to the menu item.
* Properties:
* - db_id: The DB ID of the this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
* - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
* - type: The family of objects originally represented, such as "post_type" or "taxonomy."
* - object: The type of object originally represented, such as "category," "post", or "attachment."
* - append: The singular label used to describe this type of menu item.
* - parent: The DB ID of the original object's parent object, 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.
* - attr_title: The title attribute of the link element for this menu item.
* - classes: The class attribute value for the link element of this menu item.
* - xfn: The XFN relationship expressed in the link of this menu item.
* - description: The description of this menu item.
*
* @since 3.0.0
*
* @param string $menu_item The menu item to modify
* @param string $menu_item_type The menu item type (frontend, custom, post_type, taxonomy).
* @param string $menu_item_object Optional. The menu item object type (post type or taxonomy).
* @return object $menu_item The modified menu item.
* @param object $menu_item The menu item to modify.
* @return object $menu_item The menu item with standard menu item properties.
*/
function wp_setup_nav_menu_item( $menu_item, $menu_item_type = null, $menu_item_object = '' ) {
switch ( $menu_item_type ) {
case 'frontend':
function wp_setup_nav_menu_item( $menu_item ) {
if ( isset( $menu_item->post_type ) ) {
if ( 'nav_menu_item' == $menu_item->post_type ) {
$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->object = get_post_meta( $menu_item->ID, '_menu_item_object', true );
$menu_item->type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
if ( 'post_type' == $menu_item->type ) {
$object = get_post_type_object( $menu_item->object );
$menu_item->append = $object->singular_label;
@@ -213,7 +401,7 @@ function wp_setup_nav_menu_item( $menu_item, $menu_item_type = null, $menu_item_
$menu_item->append = __('Custom');
$menu_item->url = get_post_meta( $menu_item->ID, '_menu_item_url', true );
}
$menu_item->title = $menu_item->post_title;
$menu_item->target = get_post_meta( $menu_item->ID, '_menu_item_target', true );
@@ -222,31 +410,12 @@ function wp_setup_nav_menu_item( $menu_item, $menu_item_type = null, $menu_item_
$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 );
break;
case 'custom':
} else {
$menu_item->db_id = 0;
$menu_item->object_id = (int) $menu_item->ID;
$menu_item->object = 'custom';
$menu_item->type = 'custom';
$menu_item->append = __('custom');
$menu_item->type = 'post_type';
$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 );
$menu_item->classes = get_post_meta( $menu_item->ID, '_menu_item_target', true );
$menu_item->xfn = get_post_meta( $menu_item->ID, '_menu_item_xfn', true );
break;
case 'post_type':
$menu_item->db_id = 0;
$menu_item->object_id = (int) $menu_item->ID;
$menu_item->type = $menu_item_type;
$object = get_post_type_object( $menu_item_object );
$object = get_post_type_object( $menu_item->post_type );
$menu_item->object = $object->name;
$menu_item->append = strtolower( $object->singular_label );
@@ -254,33 +423,32 @@ function wp_setup_nav_menu_item( $menu_item, $menu_item_type = null, $menu_item_
$menu_item->url = get_permalink( $menu_item->ID );
$menu_item->target = '';
$menu_item->attr_title = '';
$menu_item->attr_title = strip_tags( $menu_item->post_excerpt );
$menu_item->description = strip_tags( $menu_item->post_content );
$menu_item->classes = '';
$menu_item->xfn = '';
break;
}
} elseif ( isset( $menu_item->taxonomy ) ) {
$menu_item->ID = $menu_item->term_id;
$menu_item->db_id = 0;
$menu_item->object_id = (int) $menu_item->term_id;
$menu_item->post_parent = (int) $menu_item->parent;
$menu_item->type = 'taxonomy';
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->post_parent = (int) $menu_item->parent;
$menu_item->type = $menu_item_type;
$object = get_taxonomy( $menu_item->taxonomy );
$menu_item->object = $object->name;
$menu_item->append = strtolower( $object->singular_label );
$object = get_taxonomy( $menu_item_object );
$menu_item->object = $object->name;
$menu_item->append = strtolower( $object->singular_label );
$menu_item->title = $menu_item->name;
$menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
$menu_item->target = '';
$menu_item->attr_title = '';
$menu_item->description = strip_tags( get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy ) );
$menu_item->classes = '';
$menu_item->xfn = '';
$menu_item->title = $menu_item->name;
$menu_item->url = get_term_link( $menu_item, $menu_item_object );
$menu_item->target = '';
$menu_item->attr_title = '';
$menu_item->description = '';
$menu_item->classes = '';
$menu_item->xfn = '';
break;
}
return apply_filters( 'wp_setup_nav_menu_item', $menu_item, $menu_item_type, $menu_item_object );
return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
}
?>
?>
+2 -2
View File
@@ -2190,7 +2190,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
$data = stripslashes_deep( $data );
$where = array( 'ID' => $post_ID );
if ($update) {
if ( $update ) {
do_action( 'pre_post_update', $post_ID );
if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
if ( $wp_error )
@@ -2265,7 +2265,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
wp_transition_post_status($data['post_status'], $previous_status, $post);
if ( $update)
if ( $update )
do_action('edit_post', $post_ID, $post);
do_action('save_post', $post_ID, $post);
+6 -5
View File
@@ -393,12 +393,13 @@ function wp_default_scripts( &$scripts ) {
) );
// Custom Navigation
$scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100403' );
$scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100426' );
$scripts->localize( 'nav-menu', 'navMenuL10n', array(
'custom' => _x('Custom', 'menu nav item type'),
'thickbox' => _x('Edit Menu Item', 'Thickbox Title'),
'edit' => _x('Edit', 'menu item edit text'),
'warnDelete' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
'warnDeleteMenu' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
'warnDeleteMenuItem' => __( "You are about to permanently delete this menu item. \n 'Cancel' to stop, 'OK' to delete." ),
) );
$scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), '20100321' );
@@ -440,13 +441,13 @@ function wp_default_styles( &$styles ) {
// Any rtl stylesheets that don't have a .dev version for ltr
$no_suffix = array( 'farbtastic' );
$styles->add( 'wp-admin', "/wp-admin/css/wp-admin$suffix.css", array(), '20100424' );
$styles->add( 'wp-admin', "/wp-admin/css/wp-admin$suffix.css", array(), '20100426' );
$styles->add( 'ie', "/wp-admin/css/ie$suffix.css", array(), '20100219' );
$styles->add_data( 'ie', 'conditional', 'lte IE 7' );
// all colors stylesheets need to have the same query strings (cache manifest compat)
$colors_version = '20100418';
$colors_version = '20100426';
// Register "meta" stylesheet for admin colors. All colors-* style sheets should have the same version string.
$styles->add( 'colors', true, array(), $colors_version );
@@ -472,7 +473,7 @@ function wp_default_styles( &$styles ) {
$styles->add( 'farbtastic', '/wp-admin/css/farbtastic.css', array(), '1.2' );
$styles->add( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.css', array(), '0.9.8' );
$styles->add( 'imgareaselect', '/wp-includes/js/imgareaselect/imgareaselect.css', array(), '0.9.1' );
$styles->add( 'nav-menu', "/wp-admin/css/nav-menu$suffix.css", array(), '20100322' );
$styles->add( 'nav-menu', "/wp-admin/css/nav-menu$suffix.css", array(), '20100426' );
foreach ( $rtl_styles as $rtl_style ) {
$styles->add_data( $rtl_style, 'rtl', true );