Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#66177 new defect (bug)

Check that menu items are objects in _wp_menu_item_classes_by_context()

Reported by: josephscott Owned by:
Priority: normal Milestone: Awaiting Review
Component: Menus Version:
Severity: normal Keywords: has-patch has-unit-tests 2nd-opinion
Cc: Focuses:

Description

wp_nav_menu() passes the result of wp_get_nav_menu_items() to _wp_menu_item_classes_by_context() ( ​GH ), which assigns properties on every item without checking that the item is an object ( ​GH ). The items come through the wp_get_nav_menu_items filter, and in the normal case they are all WP_Post objects.

A filter that leaves a null in that array causes a PHP fatal error:

Fatal error: Uncaught Error: Attempt to assign property "current" on null ...

WordPress should check that each menu item is an object before assigning properties to it.

I have a PR to address this.

Change History (4)

This ticket was mentioned in ​PR #13721 on ​WordPress/wordpress-develop by ​@josephscott.


7 weeks ago
#1

  • Keywords has-patch has-unit-tests added

https://core.trac.wordpress.org/ticket/66177

AI assistance: Yes
Tool(s): Claude Code
Model(s): Fable 5.1
Used for: Review of the problem and solution. Claude wrote the test.

#2 @westonruter
7 weeks ago

  • Keywords 2nd-opinion added

I'm not sure we should do this. The filter is specifically documented as taking an array of objects:

	/**
	 * Filters the navigation menu items being returned.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $items An array of menu item post objects.
	 * @param object $menu  The menu object.
	 * @param array  $args  An array of arguments used to retrieve menu item objects.
	 */
	return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );

(Aside: It should be WP_Post[] not array.)

If a plugin is supplying anything other than an array of post objects, then they are doing it wrong. This an issue everywhere in core. We can't fix up every filter callback and filter return value one-by-one to ensure it has the expected shape as is done in ​your PR. It's not scalable.

This is what #51525 is supposed to address holistically.

#3 @therssoftware
7 weeks ago

### Test Report

Tested: ​PR 13721
Environment:

  • WordPress: 7.2-alpha
  • PHP: 8.3
  • Web Server: Nginx / CLI

#### Testing Steps & Reproduction:

  1. Hooked into wp_get_nav_menu_items to set a menu item to null (simulating menu permission/visibility filters).
  2. On trunk before patch: Render of wp_nav_menu() halts with fatal error Error: Attempt to assign property "current" on null in wp-includes/nav-menu-template.php:399.
  3. Applied PR 13721:
    • The fatal error is resolved; non-object entries are safely stripped from $menu_items.
    • The menu renders cleanly with the remaining valid items.
    • Executed PHPUnit test suite: test_wp_nav_menu_should_skip_non_object_menu_items passes cleanly (OK (1 test, 2 assertions)).
    • Ran entire --group menu test suite: 106 tests pass with 0 regressions (OK (106 tests, 238 assertions)).
    • Verified PHPCS against WordPress Coding Standards: 0 errors, 0 warnings.

PR 13721 works as expected and fixes the defect.

​@therssoftware commented on ​PR #13721:


7 weeks ago
#4

Tested and Verified ✅

Environment:

  • WordPress: 7.2-alpha (trunk)
  • PHP: 8.3

Reproduction & Verification:

  1. Before patch (trunk): Hooked into wp_get_nav_menu_items and returned an array with a null item: `php add_filter( 'wp_get_nav_menu_items', function ( $items ) {

if ( ! empty( $items ) ) {

$items[0] = null;

}
return $items;

} );

Calling wp_nav_menu() fails with fatal error:
Fatal error: Uncaught Error: Attempt to assign property "current" on null in src/wp-includes/nav-menu-template.php:399

  1. After applying PR #13721:

The fatal error is eliminated; non-object entries are safely stripped from $menu_items.
The navigation menu renders as expected with the remaining valid items intact.
Ran vendor/bin/phpunit --filter test_wp_nav_menu_should_skip_non_object_menu_items: Pass (OK (1 test, 2 assertions)).
Ran full menu test suite vendor/bin/phpunit --group menu: Pass (OK (106 tests, 238 assertions)).
PHPCS check: 0 errors, 0 warnings.

Note: See TracTickets for help on using tickets.