Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menu ordering on item update #502

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions features/menu-item.feature
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,49 @@ Feature: Manage WordPress menu items
| type | title | position | link |
| custom | First | 1 | https://first.com |
| custom | Third | 2 | https://third.com |

Scenario: Menu order is updated after item update
When I run `wp menu create "Sidebar Menu"`
Then STDOUT should not be empty

When I run `wp menu item add-custom sidebar-menu First https://first.com --porcelain`
Then save STDOUT as {ITEM_ID_1}

When I run `wp menu item add-custom sidebar-menu Second https://second.com --porcelain`
Then save STDOUT as {ITEM_ID_2}

When I run `wp menu item add-custom sidebar-menu Third https://third.com --porcelain`
Then save STDOUT as {ITEM_ID_3}

When I run `wp menu item list sidebar-menu --fields=type,title,position,link`
Then STDOUT should be a table containing rows:
| type | title | position | link |
| custom | First | 1 | https://first.com |
| custom | Second | 2 | https://second.com |
| custom | Third | 3 | https://third.com |

When I run `wp menu item update {ITEM_ID_3} --position=1`
Then STDOUT should be:
"""
Success: Menu item updated.
"""

When I run `wp menu item list sidebar-menu --fields=type,title,position,link`
Then STDOUT should be a table containing rows:
| type | title | position | link |
| custom | Third | 1 | https://third.com |
| custom | First | 2 | https://first.com |
| custom | Second | 3 | https://second.com |

When I run `wp menu item update {ITEM_ID_3} --position=2`
Then STDOUT should be:
"""
Success: Menu item updated.
"""

When I run `wp menu item list sidebar-menu --fields=type,title,position,link`
Then STDOUT should be a table containing rows:
| type | title | position | link |
| custom | First | 1 | https://first.com |
| custom | Third | 2 | https://third.com |
| custom | Second | 3 | https://second.com |
10 changes: 9 additions & 1 deletion src/Menu_Item_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,14 @@ 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 );
}

if ( ( 'update' === $method ) && $menu_item_args['menu-item-position'] ) {
if ( $menu_item_args['menu-item-position'] > $position ) {
$this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'], -1, $result );
} elseif ( $menu_item_args['menu-item-position'] < $position ) {
$this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'] - 1, +1, $result );
}
}

/**
* Set the menu
*
Expand All @@ -504,7 +512,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
WP_CLI::line( $result );
} elseif ( 'add' === $method ) {
WP_CLI::success( 'Menu item added.' );
WP_CLI::success( 'Menu item added.' );
} elseif ( 'update' === $method ) {
WP_CLI::success( 'Menu item updated.' );
}
Expand Down
Loading