WordPress / wordpress-importer

The WordPress Importer
https://wordpress.org/plugins/wordpress-importer/
GNU General Public License v2.0
78 stars 76 forks source link

Importing a WXR with a menu item that references a taxonomy that doesn't exist, causes a fatal #164

Open dd32 opened 1 month ago

dd32 commented 1 month ago

This bug is in WordPress core, but I think it can only be triggered through an import, so I'm filing the initial details here. This might be resolved via Core, or maybe it should be handled in the Importer.

Reported in the forums: https://wordpress.org/support/topic/uncaught-error-object-of-class-wp_error-could-not-be-converted-to-string-7/

Duplicated in Playground

This could also be triggered by using a plugin that adds a taxonomy, add that taxonomy to a menu, and then export/import into a new site without that plugin.

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

Example WXR that triggers this mywordpresswebsite.WordPress.2024-08-01.xml.zip which can be uploaded at this playground instance and you'll then see the error in the browser console.

The Core lines that triggers it: https://github.com/WordPress/wordpress-develop/blob/50af37a9083f003f8e98d089091d2cc428797cc5/src/wp-includes/nav-menu.php#L496

if ( 'taxonomy' === $args['menu-item-type'] ) {
    $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
}
....
if ( wp_unslash( $args['menu-item-title'] ) === wp_specialchars_decode( $original_title ) ) {

$original_title is a WP_Error as it's an unknown taxonomy in $args['menu-item-object'].

Core could avoid this as it does elsewhere in that code, or the Importer could not import unknown taxonomy items.

dd32 commented 1 month ago

It appears this may have been introduced in Core via https://core.trac.wordpress.org/changeset/48416 / https://core.trac.wordpress.org/ticket/48011 / WP 5.5, although I can't confirm if the error would've simply been something else before then (As it would have called wp_update_post( [ 'post_parent' => WP_Error, ...)

dd32 commented 1 month ago

Filed upstream in https://core.trac.wordpress.org/ticket/61799