Make WordPress Core

Opened 7 weeks ago

Closed 6 weeks ago

Last modified 6 weeks ago

#61799 closed defect (bug) (fixed)

Calling wp_update_nav_menu_item() with an invalid taxonomy causes a fatal error

Reported by: dd32's profile dd32 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.7 Priority: normal
Severity: normal Version: 5.5
Component: Import Keywords: has-patch
Focuses: Cc:

Description

For background, please see https://github.com/WordPress/wordpress-importer/issues/164

When attempting to import a WXR which contains a Nav menu item that references a taxonomy that doesn't exist on the site, wp_update_nav_menu_item() will hit a fatal error.

PHP Fatal error:  Uncaught Error: Object of class WP_Error could not be converted to string in /wordpress/wp-includes/formatting.php:2
Stack trace:
#0 /wordpress/wp-includes/nav-menu.php(2): wp_specialchars_decode(Object(WP_Error))
#1 /wordpress/wp-content/plugins/wordpress-importer/class-wp-import.php(996): wp_update_nav_menu_item(3, 0, Array)
#2 /wordpress/wp-content/plugins/wordpress-importer/class-wp-import.php(653): WP_Import->process_menu_item(Array)
#3 /wordpress/wp-content/plugins/wordpress-importer/class-wp-import.php(89): WP_Import->process_posts()
#4 /wordpress/wp-content/plugins/wordpress-importer/class-wp-import.php(65): WP_Import->import('/wordpress/wp-c...')
#5 /wordpress/wp-admin/admin.php(2): WP_Import->dispatch()
#6 {main}
  thrown in /wordpress/wp-includes/formatting.php on line 2

It appears that invalid post type archives are handled, just not missing taxonomies.

Relevant lines:
https://core.trac.wordpress.org/browser/tags/6.6/src/wp-includes/nav-menu.php?marks=496,509#L484
$original_title will be a WP_Error for a non-existent taxonomy in $args['menu-item-object'].

Change History (7)

#1 @dd32
7 weeks ago

Untested, but it might be enough to simply avoid the fatal on update and leave the menu code to resolve the issue elsewhere:

  • nav-menu.php

     
    491491                $args['menu-item-url'] = '';
    492492
    493493                $original_title = '';
    494                 if ( 'taxonomy' === $args['menu-item-type'] ) {
     494                if ( 'taxonomy' === $args['menu-item-type'] && taxonomy_exists( $args['menu-item-object'] ) ) {
    495495                        $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
    496496                        $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
    497497                } elseif ( 'post_type' === $args['menu-item-type'] ) {

#2 follow-up: @mukesh27
7 weeks ago

The changes you suggest works but the get_term_field return $term here https://github.com/WordPress/wordpress-develop/blob/6.6/src/wp-includes/taxonomy.php#L1221-L1236, If it return error then it should return null instead of $term

what do you think?

This ticket was mentioned in PR #7121 on WordPress/wordpress-develop by @narenin.


7 weeks ago
#3

  • Keywords has-patch added

#4 in reply to: ↑ 2 @dd32
7 weeks ago

Replying to mukesh27:

If it return error then it should return null instead of $term

It is 'weird' that it returns an empty string in other cases of error, but not this one.
But given it's documented as returning a WP_Error, and has done so since [5726] it's not worth changing IMHO.

Last edited 7 weeks ago by dd32 (previous) (diff)

#5 @SergeyBiryukov
7 weeks ago

  • Milestone changed from Awaiting Review to 6.7
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#6 @SergeyBiryukov
6 weeks ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 58854:

Menus: Check if taxonomy term exists in wp_update_nav_menu_item().

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.

@SergeyBiryukov commented on PR #7121:


6 weeks ago
#7

Thanks for the PR! Merged in r58854.

Note: See TracTickets for help on using tickets.