Skip to content

Commit

Permalink
Menus: Check if taxonomy term exists in wp_update_nav_menu_item().
Browse files Browse the repository at this point in the history
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()`.

Includes bringing some consistency to similar checks for post types and post type archives in the same code fragment.

Follow-up to [14283], [14450], [35382], [36095].

Props dd32, narenin, mukesh27, SergeyBiryukov.
Fixes #61799.

git-svn-id: https://develop.svn.wordpress.org/trunk@58854 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Aug 5, 2024
1 parent b17b5d1 commit 8e7035f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/wp-includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/tests/post/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 8e7035f

Please sign in to comment.