<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36889] trunk: Customize: Fix regressions and harden implementation of selective refresh for nav menus.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36889">36889</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36889","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-03-09 00:08:51 +0000 (Wed, 09 Mar 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Fix regressions and harden implementation of selective refresh for nav menus.

* Request full refresh if there are nav menu instances that lack partials for a changed setting.
* Restore `WP_Customize_Nav_Menus::$preview_nav_menu_instance_args` and `WP_Customize_Nav_Menus::export_preview_data()` from 4.3, and keeping a tally of all `wp_nav_menu()` calls regardless of whether they can use selective refresh.
* Ensure that all instances of `wp_nav_menu()` are tallied, regardless of whether they are made during the initial preview call or during subsequent partial renderings. Export `nav_menu_instance_args` with each partial rendering response just as they are returned when rendering the preview as a whole.
* Fix issues with Custom Menu widget where nav menu items would fail to render when switching between menus when a menu lacked items to begin with.
* Make sure the fallback behavior is invoked when the partial is no longer associated with a menu.
* Do fallback behavior to refresh preview when all menu items are removed from a menu.

Follows <a href="https://core.trac.wordpress.org/changeset/36586">[36586]</a>.
See <a href="https://core.trac.wordpress.org/ticket/27355">#27355</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/35362">#35362</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizenavmenusphp">trunk/src/wp-includes/class-wp-customize-nav-menus.php</a></li>
<li><a href="#trunksrcwpincludesjscustomizepreviewnavmenusjs">trunk/src/wp-includes/js/customize-preview-nav-menus.js</a></li>
<li><a href="#trunktestsphpunittestscustomizenavmenusphp">trunk/tests/phpunit/tests/customize/nav-menus.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-nav-menus.php    2016-03-08 18:24:50 UTC (rev 36888)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php      2016-03-09 00:08:51 UTC (rev 36889)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -822,6 +822,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        //
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Nav menu args used for each instance, keyed by the args HMAC.
+        *
+        * @since 4.3.0
+        * @access public
+        * @var array
+        */
+       public $preview_nav_menu_instance_args = array();
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Filter arguments for dynamic nav_menu selective refresh partials.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.5.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -862,6 +871,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'wp_nav_menu_args', array( $this, 'filter_wp_nav_menu_args' ), 1000 );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'wp_nav_menu', array( $this, 'filter_wp_nav_menu' ), 10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 );
+               add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -881,7 +892,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * wp_nav_menu() can use selective refreshed. A wp_nav_menu() can be
</span><span class="cx" style="display: block; padding: 0 10px">                 * selective refreshed if...
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $can_selective_refresh = (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $can_partial_refresh = (
</ins><span class="cx" style="display: block; padding: 0 10px">                         // ...if wp_nav_menu() is directly echoing out the menu (and thus isn't manipulating the string after generated),
</span><span class="cx" style="display: block; padding: 0 10px">                        ! empty( $args['echo'] )
</span><span class="cx" style="display: block; padding: 0 10px">                        &&
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -904,13 +915,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                ( isset( $args['items_wrap'] ) && '<' === substr( $args['items_wrap'], 0, 1 ) )
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $args['can_partial_refresh'] = $can_partial_refresh;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $can_selective_refresh ) {
-                       return $args;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $exported_args = $args;
+
+               // Empty out args which may not be JSON-serializable.
+               if ( ! $can_partial_refresh ) {
+                       $exported_args['fallback_cb'] = '';
+                       $exported_args['walker'] = '';
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $exported_args = $args;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * Replace object menu arg with a term_id menu arg, as this exports better
</span><span class="cx" style="display: block; padding: 0 10px">                 * to JS and is easier to compare hashes.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -923,7 +937,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $exported_args['args_hmac'] = $this->hash_nav_menu_args( $exported_args );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $args['customize_preview_nav_menus_args'] = $exported_args;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->preview_nav_menu_instance_args[ $exported_args['args_hmac'] ] = $exported_args;
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $args;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -942,7 +956,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return null
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function filter_wp_nav_menu( $nav_menu_content, $args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $args->customize_preview_nav_menus_args ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        $attributes .= ' data-customize-partial-type="nav_menu_instance"';
</span><span class="cx" style="display: block; padding: 0 10px">                        $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -987,14 +1001,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Exports data from PHP to JS.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.3.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @deprecated 4.5.0 Obsolete
</del><span class="cx" style="display: block; padding: 0 10px">          * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function export_preview_data() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                _deprecated_function( __METHOD__, '4.5.0' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Why not wp_localize_script? Because we're not localizing, and it forces values into strings.
+               $exports = array(
+                       'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args,
+               );
+               printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Export any wp_nav_menu() calls during the rendering of any partials.
+        *
+        * @since 4.5.0
+        * @access public
+        *
+        * @param array $response Response.
+        * @return array Response.
+        */
+       public function export_partial_rendered_nav_menu_instances( $response ) {
+               $response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args;
+               return $response;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Render a specific menu via wp_nav_menu() using the supplied arguments.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.3.0
</span></span></pre></div>
<a id="trunksrcwpincludesjscustomizepreviewnavmenusjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/js/customize-preview-nav-menus.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/customize-preview-nav-menus.js   2016-03-08 18:24:50 UTC (rev 36888)
+++ trunk/src/wp-includes/js/customize-preview-nav-menus.js     2016-03-09 00:08:51 UTC (rev 36889)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,7 +1,15 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* global _wpCustomizePreviewNavMenusExports */
</ins><span class="cx" style="display: block; padding: 0 10px"> wp.customize.navMenusPreview = wp.customize.MenusCustomizerPreview = ( function( $, _, wp, api ) {
</span><span class="cx" style="display: block; padding: 0 10px">        'use strict';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        var self = {};
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ var self = {
+               data: {
+                       navMenuInstanceArgs: {}
+               }
+       };
+       if ( 'undefined' !== typeof _wpCustomizePreviewNavMenusExports ) {
+               _.extend( self.data, _wpCustomizePreviewNavMenusExports );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Initialize nav menus preview.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,7 +18,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                var self = this;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( api.selectiveRefresh ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self.watchNavMenuLocationChanges();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Listen for changes to settings related to nav menus.
+                       api.each( function( setting ) {
+                               self.bindSettingListener( setting );
+                       } );
+                       api.bind( 'add', function( setting ) {
+                               self.bindSettingListener( setting, { fire: true } );
+                       } );
+                       api.bind( 'remove', function( setting ) {
+                               self.unbindSettingListener( setting );
+                       } );
+
+                       /*
+                        * Ensure that wp_nav_menu() instances nested inside of other partials
+                        * will be recognized as being present on the page.
+                        */
+                       api.selectiveRefresh.bind( 'render-partials-response', function( response ) {
+                               if ( response.nav_menu_instance_args ) {
+                                       _.extend( self.data.navMenuInstanceArgs, response.nav_menu_instance_args );
+                               }
+                       } );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                api.preview.bind( 'active', function() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,6 +155,31 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        },
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         * Make sure that partial fallback behavior is invoked if there is no associated menu.
+                        *
+                        * @since 4.5.0
+                        *
+                        * @returns {Promise}
+                        */
+                       refresh: function() {
+                               var partial = this, menuId, deferred = $.Deferred();
+
+                               // Make sure the fallback behavior is invoked when the partial is no longer associated with a menu.
+                               if ( _.isNumber( partial.params.navMenuArgs.menu ) ) {
+                                       menuId = partial.params.navMenuArgs.menu;
+                               } else if ( partial.params.navMenuArgs.theme_location && api.has( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' ) ) {
+                                       menuId = api( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' ).get();
+                               }
+                               if ( ! menuId ) {
+                                       partial.fallback();
+                                       deferred.reject();
+                                       return deferred.promise();
+                               }
+
+                               return api.selectiveRefresh.Partial.prototype.refresh.call( partial );
+                       },
+
+                       /**
</ins><span class="cx" style="display: block; padding: 0 10px">                          * Render content.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="cx" style="display: block; padding: 0 10px">                         * @inheritdoc
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -135,6 +187,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><span class="cx" style="display: block; padding: 0 10px">                        renderContent: function( placement ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                var partial = this, previousContainer = placement.container;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Do fallback behavior to refresh preview if menu is now empty.
+                               if ( '' === placement.addedContent ) {
+                                       placement.partial.fallback();
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( api.selectiveRefresh.Partial.prototype.renderContent.call( partial, placement ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        // Trigger deprecated event.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -152,33 +210,148 @@
</span><span class="cx" style="display: block; padding: 0 10px">                api.selectiveRefresh.partialConstructor.nav_menu_instance = self.NavMenuInstancePartial;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * Watch for changes to nav_menu_locations[] settings.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Request full refresh if there are nav menu instances that lack partials which also match the supplied args.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * Refresh partials associated with the given nav_menu_locations[] setting,
-                * or request an entire preview refresh if there are no containers in the
-                * document for a partial associated with the theme location.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param {object} navMenuInstanceArgs
+                */
+               self.handleUnplacedNavMenuInstances = function( navMenuInstanceArgs ) {
+                       var unplacedNavMenuInstances;
+                       unplacedNavMenuInstances = _.filter( _.values( self.data.navMenuInstanceArgs ), function( args ) {
+                               return ! api.selectiveRefresh.partial.has( 'nav_menu_instance[' + args.args_hmac + ']' );
+                       } );
+                       if ( _.findWhere( unplacedNavMenuInstances, navMenuInstanceArgs ) ) {
+                               api.selectiveRefresh.requestFullRefresh();
+                               return true;
+                       }
+                       return false;
+               };
+
+               /**
+                * Add change listener for a nav_menu[], nav_menu_item[], or nav_menu_locations[] setting.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 4.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 *
+                * @param {wp.customize.Value} setting
+                * @param {object}             [options]
+                * @param {boolean}            options.fire Whether to invoke the callback after binding.
+                *                                          This is used when a dynamic setting is added.
+                * @return {boolean} Whether the setting was bound.
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self.watchNavMenuLocationChanges = function() {
-                       api.bind( 'change', function( setting ) {
-                               var themeLocation, themeLocationPartialFound = false, matches = setting.id.match( /^nav_menu_locations\[(.+)]$/ );
-                               if ( ! matches ) {
-                                       return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self.bindSettingListener = function( setting, options ) {
+                       var matches;
+                       options = options || {};
+
+                       matches = setting.id.match( /^nav_menu\[(-?\d+)]$/ );
+                       if ( matches ) {
+                               setting._navMenuId = parseInt( matches[1], 10 );
+                               setting.bind( this.onChangeNavMenuSetting );
+                               if ( options.fire ) {
+                                       this.onChangeNavMenuSetting.call( setting, setting(), false );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                themeLocation = matches[1];
-                               api.selectiveRefresh.partial.each( function( partial ) {
-                                       if ( partial.extended( self.NavMenuInstancePartial ) && partial.params.navMenuArgs.theme_location === themeLocation ) {
-                                               partial.refresh();
-                                               themeLocationPartialFound = true;
-                                       }
-                               } );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         return true;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( ! themeLocationPartialFound ) {
-                                       api.selectiveRefresh.requestFullRefresh();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 matches = setting.id.match( /^nav_menu_item\[(-?\d+)]$/ );
+                       if ( matches ) {
+                               setting._navMenuItemId = parseInt( matches[1], 10 );
+                               setting.bind( this.onChangeNavMenuItemSetting );
+                               if ( options.fire ) {
+                                       this.onChangeNavMenuItemSetting.call( setting, setting(), false );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                return true;
+                       }
+
+                       matches = setting.id.match( /^nav_menu_locations\[(.+?)]/ );
+                       if ( matches ) {
+                               setting._navMenuThemeLocation = matches[1];
+                               setting.bind( this.onChangeNavMenuLocationsSetting );
+                               if ( options.fire ) {
+                                       this.onChangeNavMenuLocationsSetting.call( setting, setting(), false );
+                               }
+                               return true;
+                       }
+
+                       return false;
+               };
+
+               /**
+                * Remove change listeners for nav_menu[], nav_menu_item[], or nav_menu_locations[] setting.
+                *
+                * @since 4.5.0
+                *
+                * @param {wp.customize.Value} setting
+                */
+               self.unbindSettingListener = function( setting ) {
+                       setting.unbind( this.onChangeNavMenuSetting );
+                       setting.unbind( this.onChangeNavMenuItemSetting );
+                       setting.unbind( this.onChangeNavMenuLocationsSetting );
+               };
+
+               /**
+                * Handle change for nav_menu[] setting for nav menu instances lacking partials.
+                *
+                * @since 4.5.0
+                *
+                * @this {wp.customize.Value}
+                */
+               self.onChangeNavMenuSetting = function() {
+                       var setting = this;
+
+                       self.handleUnplacedNavMenuInstances( {
+                               menu: setting._navMenuId
</ins><span class="cx" style="display: block; padding: 0 10px">                         } );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Ensure all nav menu instances with a theme_location assigned to this menu are handled.
+                       api.each( function( otherSetting ) {
+                               if ( ! otherSetting._navMenuThemeLocation ) {
+                                       return;
+                               }
+                               if ( setting._navMenuId === otherSetting() ) {
+                                       self.handleUnplacedNavMenuInstances( {
+                                               theme_location: otherSetting._navMenuThemeLocation
+                                       } );
+                               }
+                       } );
</ins><span class="cx" style="display: block; padding: 0 10px">                 };
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               /**
+                * Handle change for nav_menu_item[] setting for nav menu instances lacking partials.
+                *
+                * @since 4.5.0
+                *
+                * @param {object} newItem New value for nav_menu_item[] setting.
+                * @param {object} oldItem Old value for nav_menu_item[] setting.
+                * @this {wp.customize.Value}
+                */
+               self.onChangeNavMenuItemSetting = function( newItem, oldItem ) {
+                       var item = newItem || oldItem, navMenuSetting;
+                       navMenuSetting = api( 'nav_menu[' + String( item.nav_menu_term_id ) + ']' );
+                       if ( navMenuSetting ) {
+                               self.onChangeNavMenuSetting.call( navMenuSetting );
+                       }
+               };
+
+               /**
+                * Handle change for nav_menu_locations[] setting for nav menu instances lacking partials.
+                *
+                * @since 4.5.0
+                *
+                * @this {wp.customize.Value}
+                */
+               self.onChangeNavMenuLocationsSetting = function() {
+                       var setting = this, hasNavMenuInstance;
+                       self.handleUnplacedNavMenuInstances( {
+                               theme_location: setting._navMenuThemeLocation
+                       } );
+
+                       // If there are no wp_nav_menu() instances that refer to the theme location, do full refresh.
+                       hasNavMenuInstance = !! _.findWhere( _.values( self.data.navMenuInstanceArgs ), {
+                               theme_location: setting._navMenuThemeLocation
+                       } );
+                       if ( ! hasNavMenuInstance ) {
+                               api.selectiveRefresh.requestFullRefresh();
+                       }
+               };
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/nav-menus.php 2016-03-08 18:24:50 UTC (rev 36888)
+++ trunk/tests/phpunit/tests/customize/nav-menus.php   2016-03-09 00:08:51 UTC (rev 36889)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -617,15 +617,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">        function test_filter_wp_nav_menu_args() {
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'customize_register', $this->wp_customize );
</span><span class="cx" style="display: block; padding: 0 10px">                $menus = $this->wp_customize->nav_menus;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $menu_id = wp_create_nav_menu( 'Foo' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $results = $menus->filter_wp_nav_menu_args( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'echo'            => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        'fallback_cb'     => 'wp_page_menu',
</span><span class="cx" style="display: block; padding: 0 10px">                        'walker'          => '',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'menu'            => wp_create_nav_menu( 'Foo' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'menu'            => $menu_id,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'customize_preview_nav_menus_args', $results );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertTrue( $results['can_partial_refresh'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $results = $menus->filter_wp_nav_menu_args( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'echo'            => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -633,7 +635,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'walker'          => new Walker_Nav_Menu(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertArrayNotHasKey( 'customize_preview_nav_menus_args', $results );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertFalse( $results['can_partial_refresh'] );
+               $this->assertArrayHasKey( 'customize_preview_nav_menus_args', $results );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 'wp_page_menu', $results['fallback_cb'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $nav_menu_term = get_term( wp_create_nav_menu( 'Bar' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -644,8 +647,39 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'menu'            => $nav_menu_term,
</span><span class="cx" style="display: block; padding: 0 10px">                        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertTrue( $results['can_partial_refresh'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertArrayHasKey( 'customize_preview_nav_menus_args', $results );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $nav_menu_term->term_id, $results['customize_preview_nav_menus_args']['menu'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $results = $menus->filter_wp_nav_menu_args( array(
+                       'echo'            => true,
+                       'fallback_cb'     => 'wp_page_menu',
+                       'walker'          => '',
+                       'menu'            => $menu_id,
+                       'container'       => 'div',
+                       'items_wrap'      => '%3$s',
+               ) );
+               $this->assertTrue( $results['can_partial_refresh'] );
+
+               $results = $menus->filter_wp_nav_menu_args( array(
+                       'echo'            => true,
+                       'fallback_cb'     => 'wp_page_menu',
+                       'walker'          => '',
+                       'menu'            => $menu_id,
+                       'container'       => false,
+                       'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
+               ) );
+               $this->assertTrue( $results['can_partial_refresh'] );
+
+               $results = $menus->filter_wp_nav_menu_args( array(
+                       'echo'            => true,
+                       'fallback_cb'     => 'wp_page_menu',
+                       'walker'          => '',
+                       'menu'            => $menu_id,
+                       'container'       => false,
+                       'items_wrap'      => '%3$s',
+               ) );
+               $this->assertFalse( $results['can_partial_refresh'] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -691,6 +725,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test WP_Customize_Nav_Menus::export_preview_data() method.
+        *
+        * @see WP_Customize_Nav_Menus::export_preview_data()
+        */
+       function test_export_preview_data() {
+               ob_start();
+               $this->wp_customize->nav_menus->export_preview_data();
+               $html = ob_get_clean();
+               $this->assertTrue( (bool) preg_match( '/_wpCustomizePreviewNavMenusExports = ({.+})/s', $html, $matches ) );
+               $exported_data = json_decode( $matches[1], true );
+               $this->assertArrayHasKey( 'navMenuInstanceArgs', $exported_data );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test WP_Customize_Nav_Menus::render_nav_menu_partial() method.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see WP_Customize_Nav_Menus::render_nav_menu_partial()
</span></span></pre>
</div>
</div>

</body>
</html>