Make WordPress Core

Opened 19 months ago

Closed 19 months ago

Last modified 19 months 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
19 months 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
19 months 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.


19 months ago
#3

  • Keywords has-patch added

#4 in reply to: ↑ 2 @dd32
19 months 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 an empty string, and has done so since [5726] it's not worth changing IMHO.

Version 0, edited 19 months ago by dd32 (next)

#5 @SergeyBiryukov
19 months ago

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

#6 @SergeyBiryukov
19 months 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:


19 months ago
#7

Thanks for the PR! Merged in r58854.

Note: See TracTickets for help on using tickets.