<!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>[41237] trunk: Map nav menu locations on theme switch</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/41237">41237</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/41237","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>obenland</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-08-09 21:03:16 +0000 (Wed, 09 Aug 2017)</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'>Map nav menu locations on theme switch

This will send nav menu locations through three levels of mapping:
1. If both themes have only one location, that gets mapped.
2. If both themes have locations with the same slug, they get mapped.
3. Locations that (even partially) match slugs from a similar kind of menu location will get mapped.

Menu locations are mapped for Live Previews in the Customizer and during theme switches.

Props westonruter, obenland, welcher, melchoyce.
Fixes <a href="https://core.trac.wordpress.org/ticket/39692">#39692</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="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesnavmenuphp">trunk/src/wp-includes/nav-menu.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmenunavmenuphp">trunk/tests/phpunit/tests/menu/nav-menu.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    2017-08-09 04:06:17 UTC (rev 41236)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php      2017-08-09 21:03:16 UTC (rev 41237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,12 +27,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $manager;
</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">-         * Previewed Menus.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Original nav menu locations before the theme was switched.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 4.3.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 4.9.0
</ins><span class="cx" style="display: block; padding: 0 10px">          * @var array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public $previewed_menus;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $original_nav_menu_locations;
</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">         * Constructor.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,8 +42,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param object $manager An instance of the WP_Customize_Manager class.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $manager ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->previewed_menus = array();
-               $this->manager         = $manager;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->manager = $manager;
+               $this->original_nav_menu_locations = get_nav_menu_locations();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L469-L499
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register', array( $this, 'customize_register' ), 11 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -582,6 +582,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '&hellip;' );
</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">+                // Attempt to re-map the nav menu location assignments when previewing a theme switch.
+               $mapped_nav_menu_locations = array();
+               if ( ! $this->manager->is_theme_active() ) {
+                       $mapped_nav_menu_locations = wp_map_nav_menu_locations( get_nav_menu_locations(), $this->original_nav_menu_locations );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $locations as $location => $description ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $setting_id = "nav_menu_locations[{$location}]";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -600,6 +606,11 @@
</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">+                        // Override the assigned nav menu location if mapped during previewed theme switch.
+                       if ( isset( $mapped_nav_menu_locations[ $location ] ) ) {
+                               $this->manager->set_post_value( $setting_id, $mapped_nav_menu_locations[ $location ] );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->manager->add_control( new WP_Customize_Nav_Menu_Location_Control( $this->manager, $setting_id, array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label'       => $description,
</span><span class="cx" style="display: block; padding: 0 10px">                                'location_id' => $location,
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2017-08-09 04:06:17 UTC (rev 41236)
+++ trunk/src/wp-includes/default-filters.php   2017-08-09 21:03:16 UTC (rev 41237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -262,6 +262,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'template_redirect',   'wp_shortlink_header',             11, 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_print_footer_scripts', '_wp_footer_scripts'                 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'init',                'check_theme_switched',            99    );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'after_switch_theme',  '_wp_menus_changed'                      );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'after_switch_theme',  '_wp_sidebars_changed'                   );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_print_styles',     'print_emoji_styles'                     );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesnavmenuphp"></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/nav-menu.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/nav-menu.php        2017-08-09 04:06:17 UTC (rev 41236)
+++ trunk/src/wp-includes/nav-menu.php  2017-08-09 21:03:16 UTC (rev 41237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1026,3 +1026,111 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Handle menu config after theme change.
+ *
+ * @access private
+ * @since 4.9.0
+ */
+function _wp_menus_changed() {
+       $old_nav_menu_locations    = get_option( 'theme_switch_menu_locations', array() );
+       $new_nav_menu_locations    = get_nav_menu_locations();
+       $mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
+
+       set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
+       delete_option( 'theme_switch_menu_locations' );
+}
+
+/**
+ * Maps nav menu locations according to assignments in previously active theme.
+ *
+ * @since 4.9.0
+ *
+ * @param array $new_nav_menu_locations New nav menu locations assignments.
+ * @param array $old_nav_menu_locations Old nav menu locations assignments.
+ * @return array Nav menus mapped to new nav menu locations.
+ */
+function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) {
+       $registered_nav_menus = get_registered_nav_menus();
+
+       // Short-circuit if there are no old nav menu location assignments to map.
+       if ( empty( $old_nav_menu_locations ) ) {
+               return $new_nav_menu_locations;
+       }
+
+       // If old and new theme have just one location, map it and we're done.
+       if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) {
+               $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations );
+               return $new_nav_menu_locations;
+       }
+
+       $old_locations = array_keys( $old_nav_menu_locations );
+
+       // Map locations with the same slug.
+       foreach ( $registered_nav_menus as $location => $name ) {
+               if ( in_array( $location, $old_locations, true ) ) {
+                       $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ];
+                       unset( $old_nav_menu_locations[ $location ] );
+               }
+       }
+
+       // If there are no old nav menu locations left, then we're done.
+       if ( empty( $old_nav_menu_locations ) ) {
+               return $new_nav_menu_locations;
+       }
+
+       /*
+        * If old and new theme both have locations that contain phrases
+        * from within the same group, make an educated guess and map it.
+        */
+       $common_slug_groups = array(
+               array( 'header', 'main', 'navigation', 'primary', 'top' ),
+               array( 'bottom', 'footer', 'secondary', 'subsidiary' ),
+       );
+
+       // Go through each group...
+       foreach ( $common_slug_groups as $slug_group ) {
+
+               // ...and see if any of these slugs...
+               foreach ( $slug_group as $slug ) {
+
+                       // ...and any of the new menu locations...
+                       foreach ( $registered_nav_menus as $new_location => $name ) {
+
+                               // ...actually match!
+                               if ( false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) {
+                                       continue;
+                               }
+
+                               // Then see if any of the old locations...
+                               foreach ( $old_nav_menu_locations as $location => $menu_id ) {
+
+                                       // ...and any slug in the same group...
+                                       foreach ( $slug_group as $slug ) {
+
+                                               // ... have a match as well.
+                                               if ( false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) {
+                                                       continue;
+                                               }
+
+                                               // Make sure this location wasn't mapped and removed previously.
+                                               if ( ! empty( $old_nav_menu_locations[ $location ] ) ) {
+
+                                                       // We have a match that can be mapped!
+                                                       $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ];
+
+                                                       // Remove the mapped location so it can't be mapped again.
+                                                       unset( $old_nav_menu_locations[ $location ] );
+
+                                                       // Go back and check the next new menu location.
+                                                       continue 3;
+                                               }
+                                       } // endforeach ( $slug_group as $slug )
+                               } // endforeach ( $old_nav_menu_locations as $location => $menu_id )
+                       } // endforeach foreach ( $registered_nav_menus as $new_location => $name )
+               } // endforeach ( $slug_group as $slug )
+       } // endforeach ( $common_slug_groups as $slug_group )
+
+       return $new_nav_menu_locations;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesthemephp"></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/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2017-08-09 04:06:17 UTC (rev 41236)
+++ trunk/src/wp-includes/theme.php     2017-08-09 21:03:16 UTC (rev 41237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -691,6 +691,7 @@
</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">        $nav_menu_locations = get_theme_mod( 'nav_menu_locations' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        add_option( 'theme_switch_menu_locations', $nav_menu_locations );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( func_num_args() > 1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $stylesheet = func_get_arg( 1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -731,13 +732,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'wp_ajax_customize_save' === current_action() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        remove_theme_mod( 'sidebars_widgets' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               if ( ! empty( $nav_menu_locations ) ) {
-                       $nav_mods = get_theme_mod( 'nav_menu_locations' );
-                       if ( empty( $nav_mods ) ) {
-                               set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
-                       }
-               }
</del><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">        update_option( 'theme_switched', $old_theme->get_stylesheet() );
</span></span></pre></div>
<a id="trunktestsphpunittestsmenunavmenuphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/menu/nav-menu.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/menu/nav-menu.php                               (rev 0)
+++ trunk/tests/phpunit/tests/menu/nav-menu.php 2017-08-09 21:03:16 UTC (rev 41237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,182 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group navmenus
+ */
+class Tests_Nav_Menu_Theme_Change extends WP_UnitTestCase {
+
+       /**
+        * Set up.
+        */
+       function setUp() {
+               parent::setUp();
+
+               // Unregister all nav menu locations.
+               foreach ( array_keys( get_registered_nav_menus() ) as $location ) {
+                       unregister_nav_menu( $location );
+               }
+       }
+
+       /**
+        * Register nav menu locations.
+        *
+        * @param array $locations Location slugs.
+        */
+       function register_nav_menu_locations( $locations ) {
+               foreach ( $locations as $location ) {
+                       register_nav_menu( $location, ucfirst( $location ) );
+               }
+       }
+
+       /**
+        * Two themes with one location each should just map, switching to a theme not previously-active.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_one_location_each() {
+               $this->register_nav_menu_locations( array( 'primary' ) );
+               $prev_theme_nav_menu_locations = array(
+                       'unique-slug' => 1,
+               );
+               $old_next_theme_nav_menu_locations = array(); // It was not active before.
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = array(
+                       'primary' => 1,
+               );
+               $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * Locations with the same name should map, switching to a theme not previously-active.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_locations_with_same_slug() {
+               $this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
+               $prev_theme_nav_menu_locations = array(
+                       'primary' => 1,
+                       'secondary' => 2,
+               );
+
+               $old_next_theme_nav_menu_locations = array(); // It was not active before.
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = $prev_theme_nav_menu_locations;
+               $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * If the new theme was previously active, we should honor any changes to nav menu mapping done when the other theme was active.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_new_theme_previously_active() {
+               $this->register_nav_menu_locations( array( 'primary' ) );
+
+               $prev_theme_nav_menu_locations = array(
+                       'primary' => 1,
+                       'secondary' => 2,
+               );
+
+               // Nav menu location assignments that were set on the next theme when it was previously active.
+               $old_next_theme_nav_menu_locations = array(
+                       'primary' => 3,
+               );
+
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = wp_array_slice_assoc( $prev_theme_nav_menu_locations, array_keys( get_registered_nav_menus() ) );
+               $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * Make educated guesses on theme locations.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_location_guessing() {
+               $this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
+
+               $prev_theme_nav_menu_locations = array(
+                       'header' => 1,
+                       'footer' => 2,
+               );
+
+               $old_next_theme_nav_menu_locations = array();
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = array(
+                       'primary' => 1,
+                       'secondary' => 2,
+               );
+               $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * Make sure two locations that fall in the same group don't get the same menu assigned.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_location_guessing_one_menu_per_group() {
+               $this->register_nav_menu_locations( array( 'primary' ) );
+               $prev_theme_nav_menu_locations = array(
+                       'top-menu' => 1,
+                       'secondary' => 2,
+               );
+
+               $old_next_theme_nav_menu_locations = array();
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = array(
+                       'main' => 1,
+               );
+               $this->assertEqualSets( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * Make sure two locations that fall in the same group get menus assigned from the same group.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_location_guessing_one_menu_per_location() {
+               $this->register_nav_menu_locations( array( 'primary', 'main' ) );
+
+               $prev_theme_nav_menu_locations = array(
+                       'navigation-menu' => 1,
+                       'top-menu' => 2,
+               );
+
+               $old_next_theme_nav_menu_locations = array();
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = array(
+                       'main' => 1,
+                       'primary' => 2,
+               );
+               $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+
+       /**
+        * Technically possible to register menu locations numerically.
+        *
+        * @covers wp_map_nav_menu_locations()
+        */
+       function test_numerical_locations() {
+               $this->register_nav_menu_locations( array( 'primary', 1 ) );
+
+               $prev_theme_nav_menu_locations = array(
+                       'main' => 1,
+                       'secondary' => 2,
+                       'tertiary' => 3,
+               );
+
+               $old_next_theme_nav_menu_locations = array();
+               $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations );
+
+               $expected_nav_menu_locations = array(
+                       'primary' => 1,
+               );
+               $this->assertEqualSets( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>