diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index a063835fda341..d808c4e212d39 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -491,17 +491,25 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item $args['menu-item-url'] = ''; $original_title = ''; + if ( 'taxonomy' === $args['menu-item-type'] ) { - $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); - $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); - } elseif ( 'post_type' === $args['menu-item-type'] ) { + $original_object = get_term( $args['menu-item-object-id'], $args['menu-item-object'] ); + if ( $original_object instanceof WP_Term ) { + $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); + $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); + } + } elseif ( 'post_type' === $args['menu-item-type'] ) { $original_object = get_post( $args['menu-item-object-id'] ); - $original_parent = (int) $original_object->post_parent; - $original_title = $original_object->post_title; + + if ( $original_object instanceof WP_Post ) { + $original_parent = (int) $original_object->post_parent; + $original_title = $original_object->post_title; + } } elseif ( 'post_type_archive' === $args['menu-item-type'] ) { $original_object = get_post_type_object( $args['menu-item-object'] ); - if ( $original_object ) { + + if ( $original_object instanceof WP_Post_Type ) { $original_title = $original_object->labels->archives; } } diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index 7a320f9f8d628..d4ece1ff1776c 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -1208,6 +1208,35 @@ public function test_wp_update_nav_menu_item_with_special_characters_in_category $this->assertEmpty( $category_item->post_title ); } + /** + * Tests `wp_update_nav_menu_item()` with a non-existing taxonomy. + * + * When inserting a term from a non-existing taxonomy as a nav item, + * the `post_title` property should be empty, and the function + * should not throw a fatal error for `wp_specialchars_decode()`. + * + * @ticket 61799 + */ + public function test_wp_update_nav_menu_item_with_invalid_taxonomy() { + register_taxonomy( 'invalid', 'post' ); + $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'invalid' ) ); + unregister_taxonomy( 'invalid' ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'taxonomy', + 'menu-item-object' => 'invalid', + 'menu-item-object-id' => $term->term_id, + 'menu-item-status' => 'publish', + ) + ); + + $menu_item = get_post( $menu_item_id ); + $this->assertEmpty( $menu_item->post_title ); + } + /** * Test passed post_date/post_date_gmt. *