From 460c358859241d957bd3ef6748f9090a3c2b01ee Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 20 Dec 2016 22:04:22 +0000 Subject: [PATCH] Customize: Fix visible edit shortcuts for `wp_nav_menu()` instances using the `menu` arg (such as in the Custom Menu widget) instead of the `theme_location` arg. Also fix logic for `focus-control-for-setting` handler to focus on the first control (lowest `priority` value) associated with a given setting instead of the last control encountered when iterating over all controls, as this ensures the first control in a `nav_menu` section is focused rather than the last one. Props westonruter, sirbrillig. See #27403. Fixes #39101. git-svn-id: https://develop.svn.wordpress.org/trunk@39622 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/customize-controls.js | 12 ++++++++---- src/wp-includes/js/customize-selective-refresh.js | 9 +++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index 9f385a003b..e6270ee0e6 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -5365,16 +5365,20 @@ // Focus on the control that is associated with the given setting. api.previewer.bind( 'focus-control-for-setting', function( settingId ) { - var matchedControl; + var matchedControls = []; api.control.each( function( control ) { var settingIds = _.pluck( control.settings, 'id' ); if ( -1 !== _.indexOf( settingIds, settingId ) ) { - matchedControl = control; + matchedControls.push( control ); } } ); - if ( matchedControl ) { - matchedControl.focus(); + // Focus on the matched control with the lowest priority (appearing higher). + if ( matchedControls.length ) { + matchedControls.sort( function( a, b ) { + return a.priority() - b.priority(); + } ); + matchedControls[0].focus(); } } ); diff --git a/src/wp-includes/js/customize-selective-refresh.js b/src/wp-includes/js/customize-selective-refresh.js index 0735b237c8..d3b81db6d7 100644 --- a/src/wp-includes/js/customize-selective-refresh.js +++ b/src/wp-includes/js/customize-selective-refresh.js @@ -312,14 +312,15 @@ wp.customize.selectiveRefresh = ( function( $, api ) { * @since 4.5.0 */ showControl: function() { - var partial = this, settingId = partial.params.primarySetting, menuSlug; + var partial = this, settingId = partial.params.primarySetting; if ( ! settingId ) { settingId = _.first( partial.settings() ); } if ( partial.getType() === 'nav_menu' ) { - menuSlug = partial.params.navMenuArgs.theme_location; - if ( menuSlug ) { - settingId = 'nav_menu_locations[' + menuSlug + ']'; + if ( partial.params.navMenuArgs.theme_location ) { + settingId = 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']'; + } else if ( partial.params.navMenuArgs.menu ) { + settingId = 'nav_menu[' + String( partial.params.navMenuArgs.menu ) + ']'; } } api.preview.send( 'focus-control-for-setting', settingId );