Skip to content

Commit

Permalink
Fix position in menu item update
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Feb 15, 2024
1 parent a9761dc commit bf7ff5c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Menu_Item_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,12 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
$this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'], +1, $result );
}

$position_value = Utils\get_flag_value( $assoc_args, 'position' );

if ( 'update' === $method && ! empty( $position_value ) ) {
$this->update_menu_item_position( $menu->term_id, $menu_item_db_id, $position_value );
}

/**
* Set the menu
*
Expand All @@ -511,6 +517,47 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
}
}

/**
* Update menu items positions.
*
* @param int $menu_id ID of the nav_menu.
* @param int $menu_item_id Menu item ID.
* @param int $position New menu item position.
*/
private function update_menu_item_position( $menu_id, $menu_item_id, $position ) {
global $wpdb;

$position = absint( $position );

// Bail if position is not numeric.
if ( 0 === $position ) {
return;
}

$menu_items = wp_get_nav_menu_items( $menu_id );

$position = ( $position > count( $menu_items ) ) ? count( $menu_items ) : $position;

$all_positions = wp_list_pluck( $menu_items, 'ID', 'menu_order' );

$old_key = array_search( (int) $menu_item_id, $all_positions, true );

// Bail if new position is same as existing.
if ( $old_key === $position ) {
return;
}

// Remove old item from the list.
unset( $all_positions[ $old_key ] );

// Add ID in new position.
array_splice( $all_positions, $position - 1, 0, (int) $menu_item_id );

foreach ( $all_positions as $key => $id ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET `menu_order`=%s WHERE ID=%s", ( $key + 1 ), $id ) );
}
}

/**
* Move block of items in one nav_menu up or down by incrementing/decrementing their menu_order field.
* Expects the menu items to have proper menu_orders (i.e. doesn't fix errors from previous incorrect operations).
Expand Down

0 comments on commit bf7ff5c

Please sign in to comment.