diff --git a/wp-admin/nav-menus.php b/wp-admin/nav-menus.php index 24fc03a31b..a4ef4d5c36 100644 --- a/wp-admin/nav-menus.php +++ b/wp-admin/nav-menus.php @@ -60,18 +60,24 @@ _wp_expand_nav_menu_post_data(); switch ( $action ) { case 'add-menu-item': check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' ); + if ( isset( $_REQUEST['nav-menu-locations'] ) ) { set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) ); } elseif ( isset( $_REQUEST['menu-item'] ) ) { wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] ); } + break; + case 'move-down-menu-item': // Moving down a menu item is the same as moving up the next in order. check_admin_referer( 'move-menu_item' ); + $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; + if ( is_nav_menu_item( $menu_item_id ) ) { $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { $menu_id = (int) $menus[0]; $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); @@ -80,6 +86,7 @@ switch ( $action ) { // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { @@ -90,19 +97,14 @@ switch ( $action ) { } // Get next in order. - if ( - isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) - ) { + if ( isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) ) { $next_item_id = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ]; $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) ); // If not siblings of same parent, bubble menu item up but keep order. - if ( - ! empty( $menu_item_data['menu_item_parent'] ) && - ( - empty( $next_item_data['menu_item_parent'] ) || - $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent'] - ) + if ( ! empty( $menu_item_data['menu_item_parent'] ) + && ( empty( $next_item_data['menu_item_parent'] ) + || (int) $next_item_data['menu_item_parent'] !== (int) $menu_item_data['menu_item_parent'] ) ) { if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $parent_db_id = (int) $menu_item_data['menu_item_parent']; @@ -116,7 +118,6 @@ switch ( $action ) { $parent_data = (array) $parent_object; $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); - } // Make menu item a child of its next sibling. @@ -132,9 +133,8 @@ switch ( $action ) { } // The item is last but still has a parent, so bubble up. - } elseif ( - ! empty( $menu_item_data['menu_item_parent'] ) && - in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) + } elseif ( ! empty( $menu_item_data['menu_item_parent'] ) + && in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true ); update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); @@ -143,11 +143,19 @@ switch ( $action ) { } break; + case 'move-up-menu-item': check_admin_referer( 'move-menu_item' ); + $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; + if ( is_nav_menu_item( $menu_item_id ) ) { - $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + if ( isset( $_REQUEST['menu'] ) ) { + $menus = array( (int) $_REQUEST['menu'] ); + } else { + $menus = wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + } + if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { $menu_id = (int) $menus[0]; $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); @@ -156,6 +164,7 @@ switch ( $action ) { // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { @@ -166,14 +175,15 @@ switch ( $action ) { } // If this menu item is not first. - if ( ! empty( $dbids_to_orders[ $menu_item_id ] ) && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ) { + if ( ! empty( $dbids_to_orders[ $menu_item_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + ) { // If this menu item is a child of the previous. - if ( - ! empty( $menu_item_data['menu_item_parent'] ) && - in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) && - isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) && - ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + if ( ! empty( $menu_item_data['menu_item_parent'] ) + && in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) + && isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + && ( (int) $menu_item_data['menu_item_parent'] === $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ) { if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $parent_db_id = (int) $menu_item_data['menu_item_parent']; @@ -190,10 +200,9 @@ switch ( $action ) { * If there is something before the parent and parent a child of it, * make menu item a child also of it. */ - if ( - ! empty( $dbids_to_orders[ $parent_db_id ] ) && - ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) && - ! empty( $parent_data['menu_item_parent'] ) + if ( ! empty( $dbids_to_orders[ $parent_db_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) + && ! empty( $parent_data['menu_item_parent'] ) ) { $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; @@ -201,11 +210,11 @@ switch ( $action ) { * Else if there is something before parent and parent not a child of it, * make menu item a child of that something's parent */ - } elseif ( - ! empty( $dbids_to_orders[ $parent_db_id ] ) && - ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) + } elseif ( ! empty( $dbids_to_orders[ $parent_db_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) ) { $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true ); + if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ), true ) ) { $menu_item_data['menu_item_parent'] = $_possible_parent_id; } else { @@ -230,12 +239,11 @@ switch ( $action ) { } // Else this menu item is not a child of the previous. - } elseif ( - empty( $menu_item_data['menu_order'] ) || - empty( $menu_item_data['menu_item_parent'] ) || - ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) || - empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) || - $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] != $menu_item_data['menu_item_parent'] + } elseif ( empty( $menu_item_data['menu_order'] ) + || empty( $menu_item_data['menu_item_parent'] ) + || ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) + || empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + || $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] !== (int) $menu_item_data['menu_item_parent'] ) { // Just make it a child of the previous; keep the order. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ]; @@ -245,6 +253,7 @@ switch ( $action ) { } } } + break; case 'delete-menu-item': @@ -255,10 +264,12 @@ switch ( $action ) { if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) { $messages[] = '
' . __( 'The menu item has been successfully deleted.' ) . '
' . __( 'The menu has been successfully deleted.' ) . '
' . $deletion->get_error_message() . '
' . __( 'Selected menus have been successfully deleted.' ) . '
' . __( 'Please enter a valid menu name.' ) . '
' . $_nav_menu_selected_id->get_error_message() . '
' . __( 'Menu locations updated.' ) . '
' . sprintf( @@ -716,7 +750,8 @@ require_once ABSPATH . 'wp-admin/admin-header.php'; term_id; + $selected = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] === $menu->term_id; + if ( $selected ) { $data_orig = 'data-orig="true"'; } @@ -727,7 +762,7 @@ require_once ABSPATH . 'wp-admin/admin-header.php';