WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#37293 closed defect (bug) (fixed)

Customizer nav menu section in PHP is unable to represent placeholders

Reported by: westonruter Owned by: westonruter
Milestone: 4.6 Priority: normal
Severity: normal Version: 4.3
Component: Customize Keywords: has-patch commit
Focuses: Cc:
PR Number:

Description

When loading the Customizer with a pre-populated state containing a placeholder (yet-to-be-saved) nav menu, a JS error is thrown when the section is attempted to be expanded:

Uncaught TypeError: Cannot read property 'reflowMenuItems' of undefined

The code that throws the error is:

api.control( 'nav_menu[' + String( section.params.menu_id ) + ']' ).reflowMenuItems();

The problem here is that menu_id is not getting set properly in WP_Customize_Nav_Menu_Section::json() when the menu ID is a placeholder (like -9349073479):

$exported['menu_id'] = intval( preg_replace( '/^nav_menu\[(\d+)\]/', '$1', $this->id ) );

Since the pattern doesn't match, the menu_id gets set to 0 which then causes a JS error.

The fix is simple: to allow the number to be negative:

<?php
$exported['menu_id'] = intval( preg_replace( '/^nav_menu\[(\d+)\]/', '$1', $this->id ) );

Attachments (1)

37293.0.diff (677 bytes) - added by westonruter 4 years ago.

Download all attachments as: .zip

Change History (4)

#1 @westonruter
4 years ago

  • Summary changed from Customizer nav menu section is not compatible with to Customizer nav menu section in PHP is unable to represent placeholders

@westonruter
4 years ago

#2 @westonruter
4 years ago

  • Keywords has-patch commit added
  • Owner set to westonruter
  • Status changed from new to accepted

#3 @westonruter
4 years ago

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

In 37981:

Customize: Ensure that WP_Customize_Nav_Menu_Section is able to represent a placeholder nav menu.

Placeholder nav menus (ones that have yet to be saved into the DB) have negative integer IDs.

Fixes #37293.
Props valendesigns, stubgo, westonruter.

Note: See TracTickets for help on using tickets.