WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/09/2017 09:03:16 PM (11 months ago)
Author:
obenland
Message:

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 #39692.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r41205 r41237  
    2828
    2929    /**
    30      * Previewed Menus.
    31      *
    32      * @since 4.3.0
     30     * Original nav menu locations before the theme was switched.
     31     *
     32     * @since 4.9.0
    3333     * @var array
    3434     */
    35     public $previewed_menus;
     35    protected $original_nav_menu_locations;
    3636
    3737    /**
     
    4343     */
    4444    public function __construct( $manager ) {
    45         $this->previewed_menus = array();
    46         $this->manager         = $manager;
     45        $this->manager = $manager;
     46        $this->original_nav_menu_locations = get_nav_menu_locations();
    4747
    4848        // See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L469-L499
     
    583583        }
    584584
     585        // Attempt to re-map the nav menu location assignments when previewing a theme switch.
     586        $mapped_nav_menu_locations = array();
     587        if ( ! $this->manager->is_theme_active() ) {
     588            $mapped_nav_menu_locations = wp_map_nav_menu_locations( get_nav_menu_locations(), $this->original_nav_menu_locations );
     589        }
     590
    585591        foreach ( $locations as $location => $description ) {
    586592            $setting_id = "nav_menu_locations[{$location}]";
     
    599605                    'default'           => 0,
    600606                ) );
     607            }
     608
     609            // Override the assigned nav menu location if mapped during previewed theme switch.
     610            if ( isset( $mapped_nav_menu_locations[ $location ] ) ) {
     611                $this->manager->set_post_value( $setting_id, $mapped_nav_menu_locations[ $location ] );
    601612            }
    602613
Note: See TracChangeset for help on using the changeset viewer.