Make WordPress Core

Ticket #32814: 32814.12.diff

File 32814.12.diff, 24.8 KB (added by westonruter, 10 years ago)

Additional changes: https://github.com/xwp/wordpress-develop/compare/6a4d458...e971770e47

  • src/wp-admin/js/customize-nav-menus.js

    diff --git src/wp-admin/js/customize-nav-menus.js src/wp-admin/js/customize-nav-menus.js
    index 7d5e054..1903f2a 100644
     
    957957                        var control = this;
    958958                        api.Control.prototype.initialize.call( control, id, options );
    959959                        control.active.validate = function() {
    960                                 return api.section( control.section() ).active();
     960                                var value, section = api.section( control.section() );
     961                                if ( section ) {
     962                                        value = section.active();
     963                                } else {
     964                                        value = false;
     965                                }
     966                                return value;
    961967                        };
    962968                },
    963969
     
    16021608                         * being deactivated.
    16031609                         */
    16041610                        control.active.validate = function() {
    1605                                 return api.section( control.section() ).active();
     1611                                var value, section = api.section( control.section() );
     1612                                if ( section ) {
     1613                                        value = section.active();
     1614                                } else {
     1615                                        value = false;
     1616                                }
     1617                                return value;
    16061618                        };
    16071619
    16081620                        control.nameElement = new api.Element( control.container.find( '.menu-name-field' ) );
     
    16481660                         * being deactivated.
    16491661                         */
    16501662                        control.active.validate = function() {
    1651                                 return api.section( control.section() ).active();
     1663                                var value, section = api.section( control.section() );
     1664                                if ( section ) {
     1665                                        value = section.active();
     1666                                } else {
     1667                                        value = false;
     1668                                }
     1669                                return value;
    16521670                        };
    16531671
    16541672                        control.autoAddElement = new api.Element( control.container.find( 'input[type=checkbox].auto_add' ) );
     
    16911709                        var control = this,
    16921710                                menuId = control.params.menu_id,
    16931711                                menu = control.setting(),
    1694                                 name;
     1712                                name,
     1713                                widgetTemplate,
     1714                                select;
    16951715
    16961716                        if ( 'undefined' === typeof this.params.menu_id ) {
    16971717                                throw new Error( 'params.menu_id was not defined' );
     
    17031723                         * being deactivated.
    17041724                         */
    17051725                        control.active.validate = function() {
    1706                                 return api.section( control.section() ).active();
     1726                                var value, section = api.section( control.section() );
     1727                                if ( section ) {
     1728                                        value = section.active();
     1729                                } else {
     1730                                        value = false;
     1731                                }
     1732                                return value;
    17071733                        };
    17081734
    17091735                        control.$controlSection = control.container.closest( '.control-section' );
     
    17251751                        if ( menu ) {
    17261752                                name = displayNavMenuName( menu.name );
    17271753
     1754                                // Add the menu to the existing controls.
    17281755                                api.control.each( function( widgetControl ) {
    17291756                                        if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
    17301757                                                return;
    17311758                                        }
    1732                                         var select = widgetControl.container.find( 'select' );
    1733                                         if ( select.find( 'option[value=' + String( menuId ) + ']' ).length === 0 ) {
     1759                                        widgetControl.container.find( '.nav-menu-widget-form-controls:first' ).show();
     1760                                        widgetControl.container.find( '.nav-menu-widget-no-menus-message:first' ).hide();
     1761
     1762                                        select = widgetControl.container.find( 'select' );
     1763                                        if ( 0 === select.find( 'option[value=' + String( menuId ) + ']' ).length ) {
    17341764                                                select.append( new Option( name, menuId ) );
    17351765                                        }
    17361766                                } );
    1737                                 $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( name, menuId ) );
     1767
     1768                                // Add the menu to the widget template.
     1769                                widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
     1770                                widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).show();
     1771                                widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).hide();
     1772                                select = widgetTemplate.find( '.widget-inside select:first' );
     1773                                if ( 0 === select.find( 'option[value=' + String( menuId ) + ']' ).length ) {
     1774                                        select.append( new Option( name, menuId ) );
     1775                                }
    17381776                        }
    17391777                },
    17401778
     
    17591797                                                var select = widgetControl.container.find( 'select' );
    17601798                                                select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
    17611799                                        });
    1762                                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( name );
    17631800                                }
    17641801                        } );
    17651802
     
    18311868                                                menuItemControl.setting.set( setting );
    18321869                                        });
    18331870                                });
    1834                         });
    18351871
     1872                        });
    18361873                        control.isReordering = false;
    18371874
    18381875                        /**
     
    18691906                        var control = this,
    18701907                                section,
    18711908                                menuId = control.params.menu_id,
    1872                                 removeSection;
     1909                                removeSection,
     1910                                widgetTemplate,
     1911                                navMenuCount = 0;
    18731912                        section = api.section( control.section() );
    18741913                        removeSection = function() {
    18751914                                section.container.remove();
     
    18881927                                removeSection();
    18891928                        }
    18901929
     1930                        api.each(function( setting ) {
     1931                                if ( /^nav_menu\[/.test( setting.id ) && false !== setting() ) {
     1932                                        navMenuCount += 1;
     1933                                }
     1934                        });
     1935
    18911936                        // Remove the menu from any Custom Menu widgets.
    18921937                        api.control.each(function( widgetControl ) {
    18931938                                if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
     
    18971942                                if ( select.val() === String( menuId ) ) {
    18981943                                        select.prop( 'selectedIndex', 0 ).trigger( 'change' );
    18991944                                }
    1900                                 select.find( 'option[value=' + String( menuId ) + ']' ).remove();
     1945
     1946                                widgetControl.container.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
     1947                                widgetControl.container.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
     1948                                widgetControl.container.find( 'option[value=' + String( menuId ) + ']' ).remove();
    19011949                        });
    1902                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).remove();
     1950
     1951                        // Remove the menu to the nav menu widget template.
     1952                        widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
     1953                        widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
     1954                        widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
     1955                        widgetTemplate.find( 'option[value=' + String( menuId ) + ']' ).remove();
    19031956                },
    19041957
    19051958                // Setup theme location checkboxes.
     
    22922345
    22932346                        // Focus on the new menu section.
    22942347                        api.section( customizeId ).focus(); // @todo should we focus on the new menu's control and open the add-items panel? Thinking user flow...
     2348
     2349                        // Fix an issue with extra space at top immediately after creating new menu.
     2350                        $( '#menu-to-edit' ).css( 'margin-top', 0 );
    22952351                }
    22962352        });
    22972353
     
    23572413                var insertedMenuIdMapping = {};
    23582414
    23592415                _( data.nav_menu_updates ).each(function( update ) {
    2360                         var oldCustomizeId, newCustomizeId, customizeId, oldSetting, newSetting, setting, settingValue, oldSection, newSection, wasSaved;
     2416                        var oldCustomizeId, newCustomizeId, customizeId, oldSetting, newSetting, setting, settingValue, oldSection, newSection, wasSaved, widgetTemplate, navMenuCount;
    23612417                        if ( 'inserted' === update.status ) {
    23622418                                if ( ! update.previous_term_id ) {
    23632419                                        throw new Error( 'Expected previous_term_id' );
     
    24072463                                        }
    24082464                                } );
    24092465
    2410                                 // Remove old setting and control.
    2411                                 oldSection.container.remove();
    2412                                 api.section.remove( oldCustomizeId );
    2413 
    2414                                 // Add new control to take its place.
     2466                                // Add new control for the new menu.
    24152467                                api.section.add( newCustomizeId, newSection );
    24162468
    2417                                 // Delete the placeholder and preview the new setting.
     2469                                // Update the values for nav menus in Custom Menu controls.
     2470                                api.control.each( function( setting ) {
     2471                                        if ( ! setting.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== setting.params.widget_id_base ) {
     2472                                                return;
     2473                                        }
     2474                                        var select, oldMenuOption, newMenuOption;
     2475                                        select = setting.container.find( 'select' );
     2476                                        oldMenuOption = select.find( 'option[value=' + String( update.previous_term_id ) + ']' );
     2477                                        newMenuOption = select.find( 'option[value=' + String( update.term_id ) + ']' );
     2478                                        newMenuOption.prop( 'selected', oldMenuOption.prop( 'selected' ) );
     2479                                        oldMenuOption.remove();
     2480                                } );
     2481
     2482                                // Delete the old placeholder nav_menu.
    24182483                                oldSetting.callbacks.disable(); // Prevent setting triggering Customizer dirty state when set.
    24192484                                oldSetting.set( false );
    24202485                                oldSetting.preview();
    24212486                                newSetting.preview();
     2487                                oldSetting._dirty = false;
     2488
     2489                                // Remove nav_menu section.
     2490                                oldSection.container.remove();
     2491                                api.section.remove( oldCustomizeId );
     2492
     2493                                // Remove the menu to the nav menu widget template.
     2494                                navMenuCount = 0;
     2495                                api.each(function( setting ) {
     2496                                        if ( /^nav_menu\[/.test( setting.id ) && false !== setting() ) {
     2497                                                navMenuCount += 1;
     2498                                        }
     2499                                });
     2500                                widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
     2501                                widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
     2502                                widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
     2503                                widgetTemplate.find( 'option[value=' + String( update.previous_term_id ) + ']' ).remove();
    24222504
    24232505                                // Update nav_menu_locations to reference the new ID.
    24242506                                api.each( function( setting ) {
     
    24352517                                        // @todo This doesn't seem to be working.
    24362518                                        newSection.expand();
    24372519                                }
    2438 
    2439                                 // @todo Update the Custom Menu selects, ensuring the newly-inserted IDs are used for any that have selected a placeholder menu.
    24402520                        } else if ( 'updated' === update.status ) {
    24412521                                customizeId = 'nav_menu[' + String( update.term_id ) + ']';
    24422522                                if ( ! api.has( customizeId ) ) {
     
    25082588                                        previewer: api.previewer
    25092589                                } );
    25102590
    2511                                 // Remove old setting and control.
     2591                                // Remove old control.
    25122592                                oldControl.container.remove();
    25132593                                api.control.remove( oldCustomizeId );
    25142594
     
    25202600                                oldSetting.set( false );
    25212601                                oldSetting.preview();
    25222602                                newSetting.preview();
     2603                                oldSetting._dirty = false;
    25232604
    25242605                                newControl.container.toggleClass( 'menu-item-edit-inactive', oldControl.container.hasClass( 'menu-item-edit-inactive' ) );
    25252606                        }
    25262607                });
    25272608
    2528                 // @todo trigger change event for each Custom Menu widget that was modified.
     2609                /*
     2610                 * Update the settings for any nav_menu widgets that had selected a placeholder ID.
     2611                 */
     2612                _.each( data.widget_nav_menu_updates, function( widgetSettingValue, widgetSettingId ) {
     2613                        var setting = api( widgetSettingId );
     2614                        if ( setting ) {
     2615                                setting._value = widgetSettingValue;
     2616                                setting.preview(); // Send to the preview now so that menu refresh will use the inserted menu.
     2617                        }
     2618                });
    25292619        };
    25302620
    25312621        /**
  • src/wp-admin/js/customize-widgets.js

    diff --git src/wp-admin/js/customize-widgets.js src/wp-admin/js/customize-widgets.js
    index 2907b4f..8aa603a 100644
     
    786786
    787787                        // Handle widgets that support live previews
    788788                        $widgetContent.on( 'change input propertychange', ':input', function( e ) {
    789                                 if ( self.liveUpdateMode ) {
    790                                         if ( e.type === 'change' ) {
    791                                                 self.updateWidget();
    792                                         } else if ( this.checkValidity && this.checkValidity() ) {
    793                                                 updateWidgetDebounced();
    794                                         }
     789                                if ( ! self.liveUpdateMode ) {
     790                                        return;
     791                                }
     792                                if ( e.type === 'change' || ( this.checkValidity && this.checkValidity() ) ) {
     793                                        updateWidgetDebounced();
    795794                                }
    796795                        } );
    797796
     
    10411040                        params.wp_customize = 'on';
    10421041                        params.nonce = api.Widgets.data.nonce;
    10431042                        params.theme = api.settings.theme.stylesheet;
     1043                        params.customized = wp.customize.previewer.query().customized;
    10441044
    10451045                        data = $.param( params );
    10461046                        $inputs = this._getInputs( $widgetContent );
  • src/wp-includes/class-wp-customize-setting.php

    diff --git src/wp-includes/class-wp-customize-setting.php src/wp-includes/class-wp-customize-setting.php
    index fa94346..d007965 100644
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    16251625                $this->_original_value    = $this->value();
    16261626                $this->_previewed_blog_id = get_current_blog_id();
    16271627
     1628                add_filter( 'wp_get_nav_menus', array( $this, 'filter_wp_get_nav_menus' ), 10, 2 );
    16281629                add_filter( 'wp_get_nav_menu_object', array( $this, 'filter_wp_get_nav_menu_object' ), 10, 2 );
    16291630                add_filter( 'default_option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
    16301631                add_filter( 'option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
    16311632        }
    16321633
    16331634        /**
     1635         * Filter the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed.
     1636         *
     1637         * @since 4.3.0
     1638         * @access public
     1639         *
     1640         * @see wp_get_nav_menus()
     1641         *
     1642         * @param array $menus An array of menu objects.
     1643         * @param array $args  An array of arguments used to retrieve menu objects.
     1644         * @return array
     1645         */
     1646        public function filter_wp_get_nav_menus( $menus, $args ) {
     1647                if ( get_current_blog_id() !== $this->_previewed_blog_id ) {
     1648                        return $menus;
     1649                }
     1650
     1651                $setting_value = $this->value();
     1652                $is_delete = ( false === $setting_value );
     1653                $index = -1;
     1654
     1655                // Find the existing menu item's position in the list.
     1656                foreach ( $menus as $i => $menu ) {
     1657                        if ( (int) $this->term_id === (int) $menu->term_id || (int) $this->previous_term_id === (int) $menu->term_id ) {
     1658                                $index = $i;
     1659                                break;
     1660                        }
     1661                }
     1662
     1663                if ( $is_delete ) {
     1664                        // Handle deleted menu by removing it from the list.
     1665                        if ( -1 !== $index ) {
     1666                                array_splice( $menus, $index, 1 );
     1667                        }
     1668                } else {
     1669                        // Handle menus being updated or inserted.
     1670                        $menu_obj = (object) array_merge( array(
     1671                                'term_id'          => $this->term_id,
     1672                                'term_taxonomy_id' => $this->term_id,
     1673                                'slug'             => sanitize_title( $setting_value['name'] ),
     1674                                'count'            => 0,
     1675                                'term_group'       => 0,
     1676                                'taxonomy'         => self::TAXONOMY,
     1677                                'filter'           => 'raw',
     1678                        ), $setting_value );
     1679
     1680                        array_splice( $menus, $index, ( -1 === $index ? 0 : 1 ), array( $menu_obj ) );
     1681                }
     1682
     1683                // Make sure the menu objects get re-sorted after an update/insert.
     1684                if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
     1685                        $this->_current_menus_sort_orderby = $args['orderby'];
     1686                        usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
     1687                }
     1688                // @todo add support for $args['hide_empty'] === true
     1689
     1690                return $menus;
     1691        }
     1692
     1693        /**
     1694         * Temporary non-closure passing of orderby value to function.
     1695         *
     1696         * @since 4.3.0
     1697         * @access protected
     1698         * @var string
     1699         *
     1700         * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
     1701         * @see WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby()
     1702         */
     1703        protected $_current_menus_sort_orderby;
     1704
     1705        /**
     1706         * Sort menu objects by the class-supplied orderby property.
     1707         *
     1708         * This is a workaround for a lack of closures.
     1709         *
     1710         * @since 4.3.0
     1711         * @access protected
     1712         * @param object $menu1
     1713         * @param object $menu2
     1714         * @return int
     1715         *
     1716         * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
     1717         */
     1718        protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
     1719                $key = $this->_current_menus_sort_orderby;
     1720                return strcmp( $menu1->$key, $menu2->$key );
     1721        }
     1722
     1723        /**
    16341724         * Filter the wp_get_nav_menu_object() result to supply the previewed menu object.
    16351725         *
    16361726         * Requesting a nav_menu object by anything but ID is not supported.
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    17521842        }
    17531843
    17541844        /**
     1845         * Storage for data to be sent back to client in customize_save_response filter.
     1846         *
     1847         * @access protected
     1848         * @since 4.3.0
     1849         * @var array
     1850         *
     1851         * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
     1852         */
     1853        protected $_widget_nav_menu_updates = array();
     1854
     1855        /**
    17551856         * Create/update the nav_menu term for this setting.
    17561857         *
    17571858         * Any created menus will have their assigned term IDs exported to the client
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    17611862         * To delete a menu, the client can send false as the value.
    17621863         *
    17631864         * @since 4.3.0
    1764          * @access public
     1865         * @access protected
    17651866         *
    17661867         * @see wp_update_nav_menu_object()
    17671868         *
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    18441945                        update_option( 'nav_menu_options', $nav_menu_options );
    18451946                }
    18461947
    1847                 // Make sure that new menus assigned to nav menu locations use their new IDs.
    18481948                if ( 'inserted' === $this->update_status ) {
     1949                        // Make sure that new menus assigned to nav menu locations use their new IDs.
    18491950                        foreach ( $this->manager->settings() as $setting ) {
    18501951                                if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) {
    18511952                                        continue;
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    18571958                                        $setting->save();
    18581959                                }
    18591960                        }
     1961
     1962                        // Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client.
     1963                        foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
     1964                                $nav_menu_widget_setting = $this->manager->get_setting( $setting_id );
     1965                                if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) {
     1966                                        continue;
     1967                                }
     1968
     1969                                $widget_instance = $nav_menu_widget_setting->post_value(); // Note that this calls WP_Customize_Widgets::sanitize_widget_instance().
     1970                                if ( empty( $widget_instance['nav_menu'] ) || intval( $widget_instance['nav_menu'] ) !== $this->previous_term_id ) {
     1971                                        continue;
     1972                                }
     1973
     1974                                $widget_instance['nav_menu'] = $this->term_id;
     1975                                $updated_widget_instance = $this->manager->widgets->sanitize_widget_js_instance( $widget_instance );
     1976                                $this->manager->set_post_value( $nav_menu_widget_setting->id, $updated_widget_instance );
     1977                                $nav_menu_widget_setting->save();
     1978
     1979                                $this->_widget_nav_menu_updates[ $nav_menu_widget_setting->id ] = $updated_widget_instance;
     1980                        }
    18601981                }
    18611982        }
    18621983
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    18641985         * Updates a nav_menu_options array.
    18651986         *
    18661987         * @since 4.3.0
    1867          * @access public
     1988         * @access protected
    18681989         *
    18691990         * @see WP_Customize_Nav_Menu_Setting::filter_nav_menu_options()
    18701991         * @see WP_Customize_Nav_Menu_Setting::update()
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    19052026                if ( ! isset( $data['nav_menu_updates'] ) ) {
    19062027                        $data['nav_menu_updates'] = array();
    19072028                }
     2029                if ( ! isset( $data['widget_nav_menu_updates'] ) ) {
     2030                        $data['widget_nav_menu_updates'] = array();
     2031                }
    19082032
    19092033                $data['nav_menu_updates'][] = array(
    19102034                        'term_id'          => $this->term_id,
    class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { 
    19142038                        'saved_value'      => 'deleted' === $this->update_status ? null : $this->value(),
    19152039                );
    19162040
     2041                $data['widget_nav_menu_updates'] = array_merge(
     2042                        $data['widget_nav_menu_updates'],
     2043                        $this->_widget_nav_menu_updates
     2044                );
     2045                $this->_widget_nav_menu_updates = array();
     2046
    19172047                return $data;
    19182048        }
    19192049}
  • src/wp-includes/class-wp-customize-widgets.php

    diff --git src/wp-includes/class-wp-customize-widgets.php src/wp-includes/class-wp-customize-widgets.php
    index cf93bb5..775e8bb 100644
    final class WP_Customize_Widgets { 
    12411241        public function call_widget_update( $widget_id ) {
    12421242                global $wp_registered_widget_updates, $wp_registered_widget_controls;
    12431243
     1244                $setting_id = $this->get_setting_id( $widget_id );
     1245
     1246                /*
     1247                 * Make sure that other setting changes have previewed since this widget
     1248                 * may depend on them (e.g. Menus being present for Custom Menu widget).
     1249                 */
     1250                if ( ! did_action( 'customize_preview_init' ) ) {
     1251                        foreach ( $this->manager->settings() as $setting ) {
     1252                                if ( $setting->id !== $setting_id ) {
     1253                                        $setting->preview();
     1254                                }
     1255                        }
     1256                }
     1257
    12441258                $this->start_capturing_option_updates();
    12451259                $parsed_id   = $this->parse_widget_id( $widget_id );
    12461260                $option_name = 'widget_' . $parsed_id['id_base'];
    final class WP_Customize_Widgets { 
    13211335                 * in place from WP_Customize_Setting::preview() will use this value
    13221336                 * instead of the default widget instance value (an empty array).
    13231337                 */
    1324                 $setting_id = $this->get_setting_id( $widget_id );
    13251338                $this->manager->set_post_value( $setting_id, $instance );
    13261339
    13271340                // Obtain the widget control with the updated instance in place.
  • src/wp-includes/default-widgets.php

    diff --git src/wp-includes/default-widgets.php src/wp-includes/default-widgets.php
    index cb6c2bb..22e61e5 100644
    class WP_Widget_Tag_Cloud extends WP_Widget { 
    15701570                $menus = wp_get_nav_menus();
    15711571
    15721572                // If no menus exists, direct the user to go and create some.
    1573                 if ( !$menus ) {
    1574                         echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
    1575                         return;
    1576                 }
    15771573                ?>
    1578                 <p>
    1579                         <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
    1580                         <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
    1581                 </p>
    1582                 <p>
    1583                         <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
    1584                         <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
    1585                                 <option value="0"><?php _e( '&mdash; Select &mdash;' ) ?></option>
    1586                 <?php
    1587                         foreach ( $menus as $menu ) {
    1588                                 echo '<option value="' . $menu->term_id . '"'
    1589                                         . selected( $nav_menu, $menu->term_id, false )
    1590                                         . '>'. esc_html( $menu->name ) . '</option>';
     1574                <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
     1575                        <?php
     1576                        if ( isset( $GLOBALS['wp_customize'] ) && $GLOBALS['wp_customize'] instanceof WP_Customize_Manager ) {
     1577                                // @todo When expanding a panel, the JS should be smart enough to collapse any existing panels and sections.
     1578                                $url = 'javascript: wp.customize.section.each(function( section ){ section.collapse(); }); wp.customize.panel( "nav_menus" ).focus();';
     1579                        } else {
     1580                                $url = admin_url( 'nav-menus.php' );
    15911581                        }
    1592                 ?>
    1593                         </select>
     1582                        ?>
     1583                        <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
    15941584                </p>
     1585                <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
     1586                        <p>
     1587                                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
     1588                                <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>"/>
     1589                        </p>
     1590                        <p>
     1591                                <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
     1592                                <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
     1593                                        <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
     1594                                        <?php foreach ( $menus as $menu ) : ?>
     1595                                                <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
     1596                                                        <?php echo esc_html( $menu->name ); ?>
     1597                                                </option>
     1598                                        <?php endforeach; ?>
     1599                                </select>
     1600                        </p>
     1601                </div>
    15951602                <?php
    15961603        }
    15971604}
  • src/wp-includes/nav-menu.php

    diff --git src/wp-includes/nav-menu.php src/wp-includes/nav-menu.php
    index d12abc5..29666f0 100644
     
    1212 *
    1313 * @since 3.0.0
    1414 *
    15  * @param string $menu Menu ID, slug, or name.
     15 * @param string $menu Menu ID, slug, or name - or the menu object.
    1616 * @return object|false False if $menu param isn't supplied or term does not exist, menu object if successful.
    1717 */
    1818function wp_get_nav_menu_object( $menu ) {
    1919        $menu_obj = false;
    20         if ( $menu ) {
     20
     21        if ( is_object( $menu ) ) {
     22                $menu_obj = $menu;
     23        }
     24
     25        if ( $menu && ! $menu_obj ) {
    2126                $menu_obj = get_term( $menu, 'nav_menu' );
    2227
    2328                if ( ! $menu_obj ) {
  • tests/phpunit/tests/customize/nav-menu-setting.php

    diff --git tests/phpunit/tests/customize/nav-menu-setting.php tests/phpunit/tests/customize/nav-menu-setting.php
    index 5d99fd6..dd1b584 100644
    class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase { 
    211211
    212212                $nav_menu_options = get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
    213213                $this->assertContains( $menu_id, $nav_menu_options['auto_add'] );
     214
     215                $menus = wp_get_nav_menus();
     216                $menus_ids = wp_list_pluck( $menus, 'term_id' );
     217                $i = array_search( $menu_id, $menus_ids );
     218                $this->assertNotFalse( $i, 'Update-previewed menu does not appear in wp_get_nav_menus()' );
     219                $filtered_menu = $menus[ $i ];
     220                $this->assertEquals( 'Name 2', $filtered_menu->name );
    214221        }
    215222
    216223        /**
    class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase { 
    249256
    250257                $nav_menu_options = $this->get_nav_menu_items_option();
    251258                $this->assertNotContains( $menu_id, $nav_menu_options['auto_add'] );
     259
     260                $menus = wp_get_nav_menus();
     261                $menus_ids = wp_list_pluck( $menus, 'term_id' );
     262                $i = array_search( $menu_id, $menus_ids );
     263                $this->assertNotFalse( $i, 'Insert-previewed menu was not injected into wp_get_nav_menus()' );
     264                $filtered_menu = $menus[ $i ];
     265                $this->assertEquals( 'New Menu Name 1', $filtered_menu->name );
    252266        }
    253267
    254268        /**