Skip to content

Commit bb9e9b2

Browse files
swissspidyCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 67bc91b commit bb9e9b2

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/Menu_Item_Command.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
634634
}
635635

636636
$menu_item_args['menu-item-type'] = $type;
637+
$pending_menu_order_updates = [];
637638

638639
// Reorder other menu items when the position changes on update.
639640
if ( 'update' === $method ) {
@@ -679,22 +680,18 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
679680
if ( $new_position < $old_position_normalized ) {
680681
// Moving up: items at 0-indexed [new_pos-1, old_pos-2] shift down by +1.
681682
for ( $i = $new_position - 1; $i <= $old_position_normalized - 2; $i++ ) {
682-
wp_update_post(
683-
[
684-
'ID' => $sorted_items[ $i ]->ID,
685-
'menu_order' => $i + 2,
686-
]
687-
);
683+
$pending_menu_order_updates[] = [
684+
'ID' => $sorted_items[ $i ]->ID,
685+
'menu_order' => $i + 2,
686+
];
688687
}
689688
} else {
690689
// Moving down: items at 0-indexed [old_pos, new_pos-1] shift up by -1.
691690
for ( $i = $old_position_normalized; $i <= $new_position - 1; $i++ ) {
692-
wp_update_post(
693-
[
694-
'ID' => $sorted_items[ $i ]->ID,
695-
'menu_order' => $i,
696-
]
697-
);
691+
$pending_menu_order_updates[] = [
692+
'ID' => $sorted_items[ $i ]->ID,
693+
'menu_order' => $i,
694+
];
698695
}
699696
}
700697
}
@@ -712,6 +709,10 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
712709
WP_CLI::error( "Couldn't update menu item." );
713710
}
714711
} else {
712+
// Apply deferred reordering of other menu items only after a successful update.
713+
foreach ( $pending_menu_order_updates as $update_args ) {
714+
wp_update_post( $update_args );
715+
}
715716

716717
if ( ( 'add' === $method ) && $menu_item_args['menu-item-position'] ) {
717718
$this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'], +1, $result );

0 commit comments

Comments
 (0)