mirror of
https://github.com/WordPress/WordPress.git
synced 2024-10-31 15:59:44 +01:00
Menus: Reset menu_item_parent
to 0 when the parent is set to the item itself.
Props: peterwilsoncc, SergeyBiryukov, azaozz. Fixes #57169. Built from https://develop.svn.wordpress.org/trunk@54973 git-svn-id: http://core.svn.wordpress.org/trunk@54525 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
95c797463d
commit
599201eca7
@ -117,6 +117,10 @@ switch ( $action ) {
|
||||
if ( ! is_wp_error( $parent_object ) ) {
|
||||
$parent_data = (array) $parent_object;
|
||||
$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
|
||||
|
||||
// Reset invalid `menu_item_parent`.
|
||||
$menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
|
||||
|
||||
update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
|
||||
}
|
||||
|
||||
@ -126,6 +130,10 @@ switch ( $action ) {
|
||||
$menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
|
||||
|
||||
$menu_item_data['menu_item_parent'] = $next_item_data['ID'];
|
||||
|
||||
// Reset invalid `menu_item_parent`.
|
||||
$menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
|
||||
|
||||
update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
|
||||
|
||||
wp_update_post( $menu_item_data );
|
||||
@ -137,6 +145,10 @@ switch ( $action ) {
|
||||
&& 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 );
|
||||
|
||||
// Reset invalid `menu_item_parent`.
|
||||
$menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
|
||||
|
||||
update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
|
||||
}
|
||||
}
|
||||
@ -247,6 +259,10 @@ switch ( $action ) {
|
||||
) {
|
||||
// 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 ];
|
||||
|
||||
// Reset invalid `menu_item_parent`.
|
||||
$menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
|
||||
|
||||
update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
|
||||
wp_update_post( $menu_item_data );
|
||||
}
|
||||
|
@ -199,6 +199,11 @@ function wp_nav_menu( $args = array() ) {
|
||||
$menu_items_tree = array();
|
||||
$menu_items_with_children = array();
|
||||
foreach ( (array) $menu_items as $menu_item ) {
|
||||
// Fix invalid `menu_item_parent`. See: https://core.trac.wordpress.org/ticket/56926.
|
||||
if ( (int) $menu_item->ID === (int) $menu_item->menu_item_parent ) {
|
||||
$menu_item->menu_item_parent = 0;
|
||||
}
|
||||
|
||||
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
|
||||
$menu_items_tree[ $menu_item->ID ] = $menu_item->menu_item_parent;
|
||||
if ( $menu_item->menu_item_parent ) {
|
||||
|
@ -563,6 +563,11 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
|
||||
$menu_item_db_id = (int) $menu_item_db_id;
|
||||
|
||||
// Reset invalid `menu_item_parent`.
|
||||
if ( (int) $args['menu-item-parent-id'] === $menu_item_db_id ) {
|
||||
$args['menu-item-parent-id'] = 0;
|
||||
}
|
||||
|
||||
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', (string) ( (int) $args['menu-item-parent-id'] ) );
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_object_id', (string) ( (int) $args['menu-item-object-id'] ) );
|
||||
@ -1273,3 +1278,31 @@ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locat
|
||||
|
||||
return $new_nav_menu_locations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents menu items from being their own parent.
|
||||
*
|
||||
* Resets menu_item_parent to 0 when the parent is set to the item itself.
|
||||
* For use before saving `_menu_item_menu_item_parent` in nav-menus.php.
|
||||
*
|
||||
* @since 6.1.2
|
||||
* @access private
|
||||
*
|
||||
* @param array $menu_item_data The menu item data array.
|
||||
* @return array The menu item data with reset menu_item_parent.
|
||||
*/
|
||||
function _wp_reset_invalid_menu_item_parent( $menu_item_data ) {
|
||||
if ( ! is_array( $menu_item_data ) ) {
|
||||
return $menu_item_data;
|
||||
}
|
||||
|
||||
if (
|
||||
! empty( $menu_item_data['ID'] ) &&
|
||||
! empty( $menu_item_data['menu_item_parent'] ) &&
|
||||
(int) $menu_item_data['ID'] === (int) $menu_item_data['menu_item_parent']
|
||||
) {
|
||||
$menu_item_data['menu_item_parent'] = 0;
|
||||
}
|
||||
|
||||
return $menu_item_data;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.2-alpha-54972';
|
||||
$wp_version = '6.2-alpha-54973';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
Reference in New Issue
Block a user