Optimize wp_update_nav_menu_item() for high performance. Only query items for a menu when we need them. Update items after updating its meta so the meta cache can be leveraged. see #22189.
git-svn-id: http://core.svn.wordpress.org/trunk@22235 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
26445266d4
commit
0ae808509c
|
@ -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 ) )
|
if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
|
||||||
return $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(
|
$defaults = array(
|
||||||
'menu-item-db-id' => $menu_item_db_id,
|
'menu-item-db-id' => $menu_item_db_id,
|
||||||
'menu-item-object-id' => 0,
|
'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 ) {
|
if ( 0 == $menu_id ) {
|
||||||
$args['menu-item-position'] = 1;
|
$args['menu-item-position'] = 1;
|
||||||
} elseif ( 0 == (int) $args['menu-item-position'] ) {
|
} 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 );
|
$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_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
|
||||||
|
@ -350,20 +347,18 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||||
'post_type' => 'nav_menu_item',
|
'post_type' => 'nav_menu_item',
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( 0 != $menu_id )
|
if ( $menu_id && ! 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 ) ) );
|
$post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
|
||||||
|
|
||||||
|
$update = 0 != $menu_item_db_id;
|
||||||
|
|
||||||
// New menu item. Default is draft status
|
// New menu item. Default is draft status
|
||||||
if ( 0 == $menu_item_db_id ) {
|
if ( ! $update ) {
|
||||||
$post['ID'] = 0;
|
$post['ID'] = 0;
|
||||||
$post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
|
$post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
|
||||||
$menu_item_db_id = wp_insert_post( $post );
|
$menu_item_db_id = wp_insert_post( $post );
|
||||||
|
if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
|
||||||
// Update existing menu item. Default is publish status
|
return $menu_item_db_id;
|
||||||
} else {
|
|
||||||
$post['ID'] = $menu_item_db_id;
|
|
||||||
$post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
|
|
||||||
wp_update_post( $post );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'custom' == $args['menu-item-type'] ) {
|
if ( 'custom' == $args['menu-item-type'] ) {
|
||||||
|
@ -371,14 +366,11 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||||
$args['menu-item-object'] = 'custom';
|
$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;
|
$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_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_menu_item_parent', strval( (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_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_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_target', sanitize_key($args['menu-item-target']) );
|
||||||
|
|
||||||
|
@ -389,10 +381,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']) );
|
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
|
||||||
|
|
||||||
if ( 0 == $menu_id )
|
if ( 0 == $menu_id )
|
||||||
update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() );
|
update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
|
||||||
else
|
elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
|
||||||
delete_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 );
|
do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
|
||||||
|
|
||||||
return $menu_item_db_id;
|
return $menu_item_db_id;
|
||||||
|
|
Loading…
Reference in New Issue